Test Failed
Push — master ( deb397...eab8d5 )
by Mr
01:40
created

StandardProjector::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Daikon\Dbal\Projector;
4
5
use Assert\Assertion;
6
use Daikon\Cqrs\EventStore\CommitInterface;
7
use Daikon\Dbal\Repository\RepositoryInterface;
8
use Daikon\MessageBus\EnvelopeInterface;
9
10
final class StandardProjector implements ProjectorInterface
11
{
12
    private $repository;
13
14
    public function __construct(RepositoryInterface $repository)
15
    {
16
        $this->repository = $repository;
17
    }
18
19
    public function handle(EnvelopeInterface $envelope): bool
20
    {
21
        $commit = $envelope->getMessage();
22
        Assertion::isInstanceOf($commit, CommitInterface::class);
23
24
        if ($commit->getStreamRevision()->toNative() === 1) {
25
            $projection = $this->repository->makeProjection();
26
        } else {
27
            $aggregateId = $commit->getStreamId()->toNative();
28
            $projection = $this->repository->findById($aggregateId);
29
        }
30
31
        foreach ($commit->getEventLog() as $domainEvent) {
32
            $projection = $projection->applyEvent($domainEvent);
33
        }
34
35
        return $this->repository->persist($projection);
36
    }
37
}
38