Passed
Pull Request — master (#84)
by Frank
09:22
created

DummyCommandHandler::handle()   B

Complexity

Conditions 7
Paths 48

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 16
c 2
b 0
f 0
nc 48
nop 1
dl 0
loc 24
rs 8.8333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EventSauce\EventSourcing\TestUtilities\TestingAggregates;
6
7
use EventSauce\EventSourcing\AggregateRootRepository;
8
9
/**
10
 * @testAsset
11
 */
12
class DummyCommandHandler
13
{
14
    /**
15
     * @phpstan-var AggregateRootRepository<DummyAggregate>
16
     */
17
    private AggregateRootRepository $repository;
18
19
    /**
20
     * @phpstan-param AggregateRootRepository<DummyAggregate> $repository
21
     */
22
    public function __construct(AggregateRootRepository $repository)
23
    {
24
        $this->repository = $repository;
25
    }
26
27
    /**
28
     * @throws DummyException
29
     */
30
    public function handle(DummyCommand $command): void
31
    {
32
        try {
33
            if ($command instanceof InitiatorCommand) {
34
                $aggregate = DummyAggregate::create($command->aggregateRootId());
35
36
                return;
37
            }
38
39
            /** @var DummyAggregate $aggregate */
40
            $aggregate = $this->repository->retrieve($command->aggregateRootId());
41
42
            if ($command instanceof PerformDummyTask) {
43
                $aggregate->performDummyTask();
44
            } elseif ($command instanceof IgnoredCommand) {
45
                $aggregate->dontDoAnything();
46
            } elseif ($command instanceof ExceptionInducingCommand) {
47
                $aggregate->throwAnException();
48
            } elseif ($command instanceof DummyIncrementCommand) {
49
                $aggregate->increment();
50
            }
51
        } finally {
52
            if (isset($aggregate)) {
53
                $this->repository->persist($aggregate);
54
            }
55
        }
56
    }
57
}
58