Completed
Push — feature/test-php-7-2-in-travis ( af9cbc...5b1260 )
by
unknown
11:15
created

SensitiveDataEventStoreDecorator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A load() 0 11 1
A append() 0 24 3
A loadFromPlayhead() 0 4 1
A assertIdentityAggregate() 0 10 3
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\SensitiveData\EventStore;
20
21
use Broadway\Domain\DomainEventStream as DomainEventStreamInterface;
22
use Broadway\Domain\DomainMessage;
23
use Broadway\EventStore\EventStore as EventStoreInterface;
24
use Surfnet\Stepup\Identity\Event\IdentityEvent;
25
use Surfnet\StepupMiddleware\CommandHandlingBundle\Exception\InvalidArgumentException;
26
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\EventSourcing\SensitiveDataMessage;
27
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\EventSourcing\SensitiveDataMessageStream;
28
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\Forgettable;
29
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\Repository\SensitiveDataMessageRepository;
30
31
/**
32
 * Event store decorator that loads and appends the sensitive data of events into a separate data store.
33
 */
34
final class SensitiveDataEventStoreDecorator implements EventStoreInterface
35
{
36
    /**
37
     * @var EventStoreInterface
38
     */
39
    private $decoratedEventStore;
40
41
    /**
42
     * @var SensitiveDataMessageRepository
43
     */
44
    private $sensitiveDataMessageRepository;
45
46
    /**
47
     * @param EventStoreInterface $decoratedEventStore
48
     * @param SensitiveDataMessageRepository $sensitiveDataMessageRepository
49
     */
50
    public function __construct(
51
        EventStoreInterface $decoratedEventStore,
52
        SensitiveDataMessageRepository $sensitiveDataMessageRepository
53
    ) {
54
        $this->decoratedEventStore = $decoratedEventStore;
55
        $this->sensitiveDataMessageRepository = $sensitiveDataMessageRepository;
56
    }
57
58
    public function load($id): DomainEventStreamInterface
59
    {
60
        $domainEventStream = $this->decoratedEventStore->load($id);
61
62
        $this->assertIdentityAggregate($domainEventStream);
63
64
        $sensitiveDataStream = $this->sensitiveDataMessageRepository->findByIdentityId($id);
65
        $sensitiveDataStream->applyToDomainEventStream($domainEventStream);
66
67
        return $domainEventStream;
68
    }
69
70
    public function append($id, DomainEventStreamInterface $eventStream): void
71
    {
72
        $this->assertIdentityAggregate($eventStream);
73
74
        $this->decoratedEventStore->append($id, $eventStream);
75
76
        $sensitiveDataMessages = [];
77
        foreach ($eventStream as $message) {
78
            /** @var DomainMessage $message */
79
            $event = $message->getPayload();
80
81
            if (!$event instanceof Forgettable) {
82
                continue;
83
            }
84
85
            $sensitiveDataMessages[] = new SensitiveDataMessage(
86
                $id,
87
                $message->getPlayhead(),
88
                $event->getSensitiveData()
89
            );
90
        }
91
92
        $this->sensitiveDataMessageRepository->append(new SensitiveDataMessageStream($sensitiveDataMessages));
93
    }
94
95
    /**
96
     * The method below is not implemented and is only there to comply with the interface to upgrade Broadway
97
     *
98
     * @param $id
99
     * @param int $playhead
100
     * @return DomainEventStreamInterface
101
     */
102
    public function loadFromPlayhead($id, int $playhead): DomainEventStreamInterface
103
    {
104
        throw new InvalidArgumentException("Not implmented");
105
    }
106
107
    /**
108
     * @param DomainEventStreamInterface $stream
109
     */
110
    public function assertIdentityAggregate(DomainEventStreamInterface $stream)
111
    {
112
        foreach ($stream as $message) {
113
            if (!$message->getPayload() instanceof IdentityEvent) {
114
                throw new InvalidArgumentException(
115
                    'The SensitiveDataEventStoreDecorator only works with Identities, please pass in an IdentityId $id'
116
                );
117
            }
118
        }
119
    }
120
}
121