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

StandardProjector   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 28
c 0
b 0
f 0
wmc 4
lcom 1
cbo 3
rs 10

2 Methods

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