Completed
Pull Request — master (#414)
by
unknown
03:33
created

PlaceRepository::saveMultiple()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
cc 3
nc 3
nop 1
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
        $firstId = null;
60
        $domainEvents = [];
61
        foreach ($aggregates as $aggregate) {
62
            Assert::isInstanceOf($aggregate, self::AGGREGATE_CLASS);
63
64
            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...
65
                // We need to pass an aggregate id to the EventStore::append() method, but it's not actually used for
66
                // anything except for a check that it's a string. So we just pass the one of the first aggregate.
67
                $firstId = $aggregate->getAggregateRootId();
68
            }
69
70
            $eventStream = $this->protectedDecorateForWrite($aggregate, $aggregate->getUncommittedEvents());
71
72
            $domainEvents = array_merge(
73
                $domainEvents,
74
                iterator_to_array($eventStream->getIterator())
75
            );
76
        }
77
78
        $eventStream = new DomainEventStream($domainEvents);
79
80
        $this->protectedEventStore->append($firstId, $eventStream);
81
        $this->protectedEventBus->publish($eventStream);
82
    }
83
84
    protected function protectedDecorateForWrite(
85
        AggregateRoot $aggregate,
86
        DomainEventStreamInterface $eventStream
87
    ): DomainEventStreamInterface {
88
        $aggregateIdentifier = $aggregate->getAggregateRootId();
89
90
        foreach ($this->protectedEventStreamDecorators as $eventStreamDecorator) {
91
            $eventStream = $eventStreamDecorator->decorateForWrite(
92
                self::AGGREGATE_CLASS,
93
                $aggregateIdentifier,
94
                $eventStream
95
            );
96
        }
97
98
        return $eventStream;
99
    }
100
}
101