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

DummyCommandHandler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 44
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 23 6
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
    public function __construct(AggregateRootRepository $repository, Clock $clock)
23
    {
24
        $this->repository = $repository;
25
        $this->clock = $clock;
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
            $this->repository->persist($aggregate);
54
        }
55
    }
56
}
57