-
Notifications
You must be signed in to change notification settings - Fork 2
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
5 changed files
with
129 additions
and
3 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
src/ConsoleCommands/GrantMembershipToPlayerConsoleCommands.php
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,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SpeedPuzzling\Web\ConsoleCommands; | ||
|
||
use DateTimeImmutable; | ||
use SpeedPuzzling\Web\Message\GrantMembership; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
|
||
#[AsCommand('myspeedpuzzling:membership:grant')] | ||
final class GrantMembershipToPlayerConsoleCommands extends Command | ||
{ | ||
public function __construct( | ||
readonly private MessageBusInterface $messageBus, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
parent::configure(); | ||
|
||
$this->addArgument('playerId', InputArgument::REQUIRED); | ||
$this->addArgument('endsAt', InputArgument::REQUIRED); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
/** @var string $playerId */ | ||
$playerId = $input->getArgument('playerId'); | ||
|
||
/** @var string $endsAt */ | ||
$endsAt = $input->getArgument('endsAt'); | ||
|
||
$this->messageBus->dispatch( | ||
new GrantMembership( | ||
playerId: $playerId, | ||
endsAt: new DateTimeImmutable($endsAt), | ||
), | ||
); | ||
|
||
return self::SUCCESS; | ||
} | ||
} |
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 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SpeedPuzzling\Web\Exceptions; | ||
|
||
final class PlayerAlreadyHaveMembership extends \Exception | ||
{ | ||
} |
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SpeedPuzzling\Web\Message; | ||
|
||
use DateTimeImmutable; | ||
|
||
readonly final class GrantMembership | ||
{ | ||
public function __construct( | ||
public string $playerId, | ||
public DateTimeImmutable $endsAt, | ||
) { | ||
} | ||
} |
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,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SpeedPuzzling\Web\MessageHandler; | ||
|
||
use Psr\Clock\ClockInterface; | ||
use Ramsey\Uuid\Uuid; | ||
use SpeedPuzzling\Web\Entity\Membership; | ||
use SpeedPuzzling\Web\Exceptions\MembershipNotFound; | ||
use SpeedPuzzling\Web\Exceptions\PlayerAlreadyHaveMembership; | ||
use SpeedPuzzling\Web\Exceptions\PlayerNotFound; | ||
use SpeedPuzzling\Web\Message\GrantMembership; | ||
use SpeedPuzzling\Web\Repository\MembershipRepository; | ||
use SpeedPuzzling\Web\Repository\PlayerRepository; | ||
|
||
readonly final class GrantMembershipHandler | ||
{ | ||
public function __construct( | ||
private PlayerRepository $playerRepository, | ||
private MembershipRepository $membershipRepository, | ||
private ClockInterface $clock, | ||
) { | ||
} | ||
|
||
/** | ||
* @throws PlayerAlreadyHaveMembership | ||
* @throws PlayerNotFound | ||
*/ | ||
public function __invoke(GrantMembership $message): void | ||
{ | ||
|
||
$player = $this->playerRepository->get($message->playerId); | ||
|
||
try { | ||
$this->membershipRepository->getByPlayerId($message->playerId); | ||
|
||
throw new PlayerAlreadyHaveMembership(); | ||
} catch (MembershipNotFound) { | ||
// We want to create new membership - ofc it is not found :-) | ||
$membership = new Membership( | ||
Uuid::uuid7(), | ||
$player, | ||
$this->clock->now(), | ||
endsAt: $message->endsAt, | ||
); | ||
|
||
$this->membershipRepository->save($membership); | ||
} | ||
} | ||
} |