Skip to content

Commit

Permalink
[Ohme] Refactor import (#11370)
Browse files Browse the repository at this point in the history
* [Ohme] Refactor import

* Fix dummy client

* Simplify page size

* Return processed count
  • Loading branch information
Remg authored Jan 28, 2025
1 parent b87da0d commit b038d84
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 57 deletions.
43 changes: 38 additions & 5 deletions src/Command/OhmeImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Command;

use App\Ohme\ContactImporter;
use App\Ohme\PaymentImporter;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -23,6 +24,7 @@ class OhmeImportCommand extends Command

public function __construct(
private readonly ContactImporter $contactImporter,
private readonly PaymentImporter $paymentImporter,
) {
parent::__construct();
}
Expand All @@ -34,6 +36,7 @@ protected function configure(): void
->addOption('contact-ohme-id', null, InputOption::VALUE_REQUIRED)
->addOption('contact-email', null, InputOption::VALUE_REQUIRED)
->addOption('contact-uuid-adherent', null, InputOption::VALUE_REQUIRED)
->addOption('with-payments', null, InputOption::VALUE_NONE)
;
}

Expand All @@ -51,6 +54,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$input->getOption('contact-uuid-adherent')
);

if ($input->getOption('with-payments')) {
$this->importPayments();
}

return self::SUCCESS;
}

Expand Down Expand Up @@ -85,16 +92,42 @@ private function importContacts(
$offset = 0;

do {
$currentPageSize = min($pageSize, $total - $offset);

$this->contactImporter->importContacts($currentPageSize, $offset, $filters);
$contactsCount = $this->contactImporter->importContacts($pageSize, $offset, $filters);

$this->io->progressAdvance($currentPageSize);
$this->io->progressAdvance($contactsCount);

$offset += $currentPageSize;
$offset += $contactsCount;
} while ($offset < $total);

$this->io->progressFinish();
$this->io->success("$offset contacts handled successfully.");
}

private function importPayments(): void
{
$total = $this->paymentImporter->getPaymentsCount();

if (0 === $total) {
$this->io->text('No payment to import.');

return;
}

$this->io->section('Importing payments');
$this->io->progressStart($total);

$pageSize = $total ? min($total, self::MAX_PAGE_SIZE) : self::MAX_PAGE_SIZE;
$offset = 0;

do {
$paymentsCount = $this->paymentImporter->importPayments($pageSize, $offset);

$this->io->progressAdvance($paymentsCount);

$offset += $pageSize;
} while ($offset < $total);

$this->io->progressFinish();
$this->io->success("$offset payments handled successfully.");
}
}
5 changes: 5 additions & 0 deletions src/Entity/Ohme/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,9 @@ public function __toString(): string
$this->lastname,
]));
}

public function incrementPaymentCount(): void
{
++$this->paymentCount;
}
}
35 changes: 3 additions & 32 deletions src/Ohme/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

class Client implements ClientInterface
{
private const PAGE_LIMIT = 100;

private readonly LimiterInterface $limiter;

public function __construct(
Expand Down Expand Up @@ -40,39 +38,12 @@ public function getContacts(int $limit = 100, int $offset = 0, array $options =
return $this->request('GET', 'contacts', $options);
}

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
public function getPayments(int $limit = 100, int $offset = 0, array $options = []): array
{
$options = [
'query' => array_merge($options, [
'limit' => self::PAGE_LIMIT,
'offset' => ($page * self::PAGE_LIMIT) - self::PAGE_LIMIT,
'limit' => $limit,
'offset' => $offset,
]),
];

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(array $options = []): array;
public function getPayments(int $limit = 100, int $offset = 0, array $options = []): array;
}
13 changes: 8 additions & 5 deletions src/Ohme/ContactImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public function __construct(
private readonly ClientInterface $client,
private readonly ContactRepository $contactRepository,
private readonly AdherentRepository $adherentRepository,
private readonly PaymentImporter $paymentImporter,
) {
}

Expand All @@ -23,15 +22,19 @@ public function getContactsCount(array $options = []): int
return $contacts['count'] ?? 0;
}

public function importContacts(int $limit = 100, int $offset = 0, array $options = []): void
public function importContacts(int $limit = 100, int $offset = 0, array $options = []): int
{
$contacts = $this->client->getContacts($limit, $offset, $options);

if (empty($contacts['data']) || !is_iterable($contacts['data'])) {
return;
return 0;
}

$total = 0;

foreach ($contacts['data'] as $contactData) {
++$total;

if (empty($contactData['id'])) {
continue;
}
Expand All @@ -41,9 +44,9 @@ public function importContacts(int $limit = 100, int $offset = 0, array $options
$contact = $this->findContact($identifier) ?? $this->createContact($identifier);

$this->updateContact($contact, $contactData);

$this->paymentImporter->importPayments($contact);
}

return $total;
}

private function findContact(string $ohmeIdentifier): ?Contact
Expand Down
40 changes: 27 additions & 13 deletions src/Ohme/PaymentImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Ohme;

use App\Entity\Contribution\Payment;
use App\Entity\Ohme\Contact;
use App\Repository\Contribution\PaymentRepository;
use App\Repository\Ohme\ContactRepository;

Expand All @@ -16,30 +15,34 @@ public function __construct(
) {
}

public function importPayments(Contact $contact, array $options = []): void
public function getPaymentsCount(): int
{
$options['contact_id'] = $contact->ohmeIdentifier;
$payments = $this->client->getPayments();

$payments = $this->client->getPayments($options);

$totalPayments = $payments['count'] ?? 0;
if ($totalPayments !== $contact->paymentCount) {
$contact->paymentCount = $totalPayments;
return $payments['count'] ?? 0;
}

$this->contactRepository->save($contact);
}
public function importPayments(int $limit = 100, int $offset = 0, array $options = []): int
{
$payments = $this->client->getPayments($limit, $offset, $options);

if (empty($payments['data']) || !is_iterable($payments['data'])) {
return;
return 0;
}

$total = 0;

foreach ($payments['data'] as $paymentData) {
++$total;

if (empty($paymentData['contact_id'])) {
continue;
}

$contact = $this->contactRepository->findOneByOhmeIdentifier($paymentData['contact_id']);

// Do not retrieve payments that can't be associated to an adherent
if (!$contact->adherent) {
if (!$contact || !$contact->adherent) {
continue;
}

Expand All @@ -49,7 +52,16 @@ public function importPayments(Contact $contact, array $options = []): void

$identifier = (string) $paymentData['id'];

$payment = $this->findPayment($identifier) ?? $this->createPayment($identifier);
$payment = $this->findPayment($identifier);

if (!$payment) {
$payment = $this->createPayment($identifier);

$contact->incrementPaymentCount();

$this->contactRepository->save($contact);
}

$payment->adherent = $contact->adherent;

$this->updatePayment($payment, $paymentData);
Expand All @@ -63,6 +75,8 @@ public function importPayments(Contact $contact, array $options = []): void
$this->contactRepository->save($contact);
}
}

return $total;
}

private function findPayment(string $identifier): ?Payment
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(array $options = []): array
public function getPayments(int $limit = 100, int $offset = 0, array $options = []): array
{
return [
'status' => 200,
Expand Down

0 comments on commit b038d84

Please sign in to comment.