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\Services\Sync; |
26
|
|
|
|
27
|
|
|
use App\Exceptions\ImportException; |
28
|
|
|
use App\Services\Configuration\Configuration; |
29
|
|
|
use App\Services\Sync\JobStatus\JobStatusManager; |
30
|
|
|
use Illuminate\Support\Facades\Storage; |
31
|
|
|
use Illuminate\Support\Str; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Class RoutineManager. |
35
|
|
|
*/ |
36
|
|
|
class RoutineManager |
37
|
|
|
{ |
38
|
|
|
/** @var array */ |
39
|
|
|
private $allErrors; |
40
|
|
|
/** @var array */ |
41
|
|
|
private $allMessages; |
42
|
|
|
/** @var array */ |
43
|
|
|
private $allWarnings; |
44
|
|
|
/** @var ParseBunqDownload */ |
45
|
|
|
private $bunqParser; |
46
|
|
|
/** @var Configuration */ |
47
|
|
|
private $configuration; |
48
|
|
|
/** @var string */ |
49
|
|
|
private $downloadIdentifier; |
50
|
|
|
/** @var string */ |
51
|
|
|
private $syncIdentifier; |
52
|
|
|
/** @var FilterTransactions */ |
53
|
|
|
private $transactionFilter; |
54
|
|
|
/** @var GenerateTransactions */ |
55
|
|
|
private $transactionGenerator; |
56
|
|
|
/** @var SendTransactions */ |
57
|
|
|
private $transactionSender; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Collect info on the current job, hold it in memory. |
61
|
|
|
* |
62
|
|
|
* ImportRoutineManager constructor. |
63
|
|
|
* |
64
|
|
|
* @param null|string $syncIdentifier |
65
|
|
|
*/ |
66
|
|
|
public function __construct(?string $syncIdentifier = null) |
67
|
|
|
{ |
68
|
|
|
app('log')->debug('Constructed RoutineManager for sync'); |
69
|
|
|
|
70
|
|
|
$this->bunqParser = new ParseBunqDownload; |
71
|
|
|
$this->transactionGenerator = new GenerateTransactions; |
72
|
|
|
$this->transactionSender = new SendTransactions; |
73
|
|
|
$this->transactionFilter = new FilterTransactions; |
74
|
|
|
|
75
|
|
|
// get line converter |
76
|
|
|
$this->allMessages = []; |
77
|
|
|
$this->allWarnings = []; |
78
|
|
|
$this->allErrors = []; |
79
|
|
|
if (null === $syncIdentifier) { |
80
|
|
|
$this->generateSyncIdentifier(); |
81
|
|
|
} |
82
|
|
|
if (null !== $syncIdentifier) { |
83
|
|
|
$this->syncIdentifier = $syncIdentifier; |
84
|
|
|
} |
85
|
|
|
$this->bunqParser->setIdentifier($this->syncIdentifier); |
86
|
|
|
$this->transactionSender->setIdentifier($this->syncIdentifier); |
87
|
|
|
$this->transactionGenerator->setIdentifier($this->syncIdentifier); |
88
|
|
|
$this->transactionFilter->setIdentifier($this->syncIdentifier); |
89
|
|
|
|
90
|
|
|
JobStatusManager::startOrFindJob($this->syncIdentifier); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
public function getAllErrors(): array |
97
|
|
|
{ |
98
|
|
|
return $this->allErrors; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
public function getAllMessages(): array |
105
|
|
|
{ |
106
|
|
|
return $this->allMessages; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
|
public function getAllWarnings(): array |
113
|
|
|
{ |
114
|
|
|
return $this->allWarnings; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public function getDownloadIdentifier(): string |
121
|
|
|
{ |
122
|
|
|
return $this->downloadIdentifier; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $downloadIdentifier |
127
|
|
|
*/ |
128
|
|
|
public function setDownloadIdentifier(string $downloadIdentifier): void |
129
|
|
|
{ |
130
|
|
|
$this->downloadIdentifier = $downloadIdentifier; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
public function getSyncIdentifier(): string |
137
|
|
|
{ |
138
|
|
|
return $this->syncIdentifier; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $syncIdentifier |
143
|
|
|
*/ |
144
|
|
|
public function setSyncIdentifier(string $syncIdentifier): void |
145
|
|
|
{ |
146
|
|
|
$this->syncIdentifier = $syncIdentifier; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param Configuration $configuration |
151
|
|
|
*/ |
152
|
|
|
public function setConfiguration(Configuration $configuration): void |
153
|
|
|
{ |
154
|
|
|
$this->configuration = $configuration; |
155
|
|
|
$this->transactionSender->setConfiguration($configuration); |
156
|
|
|
$this->transactionGenerator->setConfiguration($configuration); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Start the import. |
161
|
|
|
* |
162
|
|
|
* @throws ImportException |
163
|
|
|
*/ |
164
|
|
|
public function start(): void |
165
|
|
|
{ |
166
|
|
|
app('log')->debug(sprintf('Now in %s', __METHOD__)); |
167
|
|
|
|
168
|
|
|
// get JSON file from bunq download |
169
|
|
|
app('log')->debug('Going to parse bunq download.'); |
170
|
|
|
$array = $this->bunqParser->getDownload($this->downloadIdentifier); |
171
|
|
|
app('log')->debug('Done parsing bunq download.'); |
172
|
|
|
|
173
|
|
|
// generate Firefly III ready transactions: |
174
|
|
|
app('log')->debug('Generating Firefly III transactions.'); |
175
|
|
|
$this->transactionGenerator->collectTargetAccounts(); |
176
|
|
|
|
177
|
|
|
$transactions = $this->transactionGenerator->getTransactions($array); |
178
|
|
|
app('log')->debug(sprintf('Generated %d Firefly III transactions.', count($transactions))); |
179
|
|
|
|
180
|
|
|
$filtered = $this->transactionFilter->filter($transactions); |
181
|
|
|
app('log')->debug(sprintf('Filtered down to %d Firefly III transactions.', count($filtered))); |
182
|
|
|
|
183
|
|
|
// send to Firefly III. |
184
|
|
|
app('log')->debug('Going to send them to Firefly III.'); |
185
|
|
|
$sent = $this->transactionSender->send($filtered); |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
private function generateSyncIdentifier(): void |
189
|
|
|
{ |
190
|
|
|
app('log')->debug('Going to generate sync job identifier.'); |
191
|
|
|
$disk = Storage::disk('jobs'); |
192
|
|
|
$count = 0; |
193
|
|
|
do { |
194
|
|
|
$syncIdentifier = Str::random(16); |
195
|
|
|
$count++; |
196
|
|
|
app('log')->debug(sprintf('Attempt #%d results in "%s"', $count, $syncIdentifier)); |
197
|
|
|
} while ($count < 30 && $disk->exists($syncIdentifier)); |
198
|
|
|
$this->syncIdentifier = $syncIdentifier; |
199
|
|
|
app('log')->info(sprintf('Sync job identifier is "%s"', $syncIdentifier)); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|