1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2018 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\Projector; |
20
|
|
|
|
21
|
|
|
use Broadway\ReadModel\Projector; |
22
|
|
|
use Surfnet\Stepup\Configuration\Event\InstitutionConfigurationRemovedEvent; |
23
|
|
|
use Surfnet\Stepup\Configuration\Event\NewInstitutionConfigurationCreatedEvent; |
24
|
|
|
use Surfnet\Stepup\Configuration\Event\SelectRaaOptionChangedEvent; |
25
|
|
|
use Surfnet\Stepup\Configuration\Event\UseRaaOptionChangedEvent; |
26
|
|
|
use Surfnet\Stepup\Configuration\Event\UseRaOptionChangedEvent; |
27
|
|
|
use Surfnet\Stepup\Configuration\Value\Institution; |
28
|
|
|
use Surfnet\Stepup\Identity\Collection\InstitutionCollection; |
29
|
|
|
use Surfnet\Stepup\Identity\Event\InstitutionsAddedToWhitelistEvent; |
30
|
|
|
use Surfnet\Stepup\Identity\Event\InstitutionsRemovedFromWhitelistEvent; |
31
|
|
|
use Surfnet\Stepup\Identity\Event\WhitelistCreatedEvent; |
32
|
|
|
use Surfnet\Stepup\Identity\Event\WhitelistReplacedEvent; |
33
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Repository\InstitutionAuthorizationRepository; |
34
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Repository\InstitutionConfigurationOptionsRepository; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
38
|
|
|
*/ |
39
|
|
|
final class InstitutionAuthorizationProjector extends Projector |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* @var InstitutionAuthorizationRepository |
43
|
|
|
*/ |
44
|
|
|
private $institutionAuthorizationRepository; |
45
|
|
|
/** |
46
|
|
|
* @var InstitutionConfigurationOptionsRepository |
47
|
|
|
*/ |
48
|
|
|
private $institutionConfigurationOptionsRepository; |
49
|
|
|
|
50
|
|
|
public function __construct( |
51
|
|
|
InstitutionAuthorizationRepository $institutionAuthorizationRepository, |
52
|
|
|
InstitutionConfigurationOptionsRepository $institutionConfigurationOptionsRepository |
53
|
|
|
) { |
54
|
|
|
$this->institutionAuthorizationRepository = $institutionAuthorizationRepository; |
55
|
|
|
$this->institutionConfigurationOptionsRepository = $institutionConfigurationOptionsRepository; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function applyNewInstitutionConfigurationCreatedEvent(NewInstitutionConfigurationCreatedEvent $event) |
59
|
|
|
{ |
60
|
|
|
$this->setToDefaultIfNoConfigurationOptionsExist($event->institution); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function applyUseRaOptionChangedEvent(UseRaOptionChangedEvent $event) |
64
|
|
|
{ |
65
|
|
|
$this->institutionAuthorizationRepository->saveInstitutionOption( |
66
|
|
|
$event->institution, |
67
|
|
|
$event->useRaOption |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function applyUseRaaOptionChangedEvent(UseRaaOptionChangedEvent $event) |
72
|
|
|
{ |
73
|
|
|
$this->institutionAuthorizationRepository->saveInstitutionOption( |
74
|
|
|
$event->institution, |
75
|
|
|
$event->useRaaOption |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function applySelectRaaOptionChangedEvent(SelectRaaOptionChangedEvent $event) |
80
|
|
|
{ |
81
|
|
|
$this->institutionAuthorizationRepository->saveInstitutionOption( |
82
|
|
|
$event->institution, |
83
|
|
|
$event->selectRaaOption |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function applyInstitutionConfigurationRemovedEvent(InstitutionConfigurationRemovedEvent $event) |
88
|
|
|
{ |
89
|
|
|
$this->institutionAuthorizationRepository->clearInstitutionOption( |
90
|
|
|
$event->institution |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected function applyWhitelistCreatedEvent(WhitelistCreatedEvent $event) |
95
|
|
|
{ |
96
|
|
|
$this->setToDefaultIfNoConfigurationOptionsExist($event->whitelistedInstitutions); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected function applyWhitelistReplacedEvent(WhitelistReplacedEvent $event) |
100
|
|
|
{ |
101
|
|
|
$this->setToDefaultIfNoConfigurationOptionsExist($event->whitelistedInstitutions); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
protected function applyInstitutionsAddedToWhitelistEvent(InstitutionsAddedToWhitelistEvent $event) |
105
|
|
|
{ |
106
|
|
|
$this->setToDefaultIfNoConfigurationOptionsExist($event->addedInstitutions); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected function applyInstitutionsRemovedFromWhitelistEvent(InstitutionsRemovedFromWhitelistEvent $event) |
110
|
|
|
{ |
111
|
|
|
$this->removeIfConfigurationOptionsDoNotExist($event->removedInstitutions); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param InstitutionCollection $institutionCollection |
116
|
|
|
* @throws \Doctrine\ORM\NoResultException |
117
|
|
|
* @throws \Doctrine\ORM\NonUniqueResultException |
118
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
119
|
|
|
*/ |
120
|
|
View Code Duplication |
private function setToDefaultIfNoConfigurationOptionsExist(InstitutionCollection $institutionCollection) |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
foreach ($institutionCollection as $institution) { |
123
|
|
|
$configurationInstitution = new Institution($institution->getInstitution()); |
124
|
|
|
if (!$this->institutionConfigurationOptionsRepository->findConfigurationOptionsFor($configurationInstitution)) { |
125
|
|
|
$this->institutionAuthorizationRepository->setDefaultInstitutionOption($configurationInstitution); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param InstitutionCollection $institutionCollection |
132
|
|
|
* @throws \Doctrine\ORM\NoResultException |
133
|
|
|
* @throws \Doctrine\ORM\NonUniqueResultException |
134
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
135
|
|
|
*/ |
136
|
|
View Code Duplication |
private function removeIfConfigurationOptionsDoNotExist(InstitutionCollection $institutionCollection) |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
foreach ($institutionCollection as $institution) { |
139
|
|
|
$configurationInstitution = new Institution($institution->getInstitution()); |
140
|
|
|
if (!$this->institutionConfigurationOptionsRepository->findConfigurationOptionsFor($configurationInstitution)) { |
141
|
|
|
$this->institutionAuthorizationRepository->clearInstitutionOption( |
142
|
|
|
$configurationInstitution |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: