Passed
Pull Request — master (#84)
by Frank
16:51 queued 06:51
created

DummyCommandHandler::handle()   A

Complexity

Conditions 6
Paths 24

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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