Passed
Pull Request — master (#84)
by Frank
19:45 queued 09:46
created

DummyCommandHandler::handle()   B

Complexity

Conditions 8
Paths 48

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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