Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Ohme] Improve import command #11276

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading