1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
/** |
5
|
|
|
* RoutineManager.php |
6
|
|
|
* Copyright (c) 2020 [email protected]. |
7
|
|
|
* |
8
|
|
|
* This file is part of the Firefly III bunq importer |
9
|
|
|
* (https://github.com/firefly-iii/bunq-importer). |
10
|
|
|
* |
11
|
|
|
* This program is free software: you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU Affero General Public License as |
13
|
|
|
* published by the Free Software Foundation, either version 3 of the |
14
|
|
|
* License, or (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* This program is distributed in the hope that it will be useful, |
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19
|
|
|
* GNU Affero General Public License for more details. |
20
|
|
|
* |
21
|
|
|
* You should have received a copy of the GNU Affero General Public License |
22
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace App\Bunq\Download; |
26
|
|
|
|
27
|
|
|
use App\Bunq\Download\JobStatus\JobStatusManager; |
28
|
|
|
use App\Bunq\Requests\PaymentList; |
29
|
|
|
use App\Exceptions\ImportException; |
30
|
|
|
use App\Services\Configuration\Configuration; |
31
|
|
|
use Illuminate\Support\Facades\Storage; |
32
|
|
|
use Illuminate\Support\Str; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Class ImportRoutineManager. |
36
|
|
|
*/ |
37
|
|
|
class RoutineManager |
38
|
|
|
{ |
39
|
|
|
/** @var array */ |
40
|
|
|
private $allErrors; |
41
|
|
|
/** @var array */ |
42
|
|
|
private $allMessages; |
43
|
|
|
/** @var array */ |
44
|
|
|
private $allWarnings; |
45
|
|
|
/** @var Configuration */ |
46
|
|
|
private $configuration; |
47
|
|
|
/** @var string */ |
48
|
|
|
private $downloadIdentifier; |
49
|
|
|
/** @var PaymentList */ |
50
|
|
|
private $paymentList; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Collect info on the current job, hold it in memory. |
54
|
|
|
* |
55
|
|
|
* ImportRoutineManager constructor. |
56
|
|
|
* |
57
|
|
|
* @param string|null $downloadIdentifier |
58
|
|
|
*/ |
59
|
|
|
public function __construct(string $downloadIdentifier = null) |
60
|
|
|
{ |
61
|
|
|
app('log')->debug('Constructed ImportRoutineManager'); |
62
|
|
|
|
63
|
|
|
// get line converter |
64
|
|
|
$this->allMessages = []; |
65
|
|
|
$this->allWarnings = []; |
66
|
|
|
$this->allErrors = []; |
67
|
|
|
if (null === $downloadIdentifier) { |
68
|
|
|
app('log')->debug('Was given no download identifier, will generate one.'); |
69
|
|
|
$this->generateDownloadIdentifier(); |
70
|
|
|
} |
71
|
|
|
if (null !== $downloadIdentifier) { |
72
|
|
|
app('log')->debug('Was given download identifier, will use it.'); |
73
|
|
|
$this->downloadIdentifier = $downloadIdentifier; |
74
|
|
|
} |
75
|
|
|
JobStatusManager::startOrFindJob($this->downloadIdentifier); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
public function getAllErrors(): array |
82
|
|
|
{ |
83
|
|
|
return $this->allErrors; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
public function getAllMessages(): array |
90
|
|
|
{ |
91
|
|
|
return $this->allMessages; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
public function getAllWarnings(): array |
98
|
|
|
{ |
99
|
|
|
return $this->allWarnings; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public function getDownloadIdentifier(): string |
106
|
|
|
{ |
107
|
|
|
return $this->downloadIdentifier; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param Configuration $configuration |
112
|
|
|
*/ |
113
|
|
|
public function setConfiguration(Configuration $configuration): void |
114
|
|
|
{ |
115
|
|
|
$this->configuration = $configuration; |
116
|
|
|
$this->paymentList = new PaymentList($configuration); |
117
|
|
|
$this->paymentList->setDownloadIdentifier($this->downloadIdentifier); |
118
|
|
|
|
119
|
|
|
app('log')->debug(sprintf('Download ImportRoutineManager: created new payment list with download identifier "%s"', $this->downloadIdentifier)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function start(): void |
123
|
|
|
{ |
124
|
|
|
app('log')->debug(sprintf('Now in %s', __METHOD__)); |
125
|
|
|
// download and store transactions from bunq. |
126
|
|
|
try { |
127
|
|
|
$transactions = $this->paymentList->getPaymentList(); |
128
|
|
|
} catch (ImportException $e) { |
129
|
|
|
app('log')->error($e->getMessage()); |
130
|
|
|
app('log')->error($e->getTraceAsString()); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$count = count($transactions); |
134
|
|
|
$this->mergeMessages($count); |
135
|
|
|
$this->mergeWarnings($count); |
136
|
|
|
$this->mergeErrors($count); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
private function generateDownloadIdentifier(): void |
140
|
|
|
{ |
141
|
|
|
app('log')->debug('Going to generate download identifier.'); |
142
|
|
|
$disk = Storage::disk('jobs'); |
143
|
|
|
$count = 0; |
144
|
|
|
do { |
145
|
|
|
$downloadIdentifier = Str::random(16); |
146
|
|
|
$count++; |
147
|
|
|
app('log')->debug(sprintf('Attempt #%d results in "%s"', $count, $downloadIdentifier)); |
148
|
|
|
} while ($count < 30 && $disk->exists($downloadIdentifier)); |
149
|
|
|
$this->downloadIdentifier = $downloadIdentifier; |
150
|
|
|
app('log')->info(sprintf('Download job identifier is "%s"', $downloadIdentifier)); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param int $count |
155
|
|
|
*/ |
156
|
|
|
private function mergeErrors(int $count): void |
157
|
|
|
{ |
158
|
|
|
$one = $this->paymentList->getErrors(); |
159
|
|
|
$total = []; |
160
|
|
|
for ($i = 0; $i < $count; $i++) { |
161
|
|
|
$total[$i] = array_merge($one[$i] ?? []); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$this->allErrors = $total; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param int $count |
169
|
|
|
*/ |
170
|
|
|
private function mergeMessages(int $count): void |
171
|
|
|
{ |
172
|
|
|
$one = $this->paymentList->getMessages(); |
173
|
|
|
$total = []; |
174
|
|
|
for ($i = 0; $i < $count; $i++) { |
175
|
|
|
$total[$i] = array_merge($one[$i] ?? []); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
$this->allMessages = $total; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param int $count |
183
|
|
|
*/ |
184
|
|
|
private function mergeWarnings(int $count): void |
185
|
|
|
{ |
186
|
|
|
$one = $this->paymentList->getWarnings(); |
187
|
|
|
$total = []; |
188
|
|
|
for ($i = 0; $i < $count; $i++) { |
189
|
|
|
$total[$i] = array_merge($one[$i] ?? []); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
$this->allWarnings = $total; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|