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

DummyCommandHandler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 41
rs 10
c 2
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 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<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