Created
August 11, 2018 09:57
-
-
Save l3l0/9e880b859043e591f61df4e735cbbf0f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| <?php | |
| declare(strict_types=1); | |
| class SellOnlineTicket implements Command | |
| { | |
| public function clientDetails(): ClientDetails | |
| {} | |
| public function clientEmail(): Email | |
| {} | |
| public function eventId(): EventId | |
| {} | |
| } | |
This file contains hidden or 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
| <?php | |
| declare(strict_types=1); | |
| class SellOnlineTicketHandler implements CommandHandler | |
| { | |
| private $transactionManager; | |
| private $ticketGenerator; | |
| private $ticketSender; | |
| private $tickets; | |
| public function __construct( | |
| TransactionManager $manager, | |
| TicketGenerator $generator, | |
| Tickets $tickets, | |
| TicketSender $sender | |
| ) { | |
| $this->transactionManager = $manager; | |
| $this->ticketGenerator = $generator; | |
| $this->ticketSender = $sender; | |
| $this->tickets = $tickets; | |
| } | |
| public function execute(SellOnlineTicket $command): void | |
| { | |
| $this->transactionManager->begin(); | |
| try { | |
| $ticket = $this->ticketGenerator->generate( | |
| $command->clientDetails(), | |
| $command->eventId() | |
| ); | |
| $this->tickets->add($ticket); | |
| $this->transactionManager->commit(); | |
| } catch (Throwable $e) { | |
| $this->transactionManager->rollback(); | |
| throw $e; | |
| } | |
| $this->ticketSender->send($ticket, $command->clientEmail()); | |
| } | |
| } |
This file contains hidden or 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
| <?php | |
| declare(strict_types=1); | |
| interface TicketGenerator | |
| { | |
| public function generate(ClientDetails $details, EventId $id): Ticket; | |
| } |
This file contains hidden or 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
| <?php | |
| declare(strict_types=1); | |
| interface Tickets | |
| { | |
| /** | |
| * @throws TicketNotFound | |
| */ | |
| public function get(TicketNumber $number): Ticket; | |
| public function add(Ticket): void; | |
| } |
This file contains hidden or 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
| <?php | |
| declare(strict_types=1); | |
| interface TicketSender | |
| { | |
| /** | |
| * @throws EmailSenderFailure | |
| */ | |
| public function send(Ticket $ticket, Email $email): void; | |
| } |
This file contains hidden or 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
| <?php | |
| declare(strict_types=1); | |
| interface TransactionManager | |
| { | |
| public function begin(): void; | |
| public function commit(): void; | |
| public function rollback(): void; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment