1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2016 SURFnet B.V. |
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\ApiBundle\Configuration\EventListener; |
20
|
|
|
|
21
|
|
|
use Broadway\Domain\DomainMessage; |
22
|
|
|
use Broadway\EventHandling\EventListenerInterface; |
23
|
|
|
use Rhumsaa\Uuid\Uuid; |
24
|
|
|
use Surfnet\Stepup\Configuration\Value\Institution; |
25
|
|
|
use Surfnet\Stepup\Identity\Event\InstitutionsAddedToWhitelistEvent; |
26
|
|
|
use Surfnet\Stepup\Identity\Event\WhitelistCreatedEvent; |
27
|
|
|
use Surfnet\Stepup\Identity\Event\WhitelistReplacedEvent; |
28
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Repository\ConfiguredInstitutionRepository; |
29
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Command\CreateInstitutionConfigurationCommand; |
30
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Pipeline\Pipeline; |
31
|
|
|
|
32
|
|
|
final class WhitelistEventListener implements EventListenerInterface |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var ConfiguredInstitutionRepository |
36
|
|
|
*/ |
37
|
|
|
private $configuredInstitutionRepository; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var Pipeline |
41
|
|
|
*/ |
42
|
|
|
private $pipeline; |
43
|
|
|
|
44
|
|
|
public function __construct(ConfiguredInstitutionRepository $configuredInstitutionRepository, Pipeline $pipeline) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$this->configuredInstitutionRepository = $configuredInstitutionRepository; |
47
|
|
|
$this->pipeline = $pipeline; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param DomainMessage $domainMessage |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
public function handle(DomainMessage $domainMessage) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$event = $domainMessage->getPayload(); |
56
|
|
|
|
57
|
|
|
$classParts = explode('\\', get_class($event)); |
58
|
|
|
$method = 'apply' . end($classParts); |
59
|
|
|
|
60
|
|
|
if (!method_exists($this, $method)) { |
61
|
|
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->$method($event, $domainMessage); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
View Code Duplication |
public function applyWhitelistCreatedEvent(WhitelistCreatedEvent $event) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
foreach ($event->whitelistedInstitutions as $whitelistedInstitution) { |
70
|
|
|
$institution = new Institution($whitelistedInstitution->getInstitution()); |
71
|
|
|
|
72
|
|
|
if ($this->configuredInstitutionRepository->hasConfigurationFor($institution)) { |
73
|
|
|
continue; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->createInstitutionConfigurationFor($institution); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
public function applyWhitelistReplacedEvent(WhitelistReplacedEvent $event) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
foreach ($event->whitelistedInstitutions as $whitelistedInstitution) { |
83
|
|
|
$institution = new Institution($whitelistedInstitution->getInstitution()); |
84
|
|
|
|
85
|
|
|
if ($this->configuredInstitutionRepository->hasConfigurationFor($institution)) { |
86
|
|
|
continue; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$this->createInstitutionConfigurationFor($institution); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function applyInstitutionsAddedToWhitelistEvent(InstitutionsAddedToWhitelistEvent $event) |
94
|
|
|
{ |
95
|
|
|
foreach ($event->addedInstitutions as $addedInstitution) { |
96
|
|
|
$institution = new Institution($addedInstitution->getInstitution()); |
97
|
|
|
|
98
|
|
|
if ($this->configuredInstitutionRepository->hasConfigurationFor($institution)) { |
99
|
|
|
continue; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$this->createInstitutionConfigurationFor($institution); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param Institution $institution |
108
|
|
|
*/ |
109
|
|
|
private function createInstitutionConfigurationFor(Institution $institution) |
110
|
|
|
{ |
111
|
|
|
$command = new CreateInstitutionConfigurationCommand(); |
112
|
|
|
$command->UUID = (string) Uuid::uuid4(); |
113
|
|
|
$command->institution = $institution; |
|
|
|
|
114
|
|
|
|
115
|
|
|
$this->pipeline->process($command); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.