Skip to content

Commit

Permalink
[Ohme] Improve import command (#11276)
Browse files Browse the repository at this point in the history
  • Loading branch information
Remg authored Jan 8, 2025
1 parent f11e2cc commit 2275e4c
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 60 deletions.
2 changes: 1 addition & 1 deletion config/packages/rate_limiter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ framework:

ohme_api_request:
policy: 'fixed_window'
limit: 100
limit: 90
interval: '1 minute'
35 changes: 32 additions & 3 deletions src/Ohme/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

class Client implements ClientInterface
{
private const PAGE_LIMIT = 100;

private readonly LimiterInterface $limiter;

public function __construct(
Expand Down Expand Up @@ -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,
]),
];

Expand Down
2 changes: 1 addition & 1 deletion src/Ohme/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
10 changes: 1 addition & 9 deletions src/Ohme/ContactHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
16 changes: 1 addition & 15 deletions src/Ohme/ContactImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
43 changes: 13 additions & 30 deletions src/Ohme/PaymentImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}

Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/Ohme/DummyClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 2275e4c

Please sign in to comment.