-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from Setono/47-prune
Add prune command
- Loading branch information
Showing
15 changed files
with
167 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ | |
|
||
/behat.yml | ||
/phpspec.yml | ||
/.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusTrustpilotPlugin\Command; | ||
|
||
use Setono\SyliusTrustpilotPlugin\Pruner\PrunerInterface; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class PruneCommand extends Command | ||
{ | ||
protected static $defaultName = 'setono:sylius-trustpilot:prune'; | ||
|
||
/** @var string|null */ | ||
protected static $defaultDescription = 'Will prune the invitations table'; | ||
|
||
private PrunerInterface $pruner; | ||
|
||
public function __construct(PrunerInterface $pruner) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->pruner = $pruner; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$this->pruner->prune(); | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusTrustpilotPlugin\Pruner; | ||
|
||
use DateTimeImmutable; | ||
use Setono\SyliusTrustpilotPlugin\Repository\InvitationRepositoryInterface; | ||
|
||
final class Pruner implements PrunerInterface | ||
{ | ||
private InvitationRepositoryInterface $invitationRepository; | ||
|
||
private int $pruneOlderThan; | ||
|
||
public function __construct(InvitationRepositoryInterface $invitationRepository, int $pruneOlderThan) | ||
{ | ||
$this->invitationRepository = $invitationRepository; | ||
$this->pruneOlderThan = $pruneOlderThan; | ||
} | ||
|
||
public function prune(): void | ||
{ | ||
$this->invitationRepository->removeOlderThan( | ||
new DateTimeImmutable(sprintf('-%d minutes', $this->pruneOlderThan)) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusTrustpilotPlugin\Pruner; | ||
|
||
interface PrunerInterface | ||
{ | ||
/** | ||
* Will prune invitations table | ||
*/ | ||
public function prune(): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="http://symfony.com/schema/dic/services" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<service id="setono_sylius_trustpilot.pruner.default" | ||
class="Setono\SyliusTrustpilotPlugin\Pruner\Pruner"> | ||
<argument type="service" id="setono_sylius_trustpilot.repository.invitation"/> | ||
<argument>%setono_sylius_trustpilot.prune_older_than%</argument> | ||
</service> | ||
</services> | ||
</container> |
3 changes: 3 additions & 0 deletions
3
tests/Application/config/packages/setono_sylius_trustpilot.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
imports: | ||
- { resource: "@SetonoSyliusTrustpilotPlugin/Resources/config/app/config.yaml" } | ||
|
||
setono_sylius_trustpilot: | ||
prune_older_than: 7200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Setono\SyliusTrustpilotPlugin\Pruner; | ||
|
||
use DateInterval; | ||
use DateTimeImmutable; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Argument; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
use Setono\SyliusTrustpilotPlugin\Pruner\Pruner; | ||
use Setono\SyliusTrustpilotPlugin\Repository\InvitationRepositoryInterface; | ||
|
||
/** | ||
* @covers \Setono\SyliusTrustpilotPlugin\Pruner\Pruner | ||
*/ | ||
final class PrunerTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_prunes(): void | ||
{ | ||
$threshold = 43_200; // 30 days | ||
$thresholdDateTime = (new DateTimeImmutable())->sub(new DateInterval(sprintf('PT%dM', $threshold))); | ||
self::assertNotFalse($thresholdDateTime); | ||
|
||
$repository = $this->prophesize(InvitationRepositoryInterface::class); | ||
$repository->removeOlderThan( | ||
Argument::which('getTimestamp', $thresholdDateTime->getTimestamp()) | ||
)->shouldBeCalled(); | ||
|
||
$pruner = new Pruner($repository->reveal(), $threshold); | ||
$pruner->prune(); | ||
} | ||
} |