-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
275 additions
and
6 deletions.
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
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,9 @@ | ||
<?php | ||
|
||
namespace Sbooker\CommandBus; | ||
interface CleanStorage | ||
{ | ||
public function cleanSuccessCommands(\DateTimeImmutable $before): void; | ||
|
||
public function cleanFailedCommands(\DateTimeImmutable $before): 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Sbooker\CommandBus; | ||
|
||
interface CommandBusCleaner | ||
{ | ||
public function clean(?\DateTimeImmutable $successBefore = null, ?\DateTimeImmutable $failedBefore = null): 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Sbooker\CommandBus\Infrastructure\Console; | ||
|
||
use Sbooker\CommandBus\CommandBusCleaner; | ||
use Sbooker\Console\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
final class Clean extends Command | ||
{ | ||
private CommandBusCleaner $cleaner; | ||
|
||
public function __construct(CommandBusCleaner $cleaner) | ||
{ | ||
parent::__construct(); | ||
$this->cleaner = $cleaner; | ||
} | ||
|
||
protected function configure() | ||
{ | ||
$this->setDescription('Clean used commands in bus.'); | ||
$this->addArgument('success', InputArgument::OPTIONAL, "DateTime <https://www.php.net/manual/en/datetime.formats.php> to which all SUCCESS commands will be deleted"); | ||
$this->addArgument('failed', InputArgument::OPTIONAL, "DateTime <https://www.php.net/manual/en/datetime.formats.php> to which all FAILED commands will be deleted"); | ||
} | ||
|
||
protected function doExecute(InputInterface $input, OutputInterface $output): void | ||
{ | ||
$this->cleaner->clean($this->resolveArgument($input, 'success'), $this->resolveArgument($input, 'failed')); | ||
} | ||
|
||
private function resolveArgument(InputInterface $input, string $name): ?\DateTimeImmutable | ||
{ | ||
$argument = $input->getArgument($name); | ||
|
||
return (null === $argument) ? null : new \DateTimeImmutable($argument); | ||
} | ||
} |
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,38 @@ | ||
<?php | ||
|
||
namespace Sbooker\CommandBus; | ||
|
||
final class PersistentCommandBusCleaner implements CommandBusCleaner | ||
{ | ||
private CleanStorage $storage; | ||
private ?\DateTimeImmutable $successBefore; | ||
private ?\DateTimeImmutable $failedBefore; | ||
|
||
public function __construct(CleanStorage $storage, ?\DateTimeImmutable $successBefore, ?\DateTimeImmutable $failedBefore) | ||
{ | ||
$this->storage = $storage; | ||
$this->successBefore = $successBefore; | ||
$this->failedBefore = $failedBefore; | ||
} | ||
|
||
public function clean(?\DateTimeImmutable $successBefore = null, ?\DateTimeImmutable $failedBefore = null): void | ||
{ | ||
$successBefore = $this->selectFrom($successBefore, $this->successBefore); | ||
if (null !== $successBefore) { | ||
$this->storage->cleanSuccessCommands($successBefore); | ||
} | ||
$failedBefore = $this->selectFrom($failedBefore, $this->failedBefore); | ||
if (null !== $failedBefore) { | ||
$this->storage->cleanFailedCommands($failedBefore); | ||
} | ||
} | ||
|
||
private function selectFrom(?\DateTimeImmutable $first, ?\DateTimeImmutable $second):?\DateTimeImmutable | ||
{ | ||
if (null !== $first) { | ||
return $first; | ||
} | ||
|
||
return $second; | ||
} | ||
} |
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,88 @@ | ||
<?php | ||
|
||
namespace Sbooker\CommandBus\Tests; | ||
|
||
use Sbooker\CommandBus\CleanStorage; | ||
use Sbooker\CommandBus\CommandBusCleaner; | ||
use Sbooker\CommandBus\PersistentCommandBusCleaner; | ||
|
||
final class PersistentCommandBusCleanerTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider examples | ||
*/ | ||
public function test(?string $inputSuccess, ?string $configuredSuccess, ?string $inputFail, ?string $configuredFail, ?string $expectedSuccess, ?string $expectedFail) | ||
{ | ||
$cleaner = $this->createCleaner( | ||
$this->createStorage($expectedSuccess, $expectedFail), | ||
$configuredSuccess, | ||
$configuredFail | ||
); | ||
|
||
$cleaner->clean($this->makeDateTime($inputSuccess), $this->makeDateTime($inputFail)); | ||
} | ||
|
||
public static function examples(): array | ||
{ | ||
return [ | ||
'all empty' => [ null, null, null, null, null, null, ], | ||
'input success only' => [ '2024-01-01 00:00:00', null, null, null, '2024-01-01 00:00:00', null ], | ||
'configured success only' => [ null, '2024-01-01 00:00:00', null, null, '2024-01-01 00:00:00', null ], | ||
'both success only' => [ '2024-01-01 00:00:00', '2023-01-01 00:00:00', null, null, '2024-01-01 00:00:00', null ], | ||
'input failed only' => [ null, null, '2024-01-01 00:00:00', null, null, '2024-01-01 00:00:00' ], | ||
'both input' => [ '2024-01-01 00:00:00', null, '2023-01-01 00:00:00', null, '2024-01-01 00:00:00', '2023-01-01 00:00:00' ], | ||
'configured success input failed' =>[ null, '2024-01-01 00:00:00', '2023-01-01 00:00:00', null, '2024-01-01 00:00:00', '2023-01-01 00:00:00' ], | ||
'both success only input failed' => [ '2024-01-01 00:00:00', '2023-01-01 00:00:00', '2022-01-01 00:00:00', null, '2024-01-01 00:00:00', '2022-01-01 00:00:00', ], | ||
'configured failed only' => [ null, null, null, '2024-01-01 00:00:00', null, '2024-01-01 00:00:00' ], | ||
'input success configured failed' => [ '2024-01-01 00:00:00', null, null, '2021-01-01 00:00:00', '2024-01-01 00:00:00', '2021-01-01 00:00:00' ], | ||
'configured success configured failed' => [ null, '2024-01-01 00:00:00', null, '2021-01-01 00:00:00', '2024-01-01 00:00:00', '2021-01-01 00:00:00' ], | ||
'both success configured failed' => [ '2024-01-01 00:00:00', '2023-01-01 00:00:00', null, '2021-01-01 00:00:00', '2024-01-01 00:00:00', '2021-01-01 00:00:00' ], | ||
'both failed only' => [ null, null, '2024-01-01 00:00:00', '2023-01-01 00:00:00', null, '2024-01-01 00:00:00', ], | ||
'input success both failed' => [ '2025-01-01 00:00:00', null, '2024-01-01 00:00:00', '2023-01-01 00:00:00', '2025-01-01 00:00:00', '2024-01-01 00:00:00', ], | ||
'configured success both failed' => [ null, '2025-01-01 00:00:00', '2024-01-01 00:00:00', '2023-01-01 00:00:00', '2025-01-01 00:00:00', '2024-01-01 00:00:00', ], | ||
'both success & failed' => [ '2026-01-01 00:00:00', '2025-01-01 00:00:00', '2024-01-01 00:00:00', '2023-01-01 00:00:00', '2026-01-01 00:00:00', '2024-01-01 00:00:00', ], | ||
]; | ||
} | ||
|
||
private function createCleaner(CleanStorage $storage, ?string $configuredSuccess, ?string $configuredFail): CommandBusCleaner | ||
{ | ||
return new PersistentCommandBusCleaner( | ||
$storage, | ||
$this->makeDateTime($configuredSuccess), | ||
$this->makeDateTime($configuredFail), | ||
); | ||
} | ||
|
||
private function createStorage(?string $expectedSuccess, ?string $expectedFail): CleanStorage | ||
{ | ||
$mock = $this->createMock(CleanStorage::class); | ||
$mock | ||
->expects($this->exactly($this->resolveCount($expectedSuccess))) | ||
->method('cleanSuccessCommands') | ||
->with($this->makeDateTime($expectedSuccess)); | ||
$mock | ||
->expects($this->exactly($this->resolveCount($expectedFail))) | ||
->method('cleanFailedCommands') | ||
->with($this->makeDateTime($expectedFail)); | ||
|
||
return $mock; | ||
} | ||
|
||
private function makeDateTime(?string $dateTime): ?\DateTimeImmutable | ||
{ | ||
if (null === $dateTime) { | ||
return null; | ||
} | ||
|
||
return new \DateTimeImmutable($dateTime); | ||
} | ||
|
||
private function resolveCount(?string $dateTime): int | ||
{ | ||
if (null === $dateTime) { | ||
return 0; | ||
} | ||
|
||
return 1; | ||
} | ||
} |