firefly-iii/app/Factory/TransactionCurrencyFactory.php

106 lines
3.4 KiB
PHP
Raw Normal View History

2018-02-19 12:44:46 -06:00
<?php
/**
* TransactionCurrencyFactory.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
*/
/** @noinspection PhpDynamicAsStaticMethodCallInspection */
/** @noinspection PhpUndefinedMethodInspection */
/** @noinspection MultipleReturnStatementsInspection */
2018-05-11 03:08:34 -05:00
declare(strict_types=1);
2018-02-19 12:44:46 -06:00
namespace FireflyIII\Factory;
2019-10-30 14:02:21 -05:00
use FireflyIII\Exceptions\FireflyException;
2018-02-19 12:44:46 -06:00
use FireflyIII\Models\TransactionCurrency;
2018-03-30 15:40:20 -05:00
use Illuminate\Database\QueryException;
use Log;
2018-02-19 12:44:46 -06:00
/**
* Class TransactionCurrencyFactory
*/
class TransactionCurrencyFactory
{
2018-03-25 02:01:43 -05:00
/**
* @param array $data
*
* @return TransactionCurrency
2021-03-21 03:15:40 -05:00
* @throws FireflyException
2018-03-25 02:01:43 -05:00
*/
2019-10-30 14:02:21 -05:00
public function create(array $data): TransactionCurrency
2018-03-25 02:01:43 -05:00
{
2018-03-30 15:40:20 -05:00
try {
/** @var TransactionCurrency $currency */
$result = TransactionCurrency::create(
[
'name' => $data['name'],
'code' => $data['code'],
'symbol' => $data['symbol'],
'decimal_places' => $data['decimal_places'],
'enabled' => $data['enabled'],
2018-03-30 15:40:20 -05:00
]
);
} catch (QueryException $e) {
2018-07-26 21:46:21 -05:00
$result = null;
2018-03-30 15:40:20 -05:00
Log::error(sprintf('Could not create new currency: %s', $e->getMessage()));
2021-04-07 00:28:43 -05:00
throw new FireflyException('400004: Could not store new currency.', 0, $e);
2018-03-30 15:40:20 -05:00
}
2018-03-25 02:01:43 -05:00
2018-03-30 15:40:20 -05:00
return $result;
2018-03-25 02:01:43 -05:00
}
2018-02-19 12:44:46 -06:00
/**
* @param int|null $currencyId
* @param null|string $currencyCode
*
* @return TransactionCurrency|null
*/
public function find(?int $currencyId, ?string $currencyCode): ?TransactionCurrency
{
2021-03-21 03:15:40 -05:00
$currencyCode = (string)$currencyCode;
$currencyId = (int)$currencyId;
2018-02-19 12:44:46 -06:00
if ('' === $currencyCode && 0 === $currencyId) {
Log::debug('Cannot find anything on empty currency code and empty currency ID!');
2018-06-06 14:23:00 -05:00
2018-02-19 12:44:46 -06:00
return null;
}
// first by ID:
if ($currencyId > 0) {
$currency = TransactionCurrency::find($currencyId);
2018-04-02 07:42:07 -05:00
if (null !== $currency) {
2018-02-19 12:44:46 -06:00
return $currency;
}
Log::warning(sprintf('Currency ID is %d but found nothing!', $currencyId));
2018-02-19 12:44:46 -06:00
}
// then by code:
2019-02-13 10:38:41 -06:00
if ('' !== $currencyCode) {
2018-02-19 12:44:46 -06:00
$currency = TransactionCurrency::whereCode($currencyCode)->first();
2018-04-02 07:42:07 -05:00
if (null !== $currency) {
2018-02-19 12:44:46 -06:00
return $currency;
}
Log::warning(sprintf('Currency code is %d but found nothing!', $currencyCode));
2018-02-19 12:44:46 -06:00
}
Log::warning('Found nothing for currency.');
2018-02-19 12:44:46 -06:00
return null;
}
2018-03-05 12:35:58 -06:00
}