From 97c990be02b827464ef0c837c903a340e0d4284b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Gardien?= Date: Tue, 7 Jan 2025 16:46:59 +0100 Subject: [PATCH] [Ohme] Improve import command --- config/packages/rate_limiter.yaml | 2 +- src/Ohme/Client.php | 35 ++++++++++++++++++++++--- src/Ohme/ClientInterface.php | 2 +- src/Ohme/ContactHandler.php | 10 +------ src/Ohme/ContactImporter.php | 16 +----------- src/Ohme/PaymentImporter.php | 43 ++++++++++--------------------- tests/Ohme/DummyClient.php | 2 +- 7 files changed, 50 insertions(+), 60 deletions(-) diff --git a/config/packages/rate_limiter.yaml b/config/packages/rate_limiter.yaml index 1f652cf41cb..ba7a51f3b27 100644 --- a/config/packages/rate_limiter.yaml +++ b/config/packages/rate_limiter.yaml @@ -12,5 +12,5 @@ framework: ohme_api_request: policy: 'fixed_window' - limit: 100 + limit: 90 interval: '1 minute' diff --git a/src/Ohme/Client.php b/src/Ohme/Client.php index 209d7fd13a5..7d962d14796 100644 --- a/src/Ohme/Client.php +++ b/src/Ohme/Client.php @@ -8,6 +8,8 @@ class Client implements ClientInterface { + private const PAGE_LIMIT = 100; + private readonly LimiterInterface $limiter; public function __construct( @@ -38,12 +40,39 @@ public function getContacts(int $limit = 100, int $offset = 0, array $options = return $this->request('GET', 'contacts', $options); } - public function getPayments(int $limit = 100, int $offset = 0, array $options = []): array + public function getPayments(array $options = []): array + { + $page = 1; + $firstPage = $this->getPaymentsPage(1, $options); + + $payments = $firstPage['data'] ?? []; + $totalPayments = $firstPage['count'] ?? 0; + + if ($totalPayments > self::PAGE_LIMIT) { + do { + ++$page; + + $paymentsPage = $this->getPaymentsPage($page, $options); + + $payments = array_merge( + $payments, + $paymentsPage['data'] ?? [] + ); + } while (($page * self::PAGE_LIMIT) < $totalPayments); + } + + return [ + 'count' => $totalPayments, + 'data' => $payments, + ]; + } + + private function getPaymentsPage(int $page = 1, array $options = []): array { $options = [ 'query' => array_merge($options, [ - 'limit' => $limit, - 'offset' => $offset, + 'limit' => self::PAGE_LIMIT, + 'offset' => ($page * self::PAGE_LIMIT) - self::PAGE_LIMIT, ]), ]; diff --git a/src/Ohme/ClientInterface.php b/src/Ohme/ClientInterface.php index 22c2704ced0..48ec38dc424 100644 --- a/src/Ohme/ClientInterface.php +++ b/src/Ohme/ClientInterface.php @@ -8,5 +8,5 @@ public function updateContact(string $contactId, array $data): array; public function getContacts(int $limit = 100, int $offset = 0, array $options = []): array; - public function getPayments(int $limit = 100, int $offset = 0, array $options = []): array; + public function getPayments(array $options = []): array; } diff --git a/src/Ohme/ContactHandler.php b/src/Ohme/ContactHandler.php index 5811c3fe7e9..5d4b2bd1e0b 100644 --- a/src/Ohme/ContactHandler.php +++ b/src/Ohme/ContactHandler.php @@ -24,14 +24,6 @@ public function updateAdherentLink(Contact $contact): void return; } - $totalPayments = $this->paymentImporter->getPaymentsCount([], $contact); - $pageSize = 100; - $offset = 0; - - do { - $this->paymentImporter->importPayments($pageSize, $offset, [], $contact); - - $offset += $pageSize; - } while ($offset < $totalPayments); + $this->paymentImporter->importPayments($contact); } } diff --git a/src/Ohme/ContactImporter.php b/src/Ohme/ContactImporter.php index 44fec173f1c..72a03a033ae 100644 --- a/src/Ohme/ContactImporter.php +++ b/src/Ohme/ContactImporter.php @@ -42,21 +42,7 @@ public function importContacts(int $limit = 100, int $offset = 0, array $options $this->updateContact($contact, $contactData); - $totalPayments = $this->paymentImporter->getPaymentsCount([], $contact); - - if ($totalPayments !== $contact->paymentCount) { - $contact->paymentCount = $totalPayments; - - $this->contactRepository->save($contact); - } - - $pageSize = 100; - $offset = 0; - do { - $this->paymentImporter->importPayments($pageSize, $offset, [], $contact); - - $offset += $pageSize; - } while ($offset < $totalPayments); + $this->paymentImporter->importPayments($contact); } } diff --git a/src/Ohme/PaymentImporter.php b/src/Ohme/PaymentImporter.php index f668b756902..e63ce4d10e7 100644 --- a/src/Ohme/PaymentImporter.php +++ b/src/Ohme/PaymentImporter.php @@ -16,25 +16,19 @@ public function __construct( ) { } - public function getPaymentsCount(array $options = [], ?Contact $contact = null): int + public function importPayments(Contact $contact, array $options = []): void { - if ($contact) { - $options['contact_id'] = $contact->ohmeIdentifier; - } + $options['contact_id'] = $contact->ohmeIdentifier; - $payments = $this->client->getPayments(1, 0, $options); + $payments = $this->client->getPayments($options); - return $payments['count'] ?? 0; - } + $totalPayments = $payments['count'] ?? 0; + if ($totalPayments !== $contact->paymentCount) { + $contact->paymentCount = $totalPayments; - public function importPayments(int $limit = 100, int $offset = 0, array $options = [], ?Contact $contact = null): void - { - if ($contact) { - $options['contact_id'] = $contact->ohmeIdentifier; + $this->contactRepository->save($contact); } - $payments = $this->client->getPayments($limit, $offset, $options); - if (empty($payments['data']) || !is_iterable($payments['data'])) { return; } @@ -44,14 +38,8 @@ public function importPayments(int $limit = 100, int $offset = 0, array $options continue; } - $currentContact = $contact; - - if (!$currentContact) { - $currentContact = $this->findContact((string) $paymentData['contact_id']); - } - // Do not retrieve payments that can't be associated to an adherent - if (!$currentContact || !$currentContact->adherent) { + if (!$contact->adherent) { continue; } @@ -62,26 +50,21 @@ public function importPayments(int $limit = 100, int $offset = 0, array $options $identifier = (string) $paymentData['id']; $payment = $this->findPayment($identifier) ?? $this->createPayment($identifier); - $payment->adherent = $currentContact->adherent; + $payment->adherent = $contact->adherent; $this->updatePayment($payment, $paymentData); if ( - !$currentContact->lastPaymentDate - || $currentContact->lastPaymentDate < $payment->date + !$contact->lastPaymentDate + || $contact->lastPaymentDate < $payment->date ) { - $currentContact->lastPaymentDate = $payment->date; + $contact->lastPaymentDate = $payment->date; - $this->contactRepository->save($currentContact); + $this->contactRepository->save($contact); } } } - private function findContact(string $identifier): ?Contact - { - return $this->contactRepository->findOneByOhmeIdentifier($identifier); - } - private function findPayment(string $identifier): ?Payment { return $this->paymentRepository->findOneByOhmeIdentifier($identifier); diff --git a/tests/Ohme/DummyClient.php b/tests/Ohme/DummyClient.php index a237a223a6b..c251f89e57d 100644 --- a/tests/Ohme/DummyClient.php +++ b/tests/Ohme/DummyClient.php @@ -27,7 +27,7 @@ public function getContacts(int $limit = 100, int $offset = 0, array $options = ]; } - public function getPayments(int $limit = 100, int $offset = 0, array $options = []): array + public function getPayments(array $options = []): array { return [ 'status' => 200,