Completed
Pull Request — develop (#247)
by
unknown
55:28 queued 26:26
created

updateInstitutionCandidatesFromCollection()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
c 0
b 0
f 0
rs 8.6417
cc 6
nc 10
nop 2
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\ApiBundle\Identity\Projector;
20
21
use Broadway\ReadModel\Projector;
22
use Surfnet\Stepup\Configuration\Event\InstitutionConfigurationRemovedEvent;
23
use Surfnet\Stepup\Configuration\Event\SelectRaaOptionChangedEvent;
24
use Surfnet\Stepup\Configuration\Event\SraaUpdatedEvent;
25
use Surfnet\Stepup\Identity\Collection\InstitutionCollection;
26
use Surfnet\Stepup\Identity\Event\IdentityAccreditedAsRaaForInstitutionEvent;
27
use Surfnet\Stepup\Identity\Event\IdentityAccreditedAsRaForInstitutionEvent;
28
use Surfnet\Stepup\Identity\Event\RegistrationAuthorityRetractedForInstitutionEvent;
29
use Surfnet\Stepup\Identity\Value\IdentityId;
30
use Surfnet\Stepup\Identity\Value\Institution;
31
use Surfnet\Stepup\Configuration\Value\Institution as ConfigurationInstitution;
32
use Surfnet\Stepup\Identity\Event\CompliedWithVettedSecondFactorRevocationEvent;
33
use Surfnet\Stepup\Identity\Event\IdentityAccreditedAsRaaEvent;
34
use Surfnet\Stepup\Identity\Event\IdentityAccreditedAsRaEvent;
35
use Surfnet\Stepup\Identity\Event\IdentityForgottenEvent;
36
use Surfnet\Stepup\Identity\Event\RegistrationAuthorityRetractedEvent;
37
use Surfnet\Stepup\Identity\Event\SecondFactorVettedEvent;
38
use Surfnet\Stepup\Identity\Event\VettedSecondFactorRevokedEvent;
39
use Surfnet\Stepup\Identity\Event\YubikeySecondFactorBootstrappedEvent;
40
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Repository\InstitutionAuthorizationRepository;
41
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaCandidate;
42
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\IdentityRepository;
43
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\RaCandidateRepository;
44
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\RaListingRepository;
45
46
/**
47
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
48
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
49
 */
50
class RaCandidateProjector extends Projector
51
{
52
    /**
53
     * @var RaCandidateRepository
54
     */
55
    private $raCandidateRepository;
56
57
    /**
58
     * @var RaListingRepository
59
     */
60
    private $raListingRepository;
61
62
    /**
63
     * @var institutionAuthorizationRepository
64
     */
65
    private $institutionAuthorizationRepository;
66
    /**
67
     * @var IdentityRepository
68
     */
69
    private $identityRepository;
70
71
    public function __construct(
72
        RaCandidateRepository $raCandidateRepository,
73
        RaListingRepository $raListingRepository,
74
        InstitutionAuthorizationRepository $institutionAuthorizationRepository,
75
        IdentityRepository $identityRepository
76
    ) {
77
        $this->raCandidateRepository = $raCandidateRepository;
78
        $this->raListingRepository = $raListingRepository;
79
        $this->institutionAuthorizationRepository = $institutionAuthorizationRepository;
0 ignored issues
show
Documentation Bug introduced by
It seems like $institutionAuthorizationRepository of type object<Surfnet\StepupMid...uthorizationRepository> is incompatible with the declared type object<Surfnet\StepupMid...uthorizationRepository> of property $institutionAuthorizationRepository.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
80
        $this->identityRepository = $identityRepository;
81
    }
82
83
    /**
84
     * @param SecondFactorVettedEvent $event
85
     * @return void
86
     */
87
    public function applySecondFactorVettedEvent(SecondFactorVettedEvent $event)
88
    {
89
        $institutionAuthorizations = $this->institutionAuthorizationRepository
90
            ->findAuthorizationOptionsForInstitution(new ConfigurationInstitution($event->identityInstitution->getInstitution()));
91
92
        foreach ($institutionAuthorizations as $authorization) {
93
94
            $institution = new Institution($authorization->institution->getInstitution());
95
96
            if ($this->raListingRepository->findByIdentityIdAndInstitution($event->identityId, $institution)) {
97
                continue;
98
            }
99
100
            $candidate = RaCandidate::nominate(
101
                $event->identityId,
102
                $event->identityInstitution,
103
                $event->nameId,
104
                $event->commonName,
105
                $event->email,
106
                $institution
107
            );
108
109
            $this->raCandidateRepository->merge($candidate);
110
        }
111
    }
112
113
    /**
114
     * @param YubikeySecondFactorBootstrappedEvent $event
115
     * @return void
116
     */
117
    public function applyYubikeySecondFactorBootstrappedEvent(YubikeySecondFactorBootstrappedEvent $event)
118
    {
119
        $institutionAuthorizations = $this->institutionAuthorizationRepository
120
            ->findAuthorizationOptionsForInstitution(new ConfigurationInstitution($event->identityInstitution->getInstitution()));
121
122
        foreach ($institutionAuthorizations as $authorization) {
123
124
            $institution = new Institution($authorization->institution->getInstitution());
125
126
            $candidate = RaCandidate::nominate(
127
                $event->identityId,
128
                $event->identityInstitution,
129
                $event->nameId,
130
                $event->commonName,
131
                $event->email,
132
                $institution
133
            );
134
135
            $this->raCandidateRepository->merge($candidate);
136
        }
137
    }
138
139
    /**
140
     * @param VettedSecondFactorRevokedEvent $event
141
     * @return void
142
     */
143
    public function applyVettedSecondFactorRevokedEvent(VettedSecondFactorRevokedEvent $event)
144
    {
145
        $this->raCandidateRepository->removeByIdentityId($event->identityId);
146
    }
147
148
    /**
149
     * @param CompliedWithVettedSecondFactorRevocationEvent $event
150
     * @return void
151
     */
152
    public function applyCompliedWithVettedSecondFactorRevocationEvent(
153
        CompliedWithVettedSecondFactorRevocationEvent $event
154
    ) {
155
        $this->raCandidateRepository->removeByIdentityId($event->identityId);
156
    }
157
158
    /**
159
     * @param SraaUpdatedEvent $event
160
     *
161
     * Removes all RaCandidates that have a nameId matching an SRAA, as they cannot be made RA(A) as they
162
     * already are SRAA.
163
     */
164
    public function applySraaUpdatedEvent(SraaUpdatedEvent $event)
165
    {
166
        $this->raCandidateRepository->removeByNameIds($event->sraaList);
167
    }
168
169
    /**
170
     * @param IdentityAccreditedAsRaForInstitutionEvent $event
171
     * @return void
172
     */
173
    public function applyIdentityAccreditedAsRaForInstitutionEvent(IdentityAccreditedAsRaForInstitutionEvent $event)
174
    {
175
        $this->raCandidateRepository->removeByIdentityIdAndRaInstitution($event->identityId, $event->raInstitution);
176
    }
177
178
    /**
179
     * @param IdentityAccreditedAsRaaForInstitutionEvent $event
180
     * @return void
181
     */
182
    public function applyIdentityAccreditedAsRaaForInstitutionEvent(IdentityAccreditedAsRaaForInstitutionEvent $event)
183
    {
184
        $this->raCandidateRepository->removeByIdentityIdAndRaInstitution($event->identityId, $event->raInstitution);
185
    }
186
187
    /**
188
     * @param RegistrationAuthorityRetractedForInstitutionEvent $event
189
     * @return void
190
     */
191 View Code Duplication
    public function applyRegistrationAuthorityRetractedForInstitutionEvent(RegistrationAuthorityRetractedForInstitutionEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
192
    {
193
        $candidate = RaCandidate::nominate(
194
            $event->identityId,
195
            $event->identityInstitution,
196
            $event->nameId,
197
            $event->commonName,
198
            $event->email,
199
            $event->raInstitution
200
        );
201
202
        $this->raCandidateRepository->merge($candidate);
203
    }
204
205
    protected function applyIdentityForgottenEvent(IdentityForgottenEvent $event)
206
    {
207
        $this->raCandidateRepository->removeByIdentityId($event->identityId);
208
    }
209
210
    protected function applySelectRaaOptionChangedEvent(SelectRaaOptionChangedEvent $event)
211
    {
212
        $authorizedInstitutions = $event->selectRaaOption->getInstitutions($event->institution);
213
        $this->updateInstitutionCandidatesFromCollection(new Institution($event->institution->getInstitution()), $authorizedInstitutions);
214
    }
215
216
    protected function applyInstitutionConfigurationRemovedEvent(InstitutionConfigurationRemovedEvent $event)
217
    {
218
        $this->raCandidateRepository->removeByRaInstitution(new Institution($event->institution->getInstitution()));
219
    }
220
221
    /**
222
     * This method is kept to be backwards compatible for changes before FGA
223
     *
224
     * @param IdentityAccreditedAsRaEvent $event
225
     * @return void
226
     */
227
    public function applyIdentityAccreditedAsRaEvent(IdentityAccreditedAsRaEvent $event)
228
    {
229
        $this->raCandidateRepository->removeByIdentityIdAndRaInstitution($event->identityId, $event->identityInstitution);
230
    }
231
232
    /**
233
     * This method is kept to be backwards compatible for changes before FGA
234
     *
235
     * @param IdentityAccreditedAsRaaEvent $event
236
     * @return void
237
     */
238
    public function applyIdentityAccreditedAsRaaEvent(IdentityAccreditedAsRaaEvent $event)
239
    {
240
        $this->raCandidateRepository->removeByIdentityIdAndRaInstitution($event->identityId, $event->identityInstitution);
241
    }
242
243
    /**
244
     * This method is kept to be backwards compatible for changes before FGA
245
     *
246
     * @param RegistrationAuthorityRetractedEvent $event
247
     * @return void
248
     */
249 View Code Duplication
    public function applyRegistrationAuthorityRetractedEvent(RegistrationAuthorityRetractedEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
250
    {
251
        $candidate = RaCandidate::nominate(
252
            $event->identityId,
253
            $event->identityInstitution,
254
            $event->nameId,
255
            $event->commonName,
256
            $event->email,
257
            $event->raInstitution
0 ignored issues
show
Bug introduced by
The property raInstitution does not seem to exist in Surfnet\Stepup\Identity\...AuthorityRetractedEvent.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
258
        );
259
260
        $this->raCandidateRepository->merge($candidate);
261
    }
262
263
    /**
264
     * @param Institution $institution
265
     * @param ConfigurationInstitution[] $authorizedInstitutions
266
     * @throws \Doctrine\ORM\NonUniqueResultException
267
     */
268
    private function updateInstitutionCandidatesFromCollection(Institution $institution, array $authorizedInstitutions)
269
    {
270
271
        $raInstitutions = new InstitutionCollection();
272
        foreach ($authorizedInstitutions as $authorizedInstitution) {
273
            $raInstitutions->add(new Institution($authorizedInstitution->getInstitution()));
274
        }
275
276
        $this->raCandidateRepository->removeInstitutionsNotInList($institution, $raInstitutions);
277
278
        // loop through authorized institutions
279
        foreach ($raInstitutions as $raInstitution) {
280
281
            // add new identities
282
            $identities = $this->identityRepository->findByInstitution($raInstitution);
283
            foreach ($identities as $identity) {
284
                $identityId = new IdentityId($identity->id);
285
286
                // check if persistent in ra listing
287
                if ($this->raListingRepository->findByIdentityIdAndInstitution($identityId, $institution)) {
288
                    continue;
289
                }
290
291
                // create candidate if not exists
292
                $candidate = $this->raCandidateRepository->findByIdentityIdAndRaInstitution($identityId, $institution);
293
                if (!$candidate) {
294
                    $candidate = RaCandidate::nominate(
295
                        $identityId,
296
                        $identity->institution,
297
                        $identity->nameId,
298
                        $identity->commonName,
299
                        $identity->email,
300
                        $institution
301
                    );
302
                }
303
304
                // store
305
                $this->raCandidateRepository->merge($candidate);
306
            }
307
        }
308
    }
309
}
310