Passed
Pull Request — master (#84)
by Frank
03:17
created

DummyCommandHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
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
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
     * @throws DummyException
30
     */
31 9
    public function handle(DummyCommand $command): void
32
    {
33
        try {
34 9
            if ($command instanceof InitiatorCommand) {
35 1
                $aggregate = DummyAggregate::create($command->aggregateRootId());
36
37 1
                return;
38
            }
39
40
            /** @var DummyAggregate $aggregate */
41 8
            $aggregate = $this->repository->retrieve($command->aggregateRootId());
42
43 8
            if ($command instanceof PerformDummyTask) {
44 1
                $aggregate->performDummyTask();
45 7
            } elseif ($command instanceof IgnoredCommand) {
46 1
                $aggregate->dontDoAnything();
47 6
            } elseif ($command instanceof ExceptionInducingCommand) {
48 3
                $aggregate->throwAnException();
49 3
            } elseif ($command instanceof DummyIncrementCommand) {
50 3
                $aggregate->increment();
51
            }
52 5
        } finally {
53 9
            $this->repository->persist($aggregate);
54
        }
55 5
    }
56
}
57