Passed
Pull Request — master (#84)
by Frank
14:06 queued 04:07
created

DummyCommandHandler::handle()   A

Complexity

Conditions 6
Paths 12

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