|
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(); |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
throw new ImportException(sprintf('Bunq monetary account is unexpectedly of type "%s".', get_class($object))); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|