1 | <?php |
||||
2 | declare(strict_types=1); |
||||
3 | /** |
||||
4 | * AccountFactory.php |
||||
5 | * Copyright (c) 2018 [email protected] |
||||
6 | * |
||||
7 | * This file is part of Firefly III. |
||||
8 | * |
||||
9 | * Firefly III is free software: you can redistribute it and/or modify |
||||
10 | * it under the terms of the GNU General Public License as published by |
||||
11 | * the Free Software Foundation, either version 3 of the License, or |
||||
12 | * (at your option) any later version. |
||||
13 | * |
||||
14 | * Firefly III is distributed in the hope that it will be useful, |
||||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
17 | * GNU General Public License for more details. |
||||
18 | * |
||||
19 | * You should have received a copy of the GNU General Public License |
||||
20 | * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
||||
21 | */ |
||||
22 | |||||
23 | |||||
24 | namespace FireflyIII\Factory; |
||||
25 | |||||
26 | use FireflyIII\Models\Account; |
||||
27 | use FireflyIII\Models\AccountType; |
||||
28 | use FireflyIII\Services\Internal\Support\AccountServiceTrait; |
||||
29 | use FireflyIII\User; |
||||
30 | |||||
31 | /** |
||||
32 | * Factory to create or return accounts. |
||||
33 | * |
||||
34 | * Class AccountFactory |
||||
35 | */ |
||||
36 | class AccountFactory |
||||
37 | { |
||||
38 | use AccountServiceTrait; |
||||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
39 | /** @var User */ |
||||
40 | private $user; |
||||
41 | |||||
42 | /** |
||||
43 | * @param array $data |
||||
44 | * |
||||
45 | * @return Account |
||||
46 | */ |
||||
47 | public function create(array $data): Account |
||||
48 | { |
||||
49 | $type = $this->getAccountType($data['account_type_id'], $data['accountType']); |
||||
50 | $data['iban'] = $this->filterIban($data['iban']); |
||||
51 | |||||
52 | |||||
53 | // account may exist already: |
||||
54 | $existingAccount = $this->find($data['name'], $type->type); |
||||
0 ignored issues
–
show
It seems like
$data['name'] can also be of type null ; however, parameter $accountName of FireflyIII\Factory\AccountFactory::find() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
55 | if (null !== $existingAccount) { |
||||
56 | return $existingAccount; |
||||
57 | } |
||||
58 | |||||
59 | |||||
60 | // create it: |
||||
61 | $databaseData |
||||
62 | = [ |
||||
63 | 'user_id' => $this->user->id, |
||||
64 | 'account_type_id' => $type->id, |
||||
65 | 'name' => $data['name'], |
||||
66 | 'virtual_balance' => $data['virtualBalance'] ?? '0', |
||||
67 | 'active' => true === $data['active'], |
||||
68 | 'iban' => $data['iban'], |
||||
69 | ]; |
||||
70 | |||||
71 | // remove virtual balance when not an asset account: |
||||
72 | if ($type->type !== AccountType::ASSET) { |
||||
73 | $databaseData['virtual_balance'] = '0'; |
||||
74 | } |
||||
75 | |||||
76 | $newAccount = Account::create($databaseData); |
||||
77 | $this->updateMetaData($newAccount, $data); |
||||
78 | |||||
79 | if ($this->validIBData($data) && $type->type === AccountType::ASSET) { |
||||
80 | $this->updateIB($newAccount, $data); |
||||
81 | } |
||||
82 | if (!$this->validIBData($data) && $type->type === AccountType::ASSET) { |
||||
83 | $this->deleteIB($newAccount); |
||||
84 | } |
||||
85 | // update note: |
||||
86 | if (isset($data['notes'])) { |
||||
87 | $this->updateNote($newAccount, $data['notes']); |
||||
88 | } |
||||
89 | |||||
90 | return $newAccount; |
||||
91 | } |
||||
92 | |||||
93 | /** |
||||
94 | * @param string $accountName |
||||
95 | * @param string $accountType |
||||
96 | * |
||||
97 | * @return Account|null |
||||
98 | */ |
||||
99 | public function find(string $accountName, string $accountType): ?Account |
||||
100 | { |
||||
101 | $type = AccountType::whereType($accountType)->first(); |
||||
102 | $accounts = $this->user->accounts()->where('account_type_id', $type->id)->get(['accounts.*']); |
||||
103 | |||||
104 | /** @var Account $object */ |
||||
105 | foreach ($accounts as $object) { |
||||
106 | if ($object->name === $accountName) { |
||||
107 | return $object; |
||||
108 | } |
||||
109 | } |
||||
110 | |||||
111 | return null; |
||||
112 | } |
||||
113 | |||||
114 | /** |
||||
115 | * @param string $accountName |
||||
116 | * @param string $accountType |
||||
117 | * |
||||
118 | * @return Account |
||||
119 | */ |
||||
120 | public function findOrCreate(string $accountName, string $accountType): Account |
||||
121 | { |
||||
122 | $type = AccountType::whereType($accountType)->first(); |
||||
123 | $accounts = $this->user->accounts()->where('account_type_id', $type->id)->get(['accounts.*']); |
||||
124 | |||||
125 | /** @var Account $object */ |
||||
126 | foreach ($accounts as $object) { |
||||
127 | if ($object->name === $accountName) { |
||||
128 | return $object; |
||||
129 | } |
||||
130 | } |
||||
131 | |||||
132 | return $this->create( |
||||
133 | [ |
||||
134 | 'user_id' => $this->user->id, |
||||
135 | 'name' => $accountName, |
||||
136 | 'account_type_id' => $type->id, |
||||
137 | 'accountType' => null, |
||||
138 | 'virtualBalance' => '0', |
||||
139 | 'iban' => null, |
||||
140 | 'active' => true, |
||||
141 | ] |
||||
142 | ); |
||||
143 | } |
||||
144 | |||||
145 | /** |
||||
146 | * @param User $user |
||||
147 | */ |
||||
148 | public function setUser(User $user): void |
||||
149 | { |
||||
150 | $this->user = $user; |
||||
151 | } |
||||
152 | |||||
153 | /** |
||||
154 | * @param int|null $accountTypeId |
||||
155 | * @param null|string $accountType |
||||
156 | * |
||||
157 | * @return AccountType|null |
||||
158 | */ |
||||
159 | protected function getAccountType(?int $accountTypeId, ?string $accountType): ?AccountType |
||||
160 | { |
||||
161 | $accountTypeId = (int)$accountTypeId; |
||||
162 | if ($accountTypeId > 0) { |
||||
163 | return AccountType::find($accountTypeId); |
||||
164 | } |
||||
165 | $type = config('firefly.accountTypeByIdentifier.' . (string)$accountType); |
||||
166 | $result = AccountType::whereType($type)->first(); |
||||
167 | if (null === $result && null !== $accountType) { |
||||
168 | // try as full name: |
||||
169 | $result = AccountType::whereType($accountType)->first(); |
||||
170 | } |
||||
171 | |||||
172 | return $result; |
||||
173 | |||||
174 | } |
||||
175 | |||||
176 | } |
||||
177 |