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

ProjectorService   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 35
rs 10
c 0
b 0
f 0

2 Methods

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