1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace EventSauce\EventSourcing\Integration\TestingAggregates; |
||
6 | |||
7 | use EventSauce\EventSourcing\AggregateRootRepository; |
||
8 | use EventSauce\EventSourcing\Time\Clock; |
||
9 | |||
10 | class DummyCommandHandler |
||
11 | { |
||
12 | /** |
||
13 | * @var AggregateRootRepository |
||
14 | */ |
||
15 | private $repository; |
||
16 | |||
17 | /** |
||
18 | * @var Clock |
||
19 | */ |
||
20 | private $clock; |
||
21 | |||
22 | 9 | public function __construct(AggregateRootRepository $repository, Clock $clock) |
|
23 | { |
||
24 | 9 | $this->repository = $repository; |
|
25 | 9 | $this->clock = $clock; |
|
26 | 9 | } |
|
27 | |||
28 | /** |
||
29 | * @param DummyCommand $command |
||
30 | * |
||
31 | * @throws DummyException |
||
32 | */ |
||
33 | 9 | public function handle($command) |
|
34 | { |
||
35 | try { |
||
36 | 9 | if ($command instanceof InitiatorCommand) { |
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
37 | 1 | $aggregate = DummyAggregate::create($command->aggregateRootId()); |
|
38 | 1 | goto end; |
|
39 | } |
||
40 | |||
41 | /** @var DummyAggregate $aggregate */ |
||
42 | 8 | $aggregate = $this->repository->retrieve($command->aggregateRootId()); |
|
43 | |||
44 | 8 | if ($command instanceof DummyCommand) { |
|
0 ignored issues
–
show
|
|||
45 | 1 | $aggregate->performDummyTask(); |
|
46 | 7 | } elseif ($command instanceof IgnoredCommand) { |
|
47 | 1 | $aggregate->dontDoAnything(); |
|
48 | 6 | } elseif ($command instanceof ExceptionInducingCommand) { |
|
49 | 3 | $aggregate->throwAnException(); |
|
50 | 3 | } elseif ($command instanceof DummyIncrementCommand) { |
|
51 | 3 | $aggregate->increment(); |
|
52 | } |
||
53 | end: |
||
54 | 6 | } finally { |
|
55 | 9 | $this->repository->persist($aggregate); |
|
56 | } |
||
57 | 6 | } |
|
58 | } |
||
59 |