Completed
Push — master ( 4803b3...959101 )
by Frank
03:46
created

DummyCommandHandler::handle()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 13
cts 13
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 13
nc 5
nop 1
crap 5
1
<?php
2
3
namespace EventSauce\EventSourcing\Integration\TestingAggregates;
4
5
use EventSauce\EventSourcing\AggregateRootRepository;
6
use EventSauce\EventSourcing\Command;
7
use EventSauce\EventSourcing\CommandHandler;
8
use EventSauce\EventSourcing\Time\Clock;
9
10
class DummyCommandHandler implements CommandHandler
11
{
12
    /**
13
     * @var AggregateRootRepository
14
     */
15
    private $repository;
16
17
    /**
18
     * @var Clock
19
     */
20
    private $clock;
21
22 6
    public function __construct(AggregateRootRepository $repository, Clock $clock)
23
    {
24 6
        $this->repository = $repository;
25 6
        $this->clock = $clock;
26 6
    }
27
28 6
    public function handle(Command $command)
29
    {
30
        /** @var DummyAggregate $aggregate */
31 6
        $aggregate = $this->repository->retrieve($command->aggregateRootId());
32
33
        try {
34 6
            if ($command instanceof DummyCommand) {
35 1
                $aggregate->performDummyTask($this->clock);
36 5
            } elseif ($command instanceof IgnoredCommand) {
37 1
                $aggregate->dontDoAnything();
38 4
            } elseif ($command instanceof ExceptionInducingCommand) {
39 3
                $aggregate->throwAnException();
40 1
            } elseif ($command instanceof DummyIncrementCommand) {
41 1
                $aggregate->increment($this->clock);
42
            }
43 3
        } finally {
44 6
            $this->repository->persist($aggregate);
45
        }
46
    }
47
}