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

ProjectorService::__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\Aggregate\AggregatePrefix;
7
use Daikon\Cqrs\EventStore\CommitInterface;
8
use Daikon\Dbal\Exception\DbalException;
9
use Daikon\MessageBus\EnvelopeInterface;
10
11
final class ProjectorService implements ProjectorServiceInterface
12
{
13
    private $projectorMap;
14
15
    public function __construct(ProjectorMap $projectorMap)
16
    {
17
        $this->projectorMap = $projectorMap;
18
    }
19
20
    public function handle(EnvelopeInterface $envelope): bool
21
    {
22
        $commit = $envelope->getMessage();
23
        Assertion::implementsInterface($commit, CommitInterface::class);
24
25
        foreach ($commit->getEventLog() as $domainEvent) {
26
            $aggregatePrefix = AggregatePrefix::fromFqcn($domainEvent->getAggregateRootClass());
27
            $projectors = $this->projectorMap->filterByAggregatePrefix($aggregatePrefix);
28
            foreach ($projectors->getIterator() as $projector) {
29
                if (!$projector->handle($envelope)) {
30
                    throw new DbalException('Projector %s failed to handle message.');
31
                }
32
            }
33
        }
34
35
        return true;
36
    }
37
}
38