EmptyAccounts::processTransaction()   B
last analyzed

Complexity

Conditions 9
Paths 9

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 16
c 3
b 0
f 0
dl 0
loc 24
rs 8.0555
cc 9
nc 9
nop 1
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Accounts.php
5
 * Copyright (c) 2020 [email protected]
6
 *
7
 * This file is part of the Firefly III CSV importer
8
 * (https://github.com/firefly-iii/csv-importer).
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
22
 */
23
24
namespace App\Services\Import\Task;
25
26
use Log;
27
28
/**
29
 * Class EmptyAccounts
30
 *
31
 * A very simple task that makes sure that if the source of a deposit
32
 * or the destination of a withdrawal is empty, it will be set to "(no name)".
33
 */
34
class EmptyAccounts extends AbstractTask
35
{
36
    /**
37
     * @param array $group
38
     *
39
     * @return array
40
     */
41
    public function process(array $group): array
42
    {
43
        Log::debug('Now in EmptyAccounts::process()');
44
        $total = count($group['transactions']);
45
        foreach ($group['transactions'] as $index => $transaction) {
46
            Log::debug(sprintf('Now processing transaction %d of %d', $index + 1, $total));
47
            $group['transactions'][$index] = $this->processTransaction($transaction);
48
        }
49
50
        return $group;
51
    }
52
53
    /**
54
     * Returns true if the task requires the default account.
55
     *
56
     * @return bool
57
     */
58
    public function requiresDefaultAccount(): bool
59
    {
60
        return true;
61
    }
62
63
    /**
64
     * Returns true if the task requires the default currency of the user.
65
     *
66
     * @return bool
67
     */
68
    public function requiresTransactionCurrency(): bool
69
    {
70
        return false;
71
    }
72
    /**
73
     * @param array $transaction
74
     *
75
     * @return array
76
     */
77
    private function processTransaction(array $transaction): array
78
    {
79
        Log::debug('Now in EmptyAccounts::processTransaction()');
80
81
        if ('withdrawal' === $transaction['type']) {
82
            $destName = $transaction['destination_name'] ?? '';
83
            $destId   = (int) ($transaction['destination_id'] ?? 0);
84
            $destIban = $transaction['destination_iban'] ?? '';
85
            if ('' === $destName && 0 === $destId && ''=== $destIban) {
86
                Log::debug('Destination name + ID + IBAN of withdrawal are empty, set to "(no name)".');
87
                $transaction['destination_name'] = '(no name)';
88
            }
89
        }
90
        if ('deposit' === $transaction['type']) {
91
            $sourceName = $transaction['source_name'] ?? '';
92
            $sourceId   = (int) ($transaction['source_id'] ?? 0);
93
            $sourceIban = $transaction['source_iban'] ?? '';
94
            if ('' === $sourceName && 0 === $sourceId && '' === $sourceIban) {
95
                Log::debug('Source name + IBAN + ID of deposit are empty, set to "(no name)".');
96
                $transaction['source_name'] = '(no name)';
97
            }
98
        }
99
100
        return $transaction;
101
    }
102
103
}
104