firefly-iii/app/Factory/TransactionCurrencyFactory.php

101 lines
2.9 KiB
PHP
Raw Normal View History

2018-02-19 12:44:46 -06:00
<?php
2018-05-11 03:08:34 -05:00
2018-02-19 12:44:46 -06:00
/**
* TransactionCurrencyFactory.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
2018-05-11 03:08:34 -05:00
declare(strict_types=1);
2018-02-19 12:44:46 -06:00
namespace FireflyIII\Factory;
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
*
2018-03-30 15:40:20 -05:00
* @return TransactionCurrency|null
2018-03-25 02:01:43 -05:00
*/
2018-03-30 15:40:20 -05:00
public function create(array $data): ?TransactionCurrency
2018-03-25 02:01:43 -05:00
{
2018-03-30 15:40:20 -05:00
$result = null;
try {
/** @var TransactionCurrency $currency */
$result = TransactionCurrency::create(
[
'name' => $data['name'],
'code' => $data['code'],
'symbol' => $data['symbol'],
'decimal_places' => $data['decimal_places'],
]
);
} catch (QueryException $e) {
Log::error(sprintf('Could not create new currency: %s', $e->getMessage()));
}
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
{
2018-04-02 07:42:07 -05:00
$currencyCode = (string)$currencyCode;
$currencyId = (int)$currencyId;
2018-02-19 12:44:46 -06:00
if ('' === $currencyCode && $currencyId === 0) {
Log::warning('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:
2018-04-27 23:23:13 -05:00
if (\strlen($currencyCode) > 0) {
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
}