. */ /** @noinspection MultipleReturnStatementsInspection */ declare(strict_types=1); namespace FireflyIII\Factory; use FireflyIII\Models\PiggyBank; use FireflyIII\User; /** * Class PiggyBankFactory */ class PiggyBankFactory { /** @var User */ private $user; /** * @param int|null $piggyBankId * @param null|string $piggyBankName * * @return PiggyBank|null * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function find(?int $piggyBankId, ?string $piggyBankName): ?PiggyBank { $piggyBankId = (int)$piggyBankId; $piggyBankName = (string)$piggyBankName; if ('' === $piggyBankName && 0 === $piggyBankId) { return null; } // first find by ID: if ($piggyBankId > 0) { /** @var PiggyBank $piggyBank */ $piggyBank = $this->user->piggyBanks()->find($piggyBankId); if (null !== $piggyBank) { return $piggyBank; } } // then find by name: if (\strlen($piggyBankName) > 0) { /** @var PiggyBank $piggyBank */ $piggyBank = $this->findByName($piggyBankName); if (null !== $piggyBank) { return $piggyBank; } } return null; } /** * @param string $name * * @return PiggyBank|null */ public function findByName(string $name): ?PiggyBank { $set = $this->user->piggyBanks()->get(); /** @var PiggyBank $piggy */ foreach ($set as $piggy) { if ($piggy->name === $name) { return $piggy; } } return null; } /** * @param User $user */ public function setUser(User $user): void { $this->user = $user; } }