Skip to content

Commit

Permalink
Merge pull request #229 from Setono/delete-old-gc-command
Browse files Browse the repository at this point in the history
Add a command to disable old gift cards
  • Loading branch information
Roshyo authored May 13, 2022
2 parents 02d7eaf + 25841d2 commit 2e89a15
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 2 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"knplabs/knp-snappy-bundle": "^1.7",
"sylius/resource-bundle": "^1.6",
"symfony/config": "^4.4 || ^5.0",
"symfony/console": "^4.4 || ^5.0",
"symfony/dependency-injection": "^4.4 || ^5.0",
"symfony/event-dispatcher": "^4.4 || ^5.0",
"symfony/form": "^4.4 || ^5.0",
Expand All @@ -38,7 +39,6 @@
"phpunit/phpunit": "^9.5",
"psalm/plugin-phpunit": "^0.16.0",
"psalm/plugin-symfony": "^2.1",
"roave/security-advisories": "dev-latest",
"setono/code-quality-pack": "^2.1",
"setono/sylius-behat-pack": "^0.1",
"setono/sylius-catalog-promotion-plugin": "^0.2.0",
Expand Down Expand Up @@ -69,7 +69,8 @@
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": false,
"ergebnis/composer-normalize": true,
"symfony/thanks": true
"symfony/thanks": true,
"composer/package-versions-deprecated": true
},
"sort-packages": true
},
Expand Down
3 changes: 3 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<directory name="vendor"/>
</ignoreFiles>
</projectFiles>
<issueHandlers>
<PluginIssue name="QueryBuilderSetParameter" errorLevel="info"/>
</issueHandlers>
<plugins>
<pluginClass class="Psalm\SymfonyPsalmPlugin\Plugin">
<containerXml>tests/Application/var/cache/test/ApplicationTests_Setono_SyliusGiftCardPlugin_Application_KernelTestDebugContainer.xml</containerXml>
Expand Down
108 changes: 108 additions & 0 deletions src/Command/Cli/DisableOldGiftCardsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusGiftCardPlugin\Command\Cli;

use Doctrine\Persistence\ObjectManager;
use Setono\SyliusGiftCardPlugin\Repository\GiftCardRepositoryInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

final class DisableOldGiftCardsCommand extends Command
{
protected static $defaultName = 'setono:sylius-gift-card:disable-old-gift-cards';

private GiftCardRepositoryInterface $giftCardRepository;

private ObjectManager $giftCardManager;

public function __construct(
GiftCardRepositoryInterface $giftCardRepository,
ObjectManager $giftCardManager
) {
parent::__construct();

$this->giftCardRepository = $giftCardRepository;
$this->giftCardManager = $giftCardManager;
}

protected function configure(): void
{
$this->setDescription('Disable gift cards older than the provided date.');
$this->setHelp(
<<<'EOF'
The <info>%command.name%</info> command disables gift cards older than the provided date.
Date option accepts any date format accepted by PHP's <info>DateTime</info> class.
Such as "2020-01-01" or "2020-01-01 12:00:00" or "-3 years".
Period option accepts any period format accepted by PHP's <info>DateInterval</info> class.
Such as "P1Y" or "P1Y2M".
EOF
);
$this->addOption(
'date',
null,
InputOption::VALUE_OPTIONAL,
'The date to disable gift cards older than.',
null
);
$this->addOption(
'period',
null,
InputOption::VALUE_OPTIONAL,
'The period to disable gift cards older than.',
null
);
}

public function run(InputInterface $input, OutputInterface $output): int
{
$date = $input->getOption('date');
$period = $input->getOption('period');

if (null === $date && null === $period) {
$output->writeln('You must provide a date or a period.');

return 0;
}

if (null !== $date && null !== $period) {
$output->writeln('You must provide a date or a period, not both.');

return 0;
}

try {
if (null !== $date) {
$thresholdDate = new \DateTimeImmutable($date);
} else {
$thresholdDate = new \DateTimeImmutable('now');
$thresholdDate = $thresholdDate->sub(new \DateInterval($period));
}
} catch (\Exception $e) {
$output->writeln('The provided date or period is not valid.');

return 0;
}

$disabledGiftCardsAmount = 0;
do {
$giftCards = $this->giftCardRepository->findEnabledCreatedBefore($thresholdDate);
foreach ($giftCards as $giftCard) {
$giftCard->disable();
}

$this->giftCardManager->flush();

$disabledGiftCardsAmount += count($giftCards);
} while (count($giftCards) > 0);

$output->writeln(\sprintf('Disabled %d gift cards.', $disabledGiftCardsAmount));

return 1;
}
}
15 changes: 15 additions & 0 deletions src/Doctrine/ORM/GiftCardRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,19 @@ public function createAccountListQueryBuilder(CustomerInterface $customer): Quer

return $qb;
}

public function findEnabledCreatedBefore(\DateTimeInterface $date, ?int $limit = 100): array
{
$qb = $this->createQueryBuilder('gc');

$qb->andWhere('gc.createdAt < :date');
$qb->setParameter('date', $date);
$qb->andWhere('gc.enabled = true');

if (null !== $limit) {
$qb->setMaxResults($limit);
}

return $qb->getQuery()->getResult();
}
}
5 changes: 5 additions & 0 deletions src/Repository/GiftCardRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ public function findOneByOrderItemUnit(OrderItemUnitInterface $orderItemUnit): ?
public function findEnabled(): array;

public function createAccountListQueryBuilder(CustomerInterface $customer): QueryBuilder;

/**
* @return GiftCardInterface[]
*/
public function findEnabledCreatedBefore(\DateTimeInterface $date, ?int $limit = 100): array;
}
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<imports>
<import resource="services/applicator.xml"/>
<import resource="services/commands.xml"/>
<import resource="services/controller.xml"/>
<import resource="services/event_listener.xml"/>
<import resource="services/factory.xml"/>
Expand Down
16 changes: 16 additions & 0 deletions src/Resources/config/services/commands.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="setono_sylius_gift_card.command.cli.disable_old_gift_cards"
class="Setono\SyliusGiftCardPlugin\Command\Cli\DisableOldGiftCardsCommand">
<argument type="service" id="setono_sylius_gift_card.repository.gift_card" />
<argument type="service" id="setono_sylius_gift_card.manager.gift_card" />
<tag name="console.command"/>
</service>

</services>
</container>

0 comments on commit 2e89a15

Please sign in to comment.