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\CommandHandlingBundle\Configuration\Processor; |
20
|
|
|
|
21
|
|
|
use Broadway\Processor\Processor; |
22
|
|
|
use Rhumsaa\Uuid\Uuid; |
23
|
|
|
use Surfnet\Stepup\Configuration\Value\Institution; |
24
|
|
|
use Surfnet\Stepup\Identity\Event\IdentityCreatedEvent; |
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 Symfony\Component\DependencyInjection\ContainerInterface; |
31
|
|
|
|
32
|
|
|
final class InstitutionConfigurationProcessor extends Processor |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var ConfiguredInstitutionRepository |
36
|
|
|
*/ |
37
|
|
|
private $configuredInstitutionRepository; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var ContainerInterface |
41
|
|
|
*/ |
42
|
|
|
private $container; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The container needs to be called during runtime in order to prevent a circular reference |
46
|
|
|
* during container compilation. |
47
|
|
|
* |
48
|
|
|
* @param ConfiguredInstitutionRepository $configuredInstitutionRepository |
49
|
|
|
* @param ContainerInterface $container |
50
|
|
|
*/ |
51
|
|
|
public function __construct( |
52
|
|
|
ConfiguredInstitutionRepository $configuredInstitutionRepository, |
|
|
|
|
53
|
|
|
ContainerInterface $container |
54
|
|
|
) { |
55
|
|
|
$this->configuredInstitutionRepository = $configuredInstitutionRepository; |
56
|
|
|
$this->container = $container; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function handleIdentityCreatedEvent(IdentityCreatedEvent $event) |
60
|
|
|
{ |
61
|
|
|
$institution = new Institution($event->identityInstitution->getInstitution()); |
62
|
|
|
|
63
|
|
|
if ($this->configuredInstitutionRepository->hasConfigurationFor($institution)) { |
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->createConfigurationFor($institution); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
View Code Duplication |
public function handleWhitelistCreatedEvent(WhitelistCreatedEvent $event) |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
foreach ($event->whitelistedInstitutions as $whitelistedInstitution) { |
73
|
|
|
$institution = new Institution($whitelistedInstitution->getInstitution()); |
74
|
|
|
|
75
|
|
|
if ($this->configuredInstitutionRepository->hasConfigurationFor($institution)) { |
76
|
|
|
continue; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->createConfigurationFor($institution); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
public function handleWhitelistReplacedEvent(WhitelistReplacedEvent $event) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
foreach ($event->whitelistedInstitutions as $whitelistedInstitution) { |
86
|
|
|
$institution = new Institution($whitelistedInstitution->getInstitution()); |
87
|
|
|
|
88
|
|
|
if ($this->configuredInstitutionRepository->hasConfigurationFor($institution)) { |
89
|
|
|
continue; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->createConfigurationFor($institution); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function handleInstitutionsAddedToWhitelistEvent(InstitutionsAddedToWhitelistEvent $event) |
97
|
|
|
{ |
98
|
|
|
foreach ($event->addedInstitutions as $addedInstitution) { |
99
|
|
|
$institution = new Institution($addedInstitution->getInstitution()); |
100
|
|
|
|
101
|
|
|
if ($this->configuredInstitutionRepository->hasConfigurationFor($institution)) { |
102
|
|
|
continue; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->createConfigurationFor($institution); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param Institution $institution |
111
|
|
|
*/ |
112
|
|
|
private function createConfigurationFor(Institution $institution) |
113
|
|
|
{ |
114
|
|
|
$command = new CreateInstitutionConfigurationCommand(); |
115
|
|
|
$command->UUID = (string) Uuid::uuid4(); |
116
|
|
|
$command->institution = $institution->getInstitution(); |
117
|
|
|
|
118
|
|
|
$this->container->get('pipeline')->process($command); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.