1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ListCustomersRequest.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\Services\Spectre\Object\Customer; |
27
|
|
|
use Log; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Class ListCustomersRequest |
32
|
|
|
*/ |
33
|
|
|
class ListCustomersRequest extends SpectreRequest |
34
|
|
|
{ |
35
|
|
|
/** @var array */ |
36
|
|
|
private $customers = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* |
40
|
|
|
* @throws \FireflyIII\Exceptions\FireflyException |
41
|
|
|
* @throws \FireflyIII\Services\Spectre\Exception\SpectreException |
42
|
|
|
*/ |
43
|
|
|
public function call(): void |
44
|
|
|
{ |
45
|
|
|
$hasNextPage = true; |
46
|
|
|
$nextId = 0; |
47
|
|
|
while ($hasNextPage) { |
48
|
|
|
Log::debug(sprintf('Now calling ListCustomersRequest for next_id %d', $nextId)); |
49
|
|
|
$parameters = ['from_id' => $nextId]; |
50
|
|
|
$uri = '/api/v3/customers/?' . http_build_query($parameters); |
51
|
|
|
$response = $this->sendSignedSpectreGet($uri, []); |
52
|
|
|
|
53
|
|
|
// count entries: |
54
|
|
|
Log::debug(sprintf('Found %d entries in data-array', count($response['data']))); |
55
|
|
|
|
56
|
|
|
// extract next ID |
57
|
|
|
$hasNextPage = false; |
58
|
|
|
if (isset($response['meta']['next_id']) && intval($response['meta']['next_id']) > $nextId) { |
59
|
|
|
$hasNextPage = true; |
60
|
|
|
$nextId = $response['meta']['next_id']; |
61
|
|
|
Log::debug(sprintf('Next ID is now %d.', $nextId)); |
62
|
|
|
} else { |
63
|
|
|
Log::debug('No next page.'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// store customers: |
67
|
|
|
foreach ($response['data'] as $customerArray) { |
68
|
|
|
$this->customers[] = new Customer($customerArray); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
public function getCustomers(): array |
77
|
|
|
{ |
78
|
|
|
return $this->customers; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
} |