1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ListAccountsRequest.php |
4
|
|
|
* Copyright (c) 2018 [email protected] |
5
|
|
|
* |
6
|
|
|
* This file is part of Firefly III. |
7
|
|
|
* |
8
|
|
|
* Firefly III is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU General Public License as published by |
10
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* Firefly III is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU General Public License |
19
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
declare(strict_types=1); |
23
|
|
|
|
24
|
|
|
namespace FireflyIII\Services\Spectre\Request; |
25
|
|
|
|
26
|
|
|
use FireflyIII\Exceptions\FireflyException; |
27
|
|
|
use FireflyIII\Services\Spectre\Exception\SpectreException; |
28
|
|
|
use FireflyIII\Services\Spectre\Object\Account; |
29
|
|
|
use FireflyIII\Services\Spectre\Object\Login; |
30
|
|
|
use Log; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Class ListAccountsRequest |
34
|
|
|
*/ |
35
|
|
|
class ListAccountsRequest extends SpectreRequest |
36
|
|
|
{ |
37
|
|
|
/** @var array */ |
38
|
|
|
private $accounts = []; |
39
|
|
|
/** @var Login */ |
40
|
|
|
private $login; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @throws SpectreException |
44
|
|
|
* @throws FireflyException |
45
|
|
|
*/ |
46
|
|
|
public function call(): void |
47
|
|
|
{ |
48
|
|
|
$hasNextPage = true; |
49
|
|
|
$nextId = 0; |
50
|
|
|
while ($hasNextPage) { |
51
|
|
|
Log::debug(sprintf('Now calling ListAccountsRequest for next_id %d', $nextId)); |
52
|
|
|
$parameters = ['from_id' => $nextId, 'login_id' => $this->login->getId()]; |
53
|
|
|
$uri = '/api/v3/accounts?' . http_build_query($parameters); |
54
|
|
|
$response = $this->sendSignedSpectreGet($uri, []); |
55
|
|
|
|
56
|
|
|
// count entries: |
57
|
|
|
Log::debug(sprintf('Found %d entries in data-array', count($response['data']))); |
58
|
|
|
|
59
|
|
|
// extract next ID |
60
|
|
|
$hasNextPage = false; |
61
|
|
|
if (isset($response['meta']['next_id']) && intval($response['meta']['next_id']) > $nextId) { |
62
|
|
|
$hasNextPage = true; |
63
|
|
|
$nextId = $response['meta']['next_id']; |
64
|
|
|
Log::debug(sprintf('Next ID is now %d.', $nextId)); |
65
|
|
|
} else { |
66
|
|
|
Log::debug('No next page.'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// store customers: |
70
|
|
|
foreach ($response['data'] as $accountArray) { |
71
|
|
|
$this->accounts[] = new Account($accountArray); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public function getAccounts(): array |
80
|
|
|
{ |
81
|
|
|
return $this->accounts; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param Login $login |
86
|
|
|
*/ |
87
|
|
|
public function setLogin(Login $login): void |
88
|
|
|
{ |
89
|
|
|
$this->login = $login; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
} |