Skip to content

Commit

Permalink
Add migration command for new descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
pingiun committed Nov 17, 2024
1 parent 5c29697 commit 696ac58
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 21 deletions.
87 changes: 87 additions & 0 deletions src/Command/MigrateSubscriptionToNewMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types = 1);

namespace App\Command;

use App\Entity\Member;
use App\Service\SubscriptionSetupService;
use Doctrine\ORM\EntityManagerInterface;
use Mollie\Api\Exceptions\ApiException;
use Mollie\Api\MollieApiClient;
use Mollie\Api\Resources\Subscription;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;

class MigrateSubscriptionToNewMessage extends Command
{
protected static $defaultName = 'app:migrate-subscription-to-new-message';
protected static $defaultDescription = 'Update all Mollie subscriptions to use the currently configured payment description';

public function __construct(
private readonly MollieApiClient $mollieApiClient,
private readonly EntityManagerInterface $entityManager,
private readonly SubscriptionSetupService $subscriptionService,
)
{
parent::__construct();
}

public function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln('Updating Mollie subscription to new message');

try {
$this->confirmChangeCount($input, $output);
} catch (\Exception $e) {
$output->writeln($e->getMessage());
return Command::FAILURE;
}

$memberRepository = $this->entityManager->getRepository(Member::class);

$allSubscriptions = $this->mollieApiClient->subscriptions->iterator();
foreach ($allSubscriptions as $subscription) {
/** @var Subscription $subscription */
if (!$subscription->isActive()) {
continue;
}
$output->writeln('Updating subscription ' . $subscription->description);
/** @var Member $member */
$members = $memberRepository->findBy(['mollieSubscriptionId' => $subscription->id]);
if (count($members) === 0) {
$output->writeln('<comment>Could not find member for subscription ' . $subscription->id . '! (skipping)</comment>');
continue;
}
$member = $members[0];
$newDescription = $this->subscriptionService->generateDescription($member);
$subscription->description = $newDescription;
try {
$subscription->update();
} catch (ApiException $exception) {
$output->writeln('<error>' . $exception->getMessage() . '</error>');
}
}

return Command::SUCCESS;
}

/**
* @throws \Exception
*/
private function confirmChangeCount(InputInterface $input, OutputInterface $output): void
{
$allSubscriptions = $this->mollieApiClient->subscriptions->iterator();
$amount = $allSubscriptions->count();
$helper = $this->getHelper('question');
$question = new Question("You are about to change $amount descriptions, to continue confirm the amount: ", false);
$answer = (int)$helper->ask($input, $output, $question);
if ($answer === $amount) {
return;
}
throw new \Exception('Did not confirm');
}
}
15 changes: 0 additions & 15 deletions src/Controller/Admin/MembershipApplicationCrud.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,6 @@ public function acceptApplication(AdminContext $context)
/** @var MembershipApplication $application */
$application = $context->getEntity()->getInstance();

$mollieIntervals = [
Member::PERIOD_MONTHLY => '1 month',
Member::PERIOD_QUARTERLY => '3 months',
Member::PERIOD_ANNUALLY => '1 year'
];
$dateTimeIntervals = [
Member::PERIOD_MONTHLY => 'P1M',
Member::PERIOD_QUARTERLY => 'P3M',
Member::PERIOD_ANNUALLY => 'P1Y'
];

$startDate = new DateTime();
$startDate->setDate(date('Y'), floor(date('m') / 3) * 3, 1);
$startDate->add(new DateInterval($dateTimeIntervals[$application->getContributionPeriod()]));

$member = $application->createMember(null);
$member->generateNewPasswordToken();
$em = $this->getDoctrine()->getManager();
Expand Down
17 changes: 11 additions & 6 deletions src/Service/SubscriptionSetupService.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ public function __construct(
{
}

public function generateDescription(Member $member): string
{
$projectRoot = $this->params->get('kernel.project_dir');
$org_config = Yaml::parseFile($projectRoot . '/config/instances/' . $this->params->get('app.organizationID') . '.yaml');

$description = $org_config['mollie_payment_description'];
$description = str_replace('{{organisation_name}}', $this->params->get("app.organizationName"), $description);
return str_replace('{{member_number}}', (string)$member->getId(), $description);
}

/**
* @throws ApiException
*/
Expand All @@ -44,12 +54,7 @@ public function createSubscription(Member $member, Customer $customer): Subscrip
$startDate->setDate((int)date('Y'), (int)floor(date('m') / 3) * 3, 1);
$startDate->add(new \DateInterval($dateTimeIntervals[$member->getContributionPeriod()]));

$projectRoot = $this->params->get('kernel.project_dir');
$org_config = Yaml::parseFile($projectRoot . '/config/instances/' . $this->params->get('app.organizationID') . '.yaml');

$description = $org_config['mollie_payment_description'];
$description = str_replace('{{organisation_name}}', $this->params->get("app.organizationName"), $description);
$description = str_replace('{{member_number}}', (string)$member->getId(), $description);
$description = $this->generateDescription($member);

$subscription = $customer->createSubscription([
'amount' => [
Expand Down

0 comments on commit 696ac58

Please sign in to comment.