1 | <?php |
||
2 | /** |
||
3 | * TransactionCurrencyFactory.php |
||
4 | * Copyright (c) 2019 [email protected] |
||
5 | * |
||
6 | * This file is part of Firefly III (https://github.com/firefly-iii). |
||
7 | * |
||
8 | * This program is free software: you can redistribute it and/or modify |
||
9 | * it under the terms of the GNU Affero General Public License as |
||
10 | * published by the Free Software Foundation, either version 3 of the |
||
11 | * License, or (at your option) any later version. |
||
12 | * |
||
13 | * This program is distributed in the hope that it will be useful, |
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
16 | * GNU Affero General Public License for more details. |
||
17 | * |
||
18 | * You should have received a copy of the GNU Affero General Public License |
||
19 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||
20 | */ |
||
21 | |||
22 | /** @noinspection PhpDynamicAsStaticMethodCallInspection */ |
||
23 | /** @noinspection PhpUndefinedMethodInspection */ |
||
24 | /** @noinspection MultipleReturnStatementsInspection */ |
||
25 | |||
26 | declare(strict_types=1); |
||
27 | |||
28 | namespace FireflyIII\Factory; |
||
29 | |||
30 | use FireflyIII\Exceptions\FireflyException; |
||
31 | use FireflyIII\Models\TransactionCurrency; |
||
32 | use Illuminate\Database\QueryException; |
||
33 | use Log; |
||
34 | |||
35 | /** |
||
36 | * Class TransactionCurrencyFactory |
||
37 | */ |
||
38 | class TransactionCurrencyFactory |
||
39 | { |
||
40 | /** |
||
41 | * TransactionCurrencyFactory constructor. |
||
42 | * |
||
43 | * @codeCoverageIgnore |
||
44 | */ |
||
45 | public function __construct() |
||
46 | { |
||
47 | if ('testing' === config('app.env')) { |
||
48 | Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); |
||
49 | } |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @param array $data |
||
54 | * |
||
55 | * @return TransactionCurrency |
||
56 | * @throws FireflyException |
||
57 | */ |
||
58 | public function create(array $data): TransactionCurrency |
||
59 | { |
||
60 | try { |
||
61 | /** @var TransactionCurrency $currency */ |
||
62 | $result = TransactionCurrency::create( |
||
63 | [ |
||
64 | 'name' => $data['name'], |
||
65 | 'code' => $data['code'], |
||
66 | 'symbol' => $data['symbol'], |
||
67 | 'decimal_places' => $data['decimal_places'], |
||
68 | 'enabled' => $data['enabled'], |
||
69 | ] |
||
70 | ); |
||
71 | } catch (QueryException $e) { |
||
72 | $result = null; |
||
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
73 | Log::error(sprintf('Could not create new currency: %s', $e->getMessage())); |
||
74 | throw new FireflyException('400004: Could not store new currency.'); |
||
75 | } |
||
76 | |||
77 | return $result; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @param int|null $currencyId |
||
82 | * @param null|string $currencyCode |
||
83 | * |
||
84 | * @return TransactionCurrency|null |
||
85 | * |
||
86 | */ |
||
87 | public function find(?int $currencyId, ?string $currencyCode): ?TransactionCurrency |
||
88 | { |
||
89 | $currencyCode = (string)$currencyCode; |
||
90 | $currencyId = (int)$currencyId; |
||
91 | |||
92 | if ('' === $currencyCode && 0 === $currencyId) { |
||
93 | Log::debug('Cannot find anything on empty currency code and empty currency ID!'); |
||
94 | |||
95 | return null; |
||
96 | } |
||
97 | |||
98 | // first by ID: |
||
99 | if ($currencyId > 0) { |
||
100 | $currency = TransactionCurrency::find($currencyId); |
||
101 | if (null !== $currency) { |
||
102 | return $currency; |
||
103 | } |
||
104 | Log::warning(sprintf('Currency ID is %d but found nothing!', $currencyId)); |
||
105 | } |
||
106 | // then by code: |
||
107 | if ('' !== $currencyCode) { |
||
108 | $currency = TransactionCurrency::whereCode($currencyCode)->first(); |
||
109 | if (null !== $currency) { |
||
110 | return $currency; |
||
111 | } |
||
112 | Log::warning(sprintf('Currency code is %d but found nothing!', $currencyCode)); |
||
113 | } |
||
114 | Log::warning('Found nothing for currency.'); |
||
115 | |||
116 | return null; |
||
117 | } |
||
118 | |||
119 | |||
120 | } |
||
121 |