firefly-iii/app/Factory/CategoryFactory.php

104 lines
2.8 KiB
PHP
Raw Normal View History

2018-02-19 12:44:46 -06:00
<?php
2022-12-29 12:41:57 -06:00
2018-02-19 12:44:46 -06:00
/**
* CategoryFactory.php
2020-02-16 07:00:57 -06:00
* Copyright (c) 2019 james@firefly-iii.org
2018-02-19 12:44:46 -06:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-02-19 12:44:46 -06:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2018-02-19 12:44:46 -06:00
*
* This program is distributed in the hope that it will be useful,
2018-02-19 12:44:46 -06:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2018-02-19 12:44:46 -06:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-02-19 12:44:46 -06:00
*/
2018-05-11 03:08:34 -05:00
declare(strict_types=1);
2018-02-19 12:44:46 -06:00
namespace FireflyIII\Factory;
2021-03-28 04:46:23 -05:00
2019-10-30 14:02:21 -05:00
use FireflyIII\Exceptions\FireflyException;
2018-02-19 12:44:46 -06:00
use FireflyIII\Models\Category;
use FireflyIII\User;
2019-10-30 14:02:21 -05:00
use Illuminate\Database\QueryException;
2018-02-19 12:44:46 -06:00
use Log;
/**
* Class CategoryFactory
*/
class CategoryFactory
{
2020-10-23 12:11:25 -05:00
private User $user;
2018-02-19 12:44:46 -06:00
/**
2022-12-29 12:41:57 -06:00
* @param int|null $categoryId
* @param null|string $categoryName
2018-02-19 12:44:46 -06:00
*
* @return Category|null
2020-10-28 00:32:37 -05:00
* @throws FireflyException
2018-02-19 12:44:46 -06:00
*/
public function findOrCreate(?int $categoryId, ?string $categoryName): ?Category
{
2022-12-29 12:41:57 -06:00
$categoryId = (int)$categoryId;
$categoryName = (string)$categoryName;
2018-02-19 12:44:46 -06:00
Log::debug(sprintf('Going to find category with ID %d and name "%s"', $categoryId, $categoryName));
if ('' === $categoryName && 0 === $categoryId) {
2018-02-19 12:44:46 -06:00
return null;
}
// first by ID:
if ($categoryId > 0) {
2018-03-01 13:54:50 -06:00
/** @var Category $category */
$category = $this->user->categories()->find($categoryId);
2018-04-02 07:42:07 -05:00
if (null !== $category) {
2018-02-19 12:44:46 -06:00
return $category;
}
}
2019-02-13 10:38:41 -06:00
if ('' !== $categoryName) {
2018-03-01 13:54:50 -06:00
$category = $this->findByName($categoryName);
2018-04-02 07:42:07 -05:00
if (null !== $category) {
2018-02-19 12:44:46 -06:00
return $category;
}
2019-10-30 14:02:21 -05:00
try {
return Category::create(
[
'user_id' => $this->user->id,
'name' => $categoryName,
]
);
} catch (QueryException $e) {
Log::error($e->getMessage());
2021-04-07 00:28:43 -05:00
throw new FireflyException('400003: Could not store new category.', 0, $e);
2019-10-30 14:02:21 -05:00
}
2018-02-19 12:44:46 -06:00
}
return null;
}
2021-03-21 03:15:40 -05:00
/**
2022-12-29 12:41:57 -06:00
* @param string $name
2021-03-21 03:15:40 -05:00
*
* @return Category|null
*/
public function findByName(string $name): ?Category
{
return $this->user->categories()->where('name', $name)->first();
}
2018-02-19 12:44:46 -06:00
/**
2022-12-29 12:41:57 -06:00
* @param User $user
2018-02-19 12:44:46 -06:00
*/
public function setUser(User $user): void
2018-02-19 12:44:46 -06:00
{
$this->user = $user;
}
2018-03-05 12:35:58 -06:00
}