MonetaryAccountList   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 12
eloc 41
c 4
b 0
f 0
dl 0
loc 97
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A listing() 0 19 3
A getIban() 0 10 3
A processEntry() 0 29 5
A getColor() 0 3 1
1
<?php
2
/**
3
 * MonetaryAccountList.php
4
 * Copyright (c) 2020 [email protected].
5
 *
6
 * This file is part of the Firefly III bunq importer
7
 * (https://github.com/firefly-iii/bunq-importer).
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program 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 Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
21
 */
22
23
declare(strict_types=1);
24
25
namespace App\Bunq\Requests;
26
27
use App\Exceptions\BunqImporterException;
28
use App\Exceptions\ImportException;
29
use bunq\Exception\BunqException;
30
use bunq\Model\Core\BunqModel;
31
use bunq\Model\Generated\Endpoint\MonetaryAccount as BunqMonetaryAccount;
32
use bunq\Model\Generated\Endpoint\MonetaryAccountBank;
33
use bunq\Model\Generated\Endpoint\MonetaryAccountJoint;
34
use bunq\Model\Generated\Endpoint\MonetaryAccountSavings;
35
use bunq\Model\Generated\Object\Pointer;
36
37
/**
38
 * Class MonetaryAccount.
39
 *
40
 * @codeCoverageIgnore
41
 */
42
class MonetaryAccountList
43
{
44
    /**
45
     * Get list of monetary accounts from bunq. Format as necessary.
46
     *
47
     * @param array $params
48
     * @param array $customHeaders
49
     *
50
     * @throws ImportException
51
     * @return array
52
     */
53
    public function listing(array $params = null, array $customHeaders = null): array
54
    {
55
        app('log')->debug('Now calling bunq listing.');
56
        $params        = $params ?? [];
57
        $customHeaders = $customHeaders ?? [];
58
        $listing       = BunqMonetaryAccount::listing($params, $customHeaders);
59
        $return        = [];
60
        /** @var BunqMonetaryAccount $entry */
61
        foreach ($listing->getValue() as $entry) {
62
            try {
63
                $return[] = $this->processEntry($entry);
64
            } catch (BunqImporterException $e) {
65
                app('log')->error($e->getMessage());
66
                app('log')->error($e->getTraceAsString());
67
                throw new ImportException($e->getMessage());
68
            }
69
        }
70
71
        return $return;
72
    }
73
74
    /**
75
     * @param BunqModel $object
76
     *
77
     * @psalm-param MonetaryAccountBank|MonetaryAccountSavings|MonetaryAccountJoint $object
78
     *
79
     * @return string|null
80
     */
81
    private function getColor(BunqModel $object): ?string
82
    {
83
        return $object->getSetting()->getColor();
0 ignored issues
show
Bug introduced by
The method getSetting() does not exist on bunq\Model\Core\BunqModel. It seems like you code against a sub-type of bunq\Model\Core\BunqModel such as bunq\Model\Generated\End...\MonetaryAccountSavings or bunq\Model\Generated\Endpoint\MonetaryAccountJoint or bunq\Model\Generated\Endpoint\MonetaryAccountBank or bunq\Model\Generated\Endpoint\MonetaryAccountLight. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
        return $object->/** @scrutinizer ignore-call */ getSetting()->getColor();
Loading history...
84
    }
85
86
    /**
87
     * @param BunqModel $object
88
     *
89
     * @psalm-param MonetaryAccountBank|MonetaryAccountSavings|MonetaryAccountJoint $object
90
     * @return string|null
91
     */
92
    private function getIban(BunqModel $object): ?string
93
    {
94
        /** @var Pointer $pointer */
95
        foreach ($object->getAlias() as $pointer) {
0 ignored issues
show
Bug introduced by
The method getAlias() does not exist on bunq\Model\Core\BunqModel. It seems like you code against a sub-type of bunq\Model\Core\BunqModel such as bunq\Model\Generated\End...rPaymentServiceProvider or bunq\Model\Generated\Object\DraftPaymentEntry or bunq\Model\Generated\End...eInviteBankInquiryBatch or bunq\Model\Generated\Endpoint\MasterCardAction or bunq\Model\Generated\Endpoint\BunqMeTabEntry or bunq\Model\Generated\End...\MonetaryAccountSavings or bunq\Model\Generated\Endpoint\UserPerson or bunq\Model\Generated\Endpoint\MonetaryAccountJoint or bunq\Model\Generated\Endpoint\TransferwiseTransfer or bunq\Model\Generated\End...tactMerchantTransaction or bunq\Model\Generated\Endpoint\UserLight or bunq\Model\Generated\Endpoint\Payment or bunq\Model\Generated\End...MeFundraiserProfileUser or bunq\Model\Generated\Endpoint\TabUsageSingle or bunq\Model\Generated\Endpoint\UserCompany or bunq\Model\Generated\End...dealMerchantTransaction or bunq\Model\Generated\Endpoint\TokenQrRequestIdeal or bunq\Model\Generated\End...BunqMeFundraiserProfile or bunq\Model\Generated\End...viceNetherlandsIncoming or bunq\Model\Generated\Endpoint\MonetaryAccountBank or bunq\Model\Generated\Endpoint\MonetaryAccountLight or bunq\Model\Generated\Object\SchedulePaymentEntry or bunq\Model\Generated\Endpoint\TabUsageMultiple or bunq\Model\Generated\Endpoint\Invoice or bunq\Model\Generated\End...eMonetaryAccountInquiry or bunq\Model\Generated\Endpoint\RequestResponse or bunq\Model\Generated\Endpoint\InvoiceByUser or bunq\Model\Generated\End...fortMerchantTransaction or bunq\Model\Generated\Endpoint\CardDebit or bunq\Model\Generated\Object\CoOwner. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

95
        foreach ($object->/** @scrutinizer ignore-call */ getAlias() as $pointer) {
Loading history...
96
            if ('IBAN' === $pointer->getType()) {
97
                return $pointer->getValue();
98
            }
99
        }
100
101
        return null;
102
    }
103
104
    /**
105
     * @param BunqMonetaryAccount $entry
106
     *
107
     * @throws ImportException
108
     * @return array
109
     */
110
    private function processEntry(BunqMonetaryAccount $entry): array
111
    {
112
        /** @var MonetaryAccountBank $object */
113
        try {
114
            $object = $entry->getReferencedObject();
115
        } catch (BunqException $e) {
116
            throw new ImportException($e->getMessage());
117
        }
118
        switch (get_class($object)) {
119
            case MonetaryAccountBank::class:
120
            case MonetaryAccountSavings::class:
121
            case MonetaryAccountJoint::class:
122
                $return          = [
123
                    'id'          => $object->getId(),
124
                    'currency'    => $object->getCurrency(),
125
                    'description' => $object->getDescription(),
126
                    'uuid'        => $object->getPublicUuid(),
127
                    'status'      => $object->getStatus(),
128
                    'sub_status'  => $object->getSubStatus(),
129
                    'iban'        => null,
130
                    'color'       => null,
131
                ];
132
                $return['iban']  = $this->getIban($object);
133
                $return['color'] = $this->getColor($object);
134
135
                return $return;
136
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
137
        }
138
        throw new ImportException(sprintf('Bunq monetary account is unexpectedly of type "%s".', get_class($object)));
139
    }
140
}
141