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\DomainEventStreamInterface; |
22
|
|
|
use Broadway\Domain\DomainMessage; |
23
|
|
|
use Broadway\EventStore\EventStoreInterface; |
24
|
|
|
use Surfnet\Stepup\Identity\Value\IdentityId; |
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) |
59
|
|
|
{ |
60
|
|
|
if (!$id instanceof IdentityId) { |
61
|
|
|
throw new InvalidArgumentException( |
62
|
|
|
'The SensitiveDataEventStoreDecorator only works with Identities, please pass in an IdentityId $id' |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$domainEventStream = $this->decoratedEventStore->load($id); |
67
|
|
|
|
68
|
|
|
$sensitiveDataStream = $this->sensitiveDataMessageRepository->findByIdentityId($id); |
69
|
|
|
$sensitiveDataStream->applyToDomainEventStream($domainEventStream); |
70
|
|
|
|
71
|
|
|
return $domainEventStream; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function append($id, DomainEventStreamInterface $eventStream) |
75
|
|
|
{ |
76
|
|
|
if (!$id instanceof IdentityId) { |
77
|
|
|
throw new InvalidArgumentException( |
78
|
|
|
'The SensitiveDataEventStoreDecorator only works with Identities, please pass in an IdentityId $id' |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$this->decoratedEventStore->append($id, $eventStream); |
83
|
|
|
|
84
|
|
|
$sensitiveDataMessages = []; |
85
|
|
|
foreach ($eventStream as $message) { |
86
|
|
|
/** @var DomainMessage $message */ |
87
|
|
|
$event = $message->getPayload(); |
88
|
|
|
|
89
|
|
|
if (!$event instanceof Forgettable) { |
90
|
|
|
continue; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$sensitiveDataMessages[] = new SensitiveDataMessage( |
94
|
|
|
$id, |
95
|
|
|
$message->getPlayhead(), |
96
|
|
|
$event->getSensitiveData() |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$this->sensitiveDataMessageRepository->append(new SensitiveDataMessageStream($sensitiveDataMessages)); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|