| 1 | <?php |
||
| 10 | class DummyCommandHandler implements CommandHandler |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var AggregateRootRepository |
||
| 14 | */ |
||
| 15 | private $repository; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var Clock |
||
| 19 | */ |
||
| 20 | private $clock; |
||
| 21 | |||
| 22 | public function __construct(AggregateRootRepository $repository, Clock $clock) |
||
| 23 | { |
||
| 24 | $this->repository = $repository; |
||
| 25 | $this->clock = $clock; |
||
| 26 | } |
||
| 27 | |||
| 28 | public function handle(Command $command) |
||
| 29 | { |
||
| 30 | /** @var DummyAggregate $aggregate */ |
||
| 31 | $aggregate = $this->repository->retrieve($command->aggregateRootId()); |
||
| 32 | |||
| 33 | try { |
||
| 34 | if ($command instanceof DummyCommand) { |
||
| 35 | $aggregate->performDummyTask($this->clock); |
||
| 36 | } elseif ($command instanceof IgnoredCommand) { |
||
| 37 | $aggregate->dontDoAnything(); |
||
| 38 | } elseif ($command instanceof ExceptionInducingCommand) { |
||
| 39 | $aggregate->throwAnException(); |
||
| 40 | } elseif ($command instanceof DummyIncrementCommand) { |
||
| 41 | $aggregate->increment($this->clock); |
||
| 42 | } |
||
| 43 | } finally { |
||
| 44 | $this->repository->persist(... $aggregate->releaseEvents()); |
||
| 45 | } |
||
| 46 | } |
||
| 47 | } |