. */ declare(strict_types=1); namespace FireflyIII\Factory; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\Category; use FireflyIII\User; use Illuminate\Database\QueryException; use Log; /** * Class CategoryFactory */ class CategoryFactory { private User $user; /** * @param int|null $categoryId * @param null|string $categoryName * * @return Category|null * @throws FireflyException */ public function findOrCreate(?int $categoryId, ?string $categoryName): ?Category { $categoryId = (int)$categoryId; $categoryName = (string)$categoryName; Log::debug(sprintf('Going to find category with ID %d and name "%s"', $categoryId, $categoryName)); if ('' === $categoryName && 0 === $categoryId) { return null; } // first by ID: if ($categoryId > 0) { /** @var Category $category */ $category = $this->user->categories()->find($categoryId); if (null !== $category) { return $category; } } if ('' !== $categoryName) { $category = $this->findByName($categoryName); if (null !== $category) { return $category; } try { return Category::create( [ 'user_id' => $this->user->id, 'name' => $categoryName, ] ); } catch (QueryException $e) { Log::error($e->getMessage()); throw new FireflyException('400003: Could not store new category.', 0, $e); } } return null; } /** * @param string $name * * @return Category|null */ public function findByName(string $name): ?Category { return $this->user->categories()->where('name', $name)->first(); } /** * @param User $user */ public function setUser(User $user): void { $this->user = $user; } }