1 | <?php |
||
2 | /** |
||
3 | * CurrencyMapper.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 | declare(strict_types=1); |
||
23 | |||
24 | namespace FireflyIII\Support\Import\Routine\File; |
||
25 | |||
26 | use FireflyIII\Models\TransactionCurrency; |
||
27 | use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; |
||
28 | use FireflyIII\User; |
||
29 | use Log; |
||
30 | |||
31 | /** |
||
32 | * Class CurrencyMapper |
||
33 | * @deprecated |
||
34 | * @codeCoverageIgnore |
||
35 | */ |
||
36 | class CurrencyMapper |
||
37 | { |
||
38 | /** @var CurrencyRepositoryInterface */ |
||
39 | private $repository; |
||
40 | /** @var User */ |
||
41 | private $user; |
||
42 | |||
43 | /** |
||
44 | * @param int|null $currencyId |
||
45 | * @param array $data |
||
46 | * |
||
47 | * @return TransactionCurrency|null |
||
48 | */ |
||
49 | public function map(?int $currencyId, array $data): ?TransactionCurrency |
||
50 | { |
||
51 | Log::debug('Now in CurrencyMapper::map()'); |
||
52 | if ((int)$currencyId > 0) { |
||
53 | $result = $this->repository->findNull($currencyId); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
54 | if (null !== $result) { |
||
55 | Log::debug(sprintf('Found currency %s based on ID, return it.', $result->code)); |
||
56 | |||
57 | return $result; |
||
58 | } |
||
59 | } |
||
60 | // try to find it by all other fields. |
||
61 | $fields = ['code' => 'findByCodeNull', 'symbol' => 'findBySymbolNull', 'name' => 'findByNameNull']; |
||
62 | foreach ($fields as $field => $function) { |
||
63 | $value = (string)($data[$field] ?? ''); |
||
64 | if ('' === $value) { |
||
65 | Log::debug(sprintf('Array does not contain a value for %s. Continue', $field)); |
||
66 | continue; |
||
67 | } |
||
68 | Log::debug(sprintf('Will search for currency using %s() and argument "%s".', $function, $value)); |
||
69 | $result = $this->repository->$function($value); |
||
70 | if (null !== $result) { |
||
71 | Log::debug(sprintf('Found result: Currency #%d, code "%s"', $result->id, $result->code)); |
||
72 | |||
73 | return $result; |
||
74 | } |
||
75 | } |
||
76 | if (!isset($data['code'])) { |
||
77 | return null; |
||
78 | } |
||
79 | |||
80 | // if still nothing, and fields not null, try to create it |
||
81 | $creation = [ |
||
82 | 'code' => $data['code'], |
||
83 | 'name' => $data['name'] ?? $data['code'], |
||
84 | 'symbol' => $data['symbol'] ?? $data['code'], |
||
85 | 'enabled' => true, |
||
86 | 'decimal_places' => 2, |
||
87 | ]; |
||
88 | |||
89 | // could be NULL |
||
90 | return $this->repository->store($creation); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * @param User $user |
||
95 | */ |
||
96 | public function setUser(User $user): void |
||
97 | { |
||
98 | $this->user = $user; |
||
99 | $this->repository = app(CurrencyRepositoryInterface::class); |
||
100 | $this->repository->setUser($user); |
||
101 | } |
||
102 | |||
103 | } |
||
104 |