|
3 | 3 | namespace App\Callbacks\Resolution; |
4 | 4 |
|
5 | 5 | use App\Callbacks\Watching\CallbackPollingException; |
| 6 | +use App\Quickpay\Pagination\LinkHeaderParser; |
| 7 | +use App\Quickpay\Pagination\PaginationTargetCanonicalizer; |
6 | 8 | use App\Quickpay\QuickpayClient; |
7 | 9 | use App\Quickpay\QuickpayResponse; |
| 10 | +use DateTimeImmutable; |
| 11 | +use DateTimeZone; |
| 12 | +use InvalidArgumentException; |
8 | 13 | use UnexpectedValueException; |
9 | 14 |
|
10 | 15 | /** |
|
19 | 24 | { |
20 | 25 | public function __construct(private QuickpayClient $quickpay) {} |
21 | 26 |
|
| 27 | + /** @return array<int, QuickpayResponse> */ |
| 28 | + public function changedBetween(DateTimeImmutable $minimum, DateTimeImmutable $maximum): array |
| 29 | + { |
| 30 | + $utc = new DateTimeZone('UTC'); |
| 31 | + $query = [ |
| 32 | + 'timestamp' => 'updated_at', |
| 33 | + 'min_time' => $minimum->setTimezone($utc)->format('Y-m-d H:i:s O'), |
| 34 | + 'max_time' => $maximum->setTimezone($utc)->format('Y-m-d H:i:s O'), |
| 35 | + 'operations_size' => 0, |
| 36 | + 'page_size' => 100, |
| 37 | + ]; |
| 38 | + $response = $this->changedPage('/payments', $query); |
| 39 | + $pageCount = 1; |
| 40 | + $seen = [PaginationTargetCanonicalizer::fromQuery('/payments', $query) => true]; |
| 41 | + |
| 42 | + $ids = []; |
| 43 | + |
| 44 | + while (true) { |
| 45 | + foreach ($response->json as $payment) { |
| 46 | + $id = is_array($payment) && ! array_is_list($payment) ? ($payment['id'] ?? null) : null; |
| 47 | + |
| 48 | + if ((! is_int($id) && ! is_string($id)) || (string) $id === '') { |
| 49 | + throw new UnexpectedValueException('Quickpay returned a changed-payment row without a valid payment ID.'); |
| 50 | + } |
| 51 | + |
| 52 | + $ids[(string) $id] = true; |
| 53 | + } |
| 54 | + |
| 55 | + $next = LinkHeaderParser::next($response->header('Link')); |
| 56 | + |
| 57 | + if ($next === null) { |
| 58 | + break; |
| 59 | + } |
| 60 | + |
| 61 | + if ($pageCount >= 100) { |
| 62 | + throw new InvalidArgumentException('Pagination exceeded the configured maximum of 100 pages.'); |
| 63 | + } |
| 64 | + |
| 65 | + $canonicalNext = PaginationTargetCanonicalizer::canonical($next); |
| 66 | + |
| 67 | + if (isset($seen[$canonicalNext])) { |
| 68 | + throw new InvalidArgumentException('Quickpay returned a pagination cycle.'); |
| 69 | + } |
| 70 | + |
| 71 | + $seen[$canonicalNext] = true; |
| 72 | + $response = $this->changedPage($next); |
| 73 | + $pageCount++; |
| 74 | + } |
| 75 | + |
| 76 | + return array_map($this->byId(...), array_keys($ids)); |
| 77 | + } |
| 78 | + |
| 79 | + /** @param array<string, mixed> $query */ |
| 80 | + private function changedPage(string $path, array $query = []): QuickpayResponse |
| 81 | + { |
| 82 | + $response = $this->quickpay->get($path, $query); |
| 83 | + |
| 84 | + if (! $response->successful()) { |
| 85 | + throw $this->pollingFailure($response); |
| 86 | + } |
| 87 | + |
| 88 | + if (! is_array($response->json) || ! array_is_list($response->json)) { |
| 89 | + throw new UnexpectedValueException('Quickpay returned a malformed changed-payment response.'); |
| 90 | + } |
| 91 | + |
| 92 | + return $response; |
| 93 | + } |
| 94 | + |
22 | 95 | public function byId(string $paymentId): QuickpayResponse |
23 | 96 | { |
24 | 97 | $response = $this->quickpay->get('/payments/'.rawurlencode($paymentId)); |
|
0 commit comments