Total Complexity | 65 |
Total Lines | 422 |
Duplicated Lines | 0 % |
Changes | 12 | ||
Bugs | 1 | Features | 0 |
Complex classes like TransactionValidation often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TransactionValidation, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | trait TransactionValidation |
||
36 | { |
||
37 | /** |
||
38 | * Validates the given account information. Switches on given transaction type. |
||
39 | * |
||
40 | * @param Validator $validator |
||
41 | */ |
||
42 | public function validateAccountInformation(Validator $validator): void |
||
57 | } |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param Validator $validator |
||
62 | * @param int $index |
||
63 | * @param string $transactionType |
||
64 | * @param array $transaction |
||
65 | */ |
||
66 | protected function validateSingleAccount(Validator $validator, int $index, string $transactionType, array $transaction): void |
||
67 | { |
||
68 | /** @var AccountValidator $accountValidator */ |
||
69 | $accountValidator = app(AccountValidator::class); |
||
70 | |||
71 | $transactionType = $transaction['type'] ?? $transactionType; |
||
72 | $accountValidator->setTransactionType($transactionType); |
||
73 | |||
74 | // validate source account. |
||
75 | $sourceId = isset($transaction['source_id']) ? (int) $transaction['source_id'] : null; |
||
76 | $sourceName = $transaction['source_name'] ?? null; |
||
77 | $sourceIban = $transaction['source_iban'] ?? null; |
||
78 | $validSource = $accountValidator->validateSource($sourceId, $sourceName, $sourceIban); |
||
79 | |||
80 | // do something with result: |
||
81 | if (false === $validSource) { |
||
82 | $validator->errors()->add(sprintf('transactions.%d.source_id', $index), $accountValidator->sourceError); |
||
83 | $validator->errors()->add(sprintf('transactions.%d.source_name', $index), $accountValidator->sourceError); |
||
84 | |||
85 | return; |
||
86 | } |
||
87 | // validate destination account |
||
88 | $destinationId = isset($transaction['destination_id']) ? (int) $transaction['destination_id'] : null; |
||
89 | $destinationName = $transaction['destination_name'] ?? null; |
||
90 | $destinationIban = $transaction['destination_iban'] ?? null; |
||
91 | $validDestination = $accountValidator->validateDestination($destinationId, $destinationName, $destinationIban); |
||
92 | // do something with result: |
||
93 | if (false === $validDestination) { |
||
94 | $validator->errors()->add(sprintf('transactions.%d.destination_id', $index), $accountValidator->destError); |
||
95 | $validator->errors()->add(sprintf('transactions.%d.destination_name', $index), $accountValidator->destError); |
||
96 | |||
97 | return; |
||
98 | } |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Validates the given account information. Switches on given transaction type. |
||
103 | * |
||
104 | * @param Validator $validator |
||
105 | */ |
||
106 | public function validateAccountInformationUpdate(Validator $validator): void |
||
107 | { |
||
108 | Log::debug('Now in validateAccountInformationUpdate()'); |
||
109 | $transactions = $this->getTransactionsArray($validator); |
||
110 | |||
111 | /** |
||
112 | * @var int $index |
||
113 | * @var array $transaction |
||
114 | */ |
||
115 | foreach ($transactions as $index => $transaction) { |
||
116 | $this->validateSingleUpdate($validator, $index, $transaction); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @param Validator $validator |
||
122 | * @param int $index |
||
123 | * @param array $transaction |
||
124 | */ |
||
125 | protected function validateSingleUpdate(Validator $validator, int $index, array $transaction): void |
||
126 | { |
||
127 | /** @var AccountValidator $accountValidator */ |
||
128 | $accountValidator = app(AccountValidator::class); |
||
129 | $originalType = $this->getOriginalType((int)($transaction['transaction_journal_id'] ?? 0)); |
||
130 | $originalData = $this->getOriginalData((int)($transaction['transaction_journal_id'] ?? 0)); |
||
131 | $transactionType = $transaction['type'] ?? $originalType; |
||
132 | $accountValidator->setTransactionType($transactionType); |
||
133 | |||
134 | // if no account types are given, just skip the check. |
||
135 | if (!isset($transaction['source_id']) |
||
136 | && !isset($transaction['source_name']) |
||
137 | && !isset($transaction['destination_id']) |
||
138 | && !isset($transaction['destination_name'])) { |
||
139 | return; |
||
140 | } |
||
141 | |||
142 | // validate source account. |
||
143 | $sourceId = isset($transaction['source_id']) ? (int)$transaction['source_id'] : $originalData['source_id']; |
||
144 | $sourceName = $transaction['source_name'] ?? $originalData['source_name']; |
||
145 | $validSource = $accountValidator->validateSource($sourceId, $sourceName, null); |
||
146 | |||
147 | // do something with result: |
||
148 | if (false === $validSource) { |
||
149 | $validator->errors()->add(sprintf('transactions.%d.source_id', $index), $accountValidator->sourceError); |
||
150 | $validator->errors()->add(sprintf('transactions.%d.source_name', $index), $accountValidator->sourceError); |
||
151 | |||
152 | return; |
||
153 | } |
||
154 | // validate destination account |
||
155 | $destinationId = isset($transaction['destination_id']) ? (int)$transaction['destination_id'] : $originalData['destination_id']; |
||
156 | $destinationName = $transaction['destination_name'] ?? $originalData['destination_name']; |
||
157 | $validDestination = $accountValidator->validateDestination($destinationId, $destinationName, null); |
||
158 | // do something with result: |
||
159 | if (false === $validDestination) { |
||
160 | $validator->errors()->add(sprintf('transactions.%d.destination_id', $index), $accountValidator->destError); |
||
161 | $validator->errors()->add(sprintf('transactions.%d.destination_name', $index), $accountValidator->destError); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Adds an error to the validator when there are no transactions in the array of data. |
||
167 | * |
||
168 | * @param Validator $validator |
||
169 | */ |
||
170 | public function validateOneRecurrenceTransaction(Validator $validator): void |
||
171 | { |
||
172 | Log::debug('Now in validateOneRecurrenceTransaction()'); |
||
173 | $transactions = $this->getTransactionsArray($validator); |
||
174 | |||
175 | // need at least one transaction |
||
176 | if (0 === count($transactions)) { |
||
177 | $validator->errors()->add('transactions', (string)trans('validation.at_least_one_transaction')); |
||
178 | } |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Adds an error to the validator when there are no transactions in the array of data. |
||
183 | * |
||
184 | * @param Validator $validator |
||
185 | */ |
||
186 | public function validateOneTransaction(Validator $validator): void |
||
187 | { |
||
188 | Log::debug('Now in validateOneTransaction()'); |
||
189 | $transactions = $this->getTransactionsArray($validator); |
||
190 | // need at least one transaction |
||
191 | if (0 === count($transactions)) { |
||
192 | $validator->errors()->add('transactions.0.description', (string)trans('validation.at_least_one_transaction')); |
||
193 | Log::debug('Added error: at_least_one_transaction.'); |
||
194 | return; |
||
195 | } |
||
196 | Log::debug('Added NO errors.'); |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * All types of splits must be equal. |
||
201 | * |
||
202 | * @param Validator $validator |
||
203 | */ |
||
204 | public function validateTransactionTypes(Validator $validator): void |
||
205 | { |
||
206 | Log::debug('Now in validateTransactionTypes()'); |
||
207 | $transactions = $this->getTransactionsArray($validator); |
||
208 | |||
209 | $types = []; |
||
210 | foreach ($transactions as $transaction) { |
||
211 | $types[] = $transaction['type'] ?? 'invalid'; |
||
212 | } |
||
213 | $unique = array_unique($types); |
||
214 | if (count($unique) > 1) { |
||
215 | $validator->errors()->add('transactions.0.type', (string)trans('validation.transaction_types_equal')); |
||
216 | |||
217 | return; |
||
218 | } |
||
219 | $first = $unique[0] ?? 'invalid'; |
||
220 | if ('invalid' === $first) { |
||
221 | $validator->errors()->add('transactions.0.type', (string)trans('validation.invalid_transaction_type')); |
||
222 | } |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * All types of splits must be equal. |
||
227 | * |
||
228 | * @param Validator $validator |
||
229 | */ |
||
230 | public function validateTransactionTypesForUpdate(Validator $validator): void |
||
247 | } |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * @param array $array |
||
252 | * |
||
253 | * @return bool |
||
254 | */ |
||
255 | private function arrayEqual(array $array): bool |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * @param int $journalId |
||
262 | * |
||
263 | * @return array |
||
264 | */ |
||
265 | private function getOriginalData(int $journalId): array |
||
266 | { |
||
267 | $return = [ |
||
268 | 'source_id' => 0, |
||
269 | 'source_name' => '', |
||
270 | 'destination_id' => 0, |
||
271 | 'destination_name' => '', |
||
272 | ]; |
||
273 | if (0 === $journalId) { |
||
274 | return $return; |
||
275 | } |
||
276 | /** @var Transaction $source */ |
||
277 | $source = Transaction::where('transaction_journal_id', $journalId)->where('amount', '<', 0)->with(['account'])->first(); |
||
278 | if (null !== $source) { |
||
279 | $return['source_id'] = $source->account_id; |
||
280 | $return['source_name'] = $source->account->name; |
||
281 | } |
||
282 | /** @var Transaction $destination */ |
||
283 | $destination = Transaction::where('transaction_journal_id', $journalId)->where('amount', '>', 0)->with(['account'])->first(); |
||
284 | if (null !== $source) { |
||
285 | $return['destination_id'] = $destination->account_id; |
||
286 | $return['destination_name'] = $destination->account->name; |
||
287 | } |
||
288 | |||
289 | return $return; |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * @param int $journalId |
||
294 | * |
||
295 | * @return string |
||
296 | */ |
||
297 | private function getOriginalType(int $journalId): string |
||
298 | { |
||
299 | if (0 === $journalId) { |
||
300 | return 'invalid'; |
||
301 | } |
||
302 | /** @var TransactionJournal $journal */ |
||
303 | $journal = TransactionJournal::with(['transactionType'])->find($journalId); |
||
304 | if (null !== $journal) { |
||
305 | return strtolower($journal->transactionType->type); |
||
306 | } |
||
307 | |||
308 | return 'invalid'; |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * @param Validator $validator |
||
313 | * |
||
314 | * @return array |
||
315 | */ |
||
316 | protected function getTransactionsArray(Validator $validator): array |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * @param Validator $validator |
||
337 | */ |
||
338 | private function validateEqualAccounts(Validator $validator): void |
||
373 | } |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * @param Validator $validator |
||
378 | * @param TransactionGroup $transactionGroup |
||
379 | */ |
||
380 | private function validateEqualAccountsForUpdate(Validator $validator, TransactionGroup $transactionGroup): void |
||
460 |