Completed
Push — master ( 860638...4d5894 )
by Frank
02:47 queued 01:15
created

DummyCommandHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
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
    public function __construct(AggregateRootRepository $repository, Clock $clock)
23
    {
24
        $this->repository = $repository;
25
        $this->clock = $clock;
26
    }
27
28
    public function handle(Command $command)
29
    {
30
        /** @var DummyAggregate $aggregate */
31
        $aggregate = $this->repository->retrieve($command->aggregateRootId());
32
33
        try {
34
            if ($command instanceof DummyCommand) {
35
                $aggregate->performDummyTask($this->clock);
36
            } elseif ($command instanceof IgnoredCommand) {
37
                $aggregate->dontDoAnything();
38
            } elseif ($command instanceof ExceptionInducingCommand) {
39
                $aggregate->throwAnException();
40
            } elseif ($command instanceof DummyIncrementCommand) {
41
                $aggregate->increment($this->clock);
42
            }
43
        } finally {
44
            $this->repository->persist($aggregate);
45
        }
46
    }
47
}