Files
firefly-iii/app/Factory/BudgetFactory.php

104 lines
2.5 KiB
PHP
Raw Normal View History

2018-02-19 19:44:46 +01:00
<?php
/**
* BudgetFactory.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
2018-02-19 19:44:46 +01:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-02-19 19:44:46 +01: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 19:44:46 +01:00
*
* This program is distributed in the hope that it will be useful,
2018-02-19 19:44:46 +01: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 19:44:46 +01: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 19:44:46 +01:00
*/
/** @noinspection MultipleReturnStatementsInspection */
2018-05-11 10:08:34 +02:00
declare(strict_types=1);
2018-02-19 19:44:46 +01:00
namespace FireflyIII\Factory;
use FireflyIII\Models\Budget;
use FireflyIII\User;
use Log;
2018-02-19 19:44:46 +01:00
/**
* Class BudgetFactory.
2018-02-19 19:44:46 +01:00
*/
class BudgetFactory
{
2019-02-13 17:38:41 +01:00
/** @var User */
private $user;
/**
* Constructor.
* @codeCoverageIgnore
*/
public function __construct()
{
if ('testing' === config('app.env')) {
2019-06-07 18:20:15 +02:00
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this)));
}
}
2018-02-19 19:44:46 +01:00
/**
* @param int|null $budgetId
* @param null|string $budgetName
*
* @return Budget|null
2019-08-17 10:46:55 +02:00
*
2018-02-19 19:44:46 +01:00
*/
public function find(?int $budgetId, ?string $budgetName): ?Budget
{
2018-04-02 14:42:07 +02:00
$budgetId = (int)$budgetId;
$budgetName = (string)$budgetName;
2018-02-19 19:44:46 +01:00
if (0 === $budgetId && '' === $budgetName) {
2018-02-19 19:44:46 +01:00
return null;
}
// first by ID:
if ($budgetId > 0) {
2018-03-01 20:54:50 +01:00
/** @var Budget $budget */
$budget = $this->user->budgets()->find($budgetId);
2018-04-02 14:42:07 +02:00
if (null !== $budget) {
2018-02-19 19:44:46 +01:00
return $budget;
}
}
if ('' !== $budgetName) {
2018-03-01 20:54:50 +01:00
$budget = $this->findByName($budgetName);
2018-04-02 14:42:07 +02:00
if (null !== $budget) {
2018-02-19 19:44:46 +01:00
return $budget;
}
}
return null;
}
2018-03-01 20:54:50 +01:00
/**
* @param string $name
*
* @return Budget|null
*/
public function findByName(string $name): ?Budget
{
2019-08-10 07:04:54 +02:00
return $this->user->budgets()->where('name', $name)->first();
2018-03-01 20:54:50 +01:00
}
2018-02-19 19:44:46 +01:00
/**
* @param User $user
*/
public function setUser(User $user): void
2018-02-19 19:44:46 +01:00
{
$this->user = $user;
}
2018-03-05 19:35:58 +01:00
}