Completed
Push — master ( e5a769...e9d24b )
by
unknown
15s queued 10s
created

PlaceRepository   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 8
dl 0
loc 90
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A saveMultiple() 0 30 4
A protectedDecorateForWrite() 0 16 2
1
<?php
2
3
namespace CultuurNet\UDB3\Place;
4
5
use Assert\Assertion as Assert;
6
use Broadway\Domain\AggregateRoot;
7
use Broadway\Domain\DomainEventStream;
8
use Broadway\Domain\DomainEventStreamInterface;
9
use Broadway\EventHandling\EventBusInterface;
10
use Broadway\EventSourcing\AggregateFactory\PublicConstructorAggregateFactory;
11
use Broadway\EventSourcing\EventSourcingRepository;
12
use Broadway\EventSourcing\EventStreamDecoratorInterface;
13
use Broadway\EventStore\EventStoreInterface;
14
15
class PlaceRepository extends EventSourcingRepository
16
{
17
    private const AGGREGATE_CLASS = Place::class;
18
19
    /**
20
     * @var EventStoreInterface
21
     */
22
    protected $protectedEventStore;
23
24
    /**
25
     * @var EventBusInterface
26
     */
27
    protected $protectedEventBus;
28
29
    /**
30
     * @var array|EventStreamDecoratorInterface[]
31
     */
32
    protected $protectedEventStreamDecorators;
33
34
    /**
35
     * @param EventStoreInterface $eventStore
36
     * @param EventBusInterface $eventBus
37
     * @param EventStreamDecoratorInterface[] $eventStreamDecorators
38
     */
39
    public function __construct(
40
        EventStoreInterface $eventStore,
41
        EventBusInterface $eventBus,
42
        array $eventStreamDecorators = array()
43
    ) {
44
        parent::__construct(
45
            $eventStore,
46
            $eventBus,
47
            self::AGGREGATE_CLASS,
48
            new PublicConstructorAggregateFactory(),
49
            $eventStreamDecorators
50
        );
51
52
        $this->protectedEventStore = $eventStore;
53
        $this->protectedEventBus = $eventBus;
54
        $this->protectedEventStreamDecorators = $eventStreamDecorators;
55
    }
56
57
    public function saveMultiple(AggregateRoot ...$aggregates): void
58
    {
59
        if (empty($aggregates)) {
60
            return;
61
        }
62
63
        $firstId = null;
64
        $domainEvents = [];
65
        foreach ($aggregates as $aggregate) {
66
            Assert::isInstanceOf($aggregate, self::AGGREGATE_CLASS);
67
68
            if (!$firstId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $firstId of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
69
                // We need to pass an aggregate id to the EventStore::append() method, but it's not actually used for
70
                // anything except for a check that it's a string. So we just pass the one of the first aggregate.
71
                $firstId = $aggregate->getAggregateRootId();
72
            }
73
74
            $eventStream = $this->protectedDecorateForWrite($aggregate, $aggregate->getUncommittedEvents());
75
76
            $domainEvents = array_merge(
77
                $domainEvents,
78
                iterator_to_array($eventStream->getIterator())
79
            );
80
        }
81
82
        $eventStream = new DomainEventStream($domainEvents);
83
84
        $this->protectedEventStore->append($firstId, $eventStream);
85
        $this->protectedEventBus->publish($eventStream);
86
    }
87
88
    protected function protectedDecorateForWrite(
89
        AggregateRoot $aggregate,
90
        DomainEventStreamInterface $eventStream
91
    ): DomainEventStreamInterface {
92
        $aggregateIdentifier = $aggregate->getAggregateRootId();
93
94
        foreach ($this->protectedEventStreamDecorators as $eventStreamDecorator) {
95
            $eventStream = $eventStreamDecorator->decorateForWrite(
96
                self::AGGREGATE_CLASS,
97
                $aggregateIdentifier,
98
                $eventStream
99
            );
100
        }
101
102
        return $eventStream;
103
    }
104
}
105