Conditions | 4 |
Paths | 4 |
Total Lines | 129 |
Code Lines | 97 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
244 | private function makeMultiGroup(TransactionJournal $journal): void |
||
245 | { |
||
246 | // double check transaction count. |
||
247 | if ($journal->transactions->count() <= 2) { |
||
248 | // @codeCoverageIgnoreStart |
||
249 | Log::debug(sprintf('Will not try to convert journal #%d because it has 2 or less transactions.', $journal->id)); |
||
250 | |||
251 | return; |
||
252 | // @codeCoverageIgnoreEnd |
||
253 | } |
||
254 | Log::debug(sprintf('Will now try to convert journal #%d', $journal->id)); |
||
255 | |||
256 | $this->journalRepository->setUser($journal->user); |
||
257 | $this->groupFactory->setUser($journal->user); |
||
258 | $this->cliRepository->setUser($journal->user); |
||
259 | |||
260 | $data = [ |
||
261 | // mandatory fields. |
||
262 | 'group_title' => $journal->description, |
||
263 | 'transactions' => [], |
||
264 | ]; |
||
265 | $destTransactions = $this->getDestinationTransactions($journal); |
||
266 | $budgetId = $this->cliRepository->getJournalBudgetId($journal); |
||
267 | $categoryId = $this->cliRepository->getJournalCategoryId($journal); |
||
268 | $notes = $this->cliRepository->getNoteText($journal); |
||
269 | $tags = $this->cliRepository->getTags($journal); |
||
270 | $internalRef = $this->cliRepository->getMetaField($journal, 'internal-reference'); |
||
271 | $sepaCC = $this->cliRepository->getMetaField($journal, 'sepa_cc'); |
||
272 | $sepaCtOp = $this->cliRepository->getMetaField($journal, 'sepa_ct_op'); |
||
273 | $sepaCtId = $this->cliRepository->getMetaField($journal, 'sepa_ct_id'); |
||
274 | $sepaDb = $this->cliRepository->getMetaField($journal, 'sepa_db'); |
||
275 | $sepaCountry = $this->cliRepository->getMetaField($journal, 'sepa_country'); |
||
276 | $sepaEp = $this->cliRepository->getMetaField($journal, 'sepa_ep'); |
||
277 | $sepaCi = $this->cliRepository->getMetaField($journal, 'sepa_ci'); |
||
278 | $sepaBatchId = $this->cliRepository->getMetaField($journal, 'sepa_batch_id'); |
||
279 | $externalId = $this->cliRepository->getMetaField($journal, 'external-id'); |
||
280 | $originalSource = $this->cliRepository->getMetaField($journal, 'original-source'); |
||
281 | $recurrenceId = $this->cliRepository->getMetaField($journal, 'recurrence_id'); |
||
282 | $bunq = $this->cliRepository->getMetaField($journal, 'bunq_payment_id'); |
||
283 | $hash = $this->cliRepository->getMetaField($journal, 'import_hash'); |
||
284 | $hashTwo = $this->cliRepository->getMetaField($journal, 'import_hash_v2'); |
||
285 | $interestDate = $this->cliRepository->getMetaDate($journal, 'interest_date'); |
||
286 | $bookDate = $this->cliRepository->getMetaDate($journal, 'book_date'); |
||
287 | $processDate = $this->cliRepository->getMetaDate($journal, 'process_date'); |
||
288 | $dueDate = $this->cliRepository->getMetaDate($journal, 'due_date'); |
||
289 | $paymentDate = $this->cliRepository->getMetaDate($journal, 'payment_date'); |
||
290 | $invoiceDate = $this->cliRepository->getMetaDate($journal, 'invoice_date'); |
||
291 | |||
292 | Log::debug(sprintf('Will use %d positive transactions to create a new group.', $destTransactions->count())); |
||
293 | |||
294 | /** @var Transaction $transaction */ |
||
295 | foreach ($destTransactions as $transaction) { |
||
296 | Log::debug(sprintf('Now going to add transaction #%d to the array.', $transaction->id)); |
||
297 | $opposingTr = $this->findOpposingTransaction($journal, $transaction); |
||
298 | |||
299 | if (null === $opposingTr) { |
||
300 | // @codeCoverageIgnoreStart |
||
301 | $this->error( |
||
302 | sprintf( |
||
303 | 'Journal #%d has no opposing transaction for transaction #%d. Cannot upgrade this entry.', |
||
304 | $journal->id, $transaction->id |
||
305 | ) |
||
306 | ); |
||
307 | continue; |
||
308 | // @codeCoverageIgnoreEnd |
||
309 | } |
||
310 | |||
311 | // overrule journal category with transaction category. |
||
312 | $budgetId = $this->getTransactionBudget($transaction, $opposingTr) ?? $budgetId; |
||
313 | $categoryId = $this->getTransactionCategory($transaction, $opposingTr) ?? $categoryId; |
||
314 | |||
315 | $tArray = [ |
||
316 | 'type' => strtolower($journal->transactionType->type), |
||
317 | 'date' => $journal->date, |
||
318 | 'user' => $journal->user_id, |
||
319 | 'currency_id' => $transaction->transaction_currency_id, |
||
320 | 'foreign_currency_id' => $transaction->foreign_currency_id, |
||
321 | 'amount' => $transaction->amount, |
||
322 | 'foreign_amount' => $transaction->foreign_amount, |
||
323 | 'description' => $transaction->description ?? $journal->description, |
||
324 | 'source_id' => $opposingTr->account_id, |
||
325 | 'destination_id' => $transaction->account_id, |
||
326 | 'budget_id' => $budgetId, |
||
327 | 'category_id' => $categoryId, |
||
328 | 'bill_id' => $journal->bill_id, |
||
329 | 'notes' => $notes, |
||
330 | 'tags' => $tags, |
||
331 | 'internal_reference' => $internalRef, |
||
332 | 'sepa_cc' => $sepaCC, |
||
333 | 'sepa_ct_op' => $sepaCtOp, |
||
334 | 'sepa_ct_id' => $sepaCtId, |
||
335 | 'sepa_db' => $sepaDb, |
||
336 | 'sepa_country' => $sepaCountry, |
||
337 | 'sepa_ep' => $sepaEp, |
||
338 | 'sepa_ci' => $sepaCi, |
||
339 | 'sepa_batch_id' => $sepaBatchId, |
||
340 | 'external_id' => $externalId, |
||
341 | 'original-source' => $originalSource, |
||
342 | 'recurrence_id' => $recurrenceId, |
||
343 | 'bunq_payment_id' => $bunq, |
||
344 | 'import_hash' => $hash, |
||
345 | 'import_hash_v2' => $hashTwo, |
||
346 | 'interest_date' => $interestDate, |
||
347 | 'book_date' => $bookDate, |
||
348 | 'process_date' => $processDate, |
||
349 | 'due_date' => $dueDate, |
||
350 | 'payment_date' => $paymentDate, |
||
351 | 'invoice_date' => $invoiceDate, |
||
352 | ]; |
||
353 | |||
354 | $data['transactions'][] = $tArray; |
||
355 | } |
||
356 | Log::debug(sprintf('Now calling transaction journal factory (%d transactions in array)', count($data['transactions']))); |
||
357 | $group = $this->groupFactory->create($data); |
||
358 | Log::debug('Done calling transaction journal factory'); |
||
359 | |||
360 | // delete the old transaction journal. |
||
361 | $this->service->destroy($journal); |
||
362 | |||
363 | $this->count++; |
||
364 | |||
365 | // report on result: |
||
366 | Log::debug( |
||
367 | sprintf('Migrated journal #%d into group #%d with these journals: #%s', |
||
368 | $journal->id, $group->id, implode(', #', $group->transactionJournals->pluck('id')->toArray())) |
||
369 | ); |
||
370 | $this->line( |
||
371 | sprintf('Migrated journal #%d into group #%d with these journals: #%s', |
||
372 | $journal->id, $group->id, implode(', #', $group->transactionJournals->pluck('id')->toArray())) |
||
373 | ); |
||
451 |