Files
firefly-iii/app/Repositories/ObjectGroup/CreatesObjectGroups.php

66 lines
1.4 KiB
PHP
Raw Normal View History

2020-06-07 11:31:01 +02:00
<?php
declare(strict_types=1);
namespace FireflyIII\Repositories\ObjectGroup;
use FireflyIII\Models\ObjectGroup;
2020-06-20 10:22:07 +02:00
use FireflyIII\User;
2020-06-07 11:31:01 +02:00
/**
* Trait CreatesObjectGroups
*/
trait CreatesObjectGroups
{
/**
* @param string $title
*
* @return null|ObjectGroup
*/
protected function findObjectGroup(string $title): ?ObjectGroup
{
2020-06-20 10:22:07 +02:00
return $this->user->objectGroups()->where('title', $title)->first();
2020-06-07 11:31:01 +02:00
}
2020-06-20 10:22:07 +02:00
2020-06-07 11:31:01 +02:00
/**
2020-06-20 10:22:07 +02:00
* @param User $user
2020-06-07 11:31:01 +02:00
* @param string $title
*
* @return ObjectGroup|null
*/
protected function findOrCreateObjectGroup(string $title): ?ObjectGroup
{
$group = null;
$maxOrder = $this->getObjectGroupMaxOrder();
if (!$this->hasObjectGroup($title)) {
return ObjectGroup::create(
[
2020-06-20 10:22:07 +02:00
'user_id' => $this->user->id,
'title' => $title,
'order' => $maxOrder + 1,
2020-06-07 11:31:01 +02:00
]
);
}
return $this->findObjectGroup($title);
}
/**
* @return int
*/
protected function getObjectGroupMaxOrder(): int
{
2020-06-20 10:22:07 +02:00
return (int) $this->user->objectGroups()->max('order');
2020-06-07 11:31:01 +02:00
}
/**
* @param string $title
*
* @return bool
*/
protected function hasObjectGroup(string $title): bool
{
2020-06-20 10:22:07 +02:00
return 1 === $this->user->objectGroups()->where('title', $title)->count();
2020-06-07 11:31:01 +02:00
}
}