Test Failed
Push — master ( 383de7...76b093 )
by Mr
01:46
created

ProjectorService::handle()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 14
nc 4
nop 1
1
<?php
2
3
namespace Daikon\ReadModel\Projector;
4
5
use Assert\Assertion;
6
use Daikon\EventSourcing\Aggregate\AggregatePrefix;
7
use Daikon\EventSourcing\EventStore\CommitInterface;
8
use Daikon\MessageBus\EnvelopeInterface;
9
use Daikon\MessageBus\MessageBusInterface;
10
use Daikon\ReadModel\Exception\ReadModelException;
11
12
final class ProjectorService implements ProjectorServiceInterface
13
{
14
    private $projectorMap;
15
16
    private $messageBus;
17
18
    public function __construct(ProjectorMap $projectorMap, MessageBusInterface $messageBus)
19
    {
20
        $this->projectorMap = $projectorMap;
21
        $this->messageBus = $messageBus;
22
    }
23
24
    public function handle(EnvelopeInterface $envelope): bool
25
    {
26
        $commit = $envelope->getMessage();
27
        Assertion::implementsInterface($commit, CommitInterface::class);
28
29
        $metadata = $envelope->getMetadata();
30
        foreach ($commit->getEventLog() as $domainEvent) {
31
            $fqcn = $domainEvent->getAggregateRootClass();
32
            $aggregateAlias = $fqcn::getAlias();
33
            $projectors = $this->projectorMap->filterByAggregateAlias($aggregateAlias);
34
            foreach ($projectors->getIterator() as $projector) {
35
                if (!$projector->handle($envelope)) {
36
                    throw new ReadModelException('Projector %s failed to handle message.');
37
                }
38
            }
39
40
            $metadata = $metadata->with('_aggregate_alias', $aggregateAlias->toNative());
41
            $this->messageBus->publish($domainEvent, 'events', $metadata);
42
        }
43
44
        return true;
45
    }
46
}
47