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\Repository; |
20
|
|
|
|
21
|
|
|
use Doctrine\DBAL\Connection; |
22
|
|
|
use Exception as CoreException; |
23
|
|
|
use GuzzleHttp; |
24
|
|
|
use Surfnet\Stepup\Identity\Value\IdentityId; |
25
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Exception\RuntimeException; |
26
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\EventSourcing\SensitiveDataMessage; |
27
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\EventSourcing\SensitiveDataMessageStream; |
28
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\SensitiveData; |
29
|
|
|
|
30
|
|
|
class SensitiveDataMessageRepository |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var Connection |
34
|
|
|
*/ |
35
|
|
|
private $connection; |
36
|
|
|
|
37
|
|
|
public function __construct(Connection $connection) |
38
|
|
|
{ |
39
|
|
|
$this->connection = $connection; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Finds all sensitive data records for a given Identity, ordered by playhead. |
44
|
|
|
* |
45
|
|
|
* @param IdentityId $identityId |
46
|
|
|
* @return SensitiveDataMessageStream |
47
|
|
|
*/ |
48
|
|
|
public function findByIdentityId(IdentityId $identityId) |
49
|
|
|
{ |
50
|
|
|
$sql = 'SELECT identity_id, playhead, sensitive_data |
51
|
|
|
FROM event_stream_sensitive_data |
52
|
|
|
WHERE identity_id = :identity_id |
53
|
|
|
ORDER BY playhead ASC'; |
54
|
|
|
|
55
|
|
|
$rows = $this->connection->fetchAll($sql, ['identity_id' => (string) $identityId]); |
56
|
|
|
$messages = array_map(function (array $row) use ($identityId) { |
57
|
|
|
return new SensitiveDataMessage( |
58
|
|
|
$identityId, |
59
|
|
|
(int) $row['playhead'], |
60
|
|
|
SensitiveData::deserialize(GuzzleHttp\json_decode($row['sensitive_data'], true)) |
61
|
|
|
); |
62
|
|
|
}, $rows); |
63
|
|
|
|
64
|
|
|
return new SensitiveDataMessageStream($messages); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param SensitiveDataMessageStream $sensitiveDataMessageStream |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
View Code Duplication |
public function append(SensitiveDataMessageStream $sensitiveDataMessageStream) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$this->connection->beginTransaction(); |
74
|
|
|
|
75
|
|
|
try { |
76
|
|
|
foreach ($sensitiveDataMessageStream as $sensitiveDataMessage) { |
77
|
|
|
/** @var SensitiveDataMessage $sensitiveDataMessage */ |
78
|
|
|
$this->connection->insert('event_stream_sensitive_data', [ |
79
|
|
|
'identity_id' => (string) $sensitiveDataMessage->getIdentityId(), |
80
|
|
|
'playhead' => $sensitiveDataMessage->getPlayhead(), |
81
|
|
|
'sensitive_data' => json_encode((object) $sensitiveDataMessage->getSensitiveData()->serialize()), |
82
|
|
|
]); |
83
|
|
|
} |
84
|
|
|
$this->connection->commit(); |
85
|
|
|
} catch (CoreException $e) { |
86
|
|
|
$this->connection->rollBack(); |
87
|
|
|
throw new RuntimeException('An exception occurred while appending sensitive data', 0, $e); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param SensitiveDataMessageStream $sensitiveDataMessageStream |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
|
View Code Duplication |
public function modify(SensitiveDataMessageStream $sensitiveDataMessageStream) |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$this->connection->beginTransaction(); |
98
|
|
|
|
99
|
|
|
try { |
100
|
|
|
foreach ($sensitiveDataMessageStream as $sensitiveDataMessage) { |
101
|
|
|
/** @var SensitiveDataMessage $sensitiveDataMessage */ |
102
|
|
|
$this->connection->update( |
103
|
|
|
'event_stream_sensitive_data', |
104
|
|
|
['sensitive_data' => json_encode((object) $sensitiveDataMessage->getSensitiveData()->serialize())], |
105
|
|
|
[ |
106
|
|
|
'identity_id' => (string) $sensitiveDataMessage->getIdentityId(), |
107
|
|
|
'playhead' => $sensitiveDataMessage->getPlayhead(), |
108
|
|
|
] |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
$this->connection->commit(); |
112
|
|
|
} catch (CoreException $e) { |
113
|
|
|
$this->connection->rollBack(); |
114
|
|
|
throw new RuntimeException('An exception occurred while updating sensitive data', 0, $e); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.