|
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\Stepup\Identity; |
|
20
|
|
|
|
|
21
|
|
|
use Broadway\EventSourcing\EventSourcedAggregateRoot; |
|
22
|
|
|
use Surfnet\Stepup\Exception\DomainException; |
|
23
|
|
|
use Surfnet\Stepup\Identity\Api\Whitelist as WhitelistApi; |
|
24
|
|
|
use Surfnet\Stepup\Identity\Collection\InstitutionCollection; |
|
25
|
|
|
use Surfnet\Stepup\Identity\Event\InstitutionsAddedToWhitelistEvent; |
|
26
|
|
|
use Surfnet\Stepup\Identity\Event\InstitutionsRemovedFromWhitelistEvent; |
|
27
|
|
|
use Surfnet\Stepup\Identity\Event\WhitelistCreatedEvent; |
|
28
|
|
|
use Surfnet\Stepup\Identity\Event\WhitelistReplacedEvent; |
|
29
|
|
|
|
|
30
|
|
|
final class Whitelist extends EventSourcedAggregateRoot implements WhitelistApi |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* There can ever be only one whitelist, so using a fixed UUIDv4 |
|
34
|
|
|
*/ |
|
35
|
|
|
const WHITELIST_AGGREGATE_ID = '125ccee5-d650-437a-a0b0-6bf17c8188fa'; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var InstitutionCollection The collection of institutions currently on the whitelist |
|
39
|
|
|
*/ |
|
40
|
|
|
private $whitelist; |
|
41
|
|
|
|
|
42
|
|
|
public function __construct() |
|
43
|
|
|
{ |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function getAggregateRootId() |
|
47
|
|
|
{ |
|
48
|
|
|
return self::WHITELIST_AGGREGATE_ID; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public static function create(InstitutionCollection $institutionCollection) |
|
52
|
|
|
{ |
|
53
|
|
|
$whitelist = new self(); |
|
54
|
|
|
$whitelist->apply(new WhitelistCreatedEvent($institutionCollection)); |
|
55
|
|
|
|
|
56
|
|
|
return $whitelist; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function replaceAll(InstitutionCollection $institutionCollection) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->apply(new WhitelistReplacedEvent($institutionCollection)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
View Code Duplication |
public function add(InstitutionCollection $institutionCollection) |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
|
|
foreach ($institutionCollection as $institution) { |
|
67
|
|
|
if ($this->whitelist->contains($institution)) { |
|
68
|
|
|
throw new DomainException(sprintf( |
|
69
|
|
|
'Cannot add institution "%s" as it is already whitelisted', |
|
70
|
|
|
$institution |
|
71
|
|
|
)); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$this->apply(new InstitutionsAddedToWhitelistEvent($institutionCollection)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
View Code Duplication |
public function remove(InstitutionCollection $institutionCollection) |
|
|
|
|
|
|
79
|
|
|
{ |
|
80
|
|
|
foreach ($institutionCollection as $institution) { |
|
81
|
|
|
if (!$this->whitelist->contains($institution)) { |
|
82
|
|
|
throw new DomainException(sprintf( |
|
83
|
|
|
'Cannot remove institution "%s" as it is not whitelisted', |
|
84
|
|
|
$institution |
|
85
|
|
|
)); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this->apply(new InstitutionsRemovedFromWhitelistEvent($institutionCollection)); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
protected function applyWhitelistCreatedEvent(WhitelistCreatedEvent $event) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->whitelist = new InstitutionCollection(); |
|
95
|
|
|
$this->whitelist->addAllFrom($event->whitelistedInstitutions); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
protected function applyWhitelistReplacedEvent(WhitelistReplacedEvent $event) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->whitelist = new InstitutionCollection(); |
|
101
|
|
|
$this->whitelist->addAllFrom($event->whitelistedInstitutions); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
protected function applyInstitutionsAddedToWhitelistEvent(InstitutionsAddedToWhitelistEvent $event) |
|
105
|
|
|
{ |
|
106
|
|
|
$this->whitelist->addAllFrom($event->addedInstitutions); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
protected function applyInstitutionsRemovedFromWhitelistEvent(InstitutionsRemovedFromWhitelistEvent $event) |
|
110
|
|
|
{ |
|
111
|
|
|
$this->whitelist->removeAllIn($event->removedInstitutions); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
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.