decorateForWrite()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 3
dl 0
loc 22
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupMiddleware\CommandHandlingBundle\EventSourcing;
20
21
use Broadway\Domain\DomainEventStream;
22
use Broadway\Domain\DomainMessage;
23
use Broadway\Domain\Metadata as BroadwayMetadata;
24
use Broadway\EventSourcing\EventStreamDecorator;
25
use Surfnet\StepupMiddleware\CommandHandlingBundle\Command\Metadata;
26
27
final class MetadataEnrichingEventStreamDecorator implements EventStreamDecorator, MetadataEnricher
28
{
29
    private ?Metadata $metadata = null;
30
31
    public function setMetadata(Metadata $metadata = null): void
32
    {
33
        $this->metadata = $metadata;
34
    }
35
36
    public function decorateForWrite(
37
        string $aggregateType,
38
        string $aggregateIdentifier,
39
        DomainEventStream $eventStream,
40
    ): DomainEventStream {
41
        if (!$this->metadata instanceof Metadata) {
42
            return $eventStream;
43
        }
44
45
        $domainMessages = [];
46
47
        foreach ($eventStream as $domainMessage) {
48
            /** @var DomainMessage $domainMessage */
49
            $domainMessages[] = $domainMessage->andMetadata(
50
                new BroadwayMetadata([
51
                    'actorId' => $this->metadata->actorId,
52
                    'actorInstitution' => $this->metadata->actorInstitution,
53
                ]),
54
            );
55
        }
56
57
        return new DomainEventStream($domainMessages);
58
    }
59
}
60