Completed
Pull Request — feature/fine-grained-authoriza... (#246)
by
unknown
56:03 queued 45:22
created

applyIdentityAccreditedAsRaForInstitutionEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
dl 17
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\Identity\Event\AppointedAsRaaEvent;
23
use Surfnet\Stepup\Identity\Event\AppointedAsRaEvent;
24
use Surfnet\Stepup\Identity\Event\AppointedAsRaaForInstitutionEvent;
25
use Surfnet\Stepup\Identity\Event\AppointedAsRaForInstitutionEvent;
26
use Surfnet\Stepup\Identity\Event\IdentityAccreditedAsRaaEvent;
27
use Surfnet\Stepup\Identity\Event\IdentityAccreditedAsRaaForInstitutionEvent;
28
use Surfnet\Stepup\Identity\Event\IdentityAccreditedAsRaEvent;
29
use Surfnet\Stepup\Identity\Event\IdentityAccreditedAsRaForInstitutionEvent;
30
use Surfnet\Stepup\Identity\Event\IdentityForgottenEvent;
31
use Surfnet\Stepup\Identity\Event\RegistrationAuthorityInformationAmendedEvent;
32
use Surfnet\Stepup\Identity\Event\RegistrationAuthorityInformationAmendedForInstitutionEvent;
33
use Surfnet\Stepup\Identity\Event\RegistrationAuthorityRetractedEvent;
34
use Surfnet\Stepup\Identity\Event\RegistrationAuthorityRetractedForInstitutionEvent;
35
use Surfnet\StepupMiddleware\ApiBundle\Exception\RuntimeException;
36
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaListing;
37
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\IdentityRepository;
38
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\RaListingRepository;
39
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\AuthorityRole;
40
41
/**
42
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects) - Events, events, events!
43
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
44
 */
45
class RaListingProjector extends Projector
46
{
47
    /**
48
     * @var RaListingRepository
49
     */
50
    private $raListingRepository;
51
52
    /**
53
     * @var IdentityRepository
54
     */
55
    private $identityRepository;
56
57
    public function __construct(RaListingRepository $raListingRepository, IdentityRepository $identityRepository)
58
    {
59
        $this->raListingRepository = $raListingRepository;
60
        $this->identityRepository = $identityRepository;
61
    }
62
63
    /**
64
     * @param IdentityAccreditedAsRaForInstitutionEvent $event
65
     * @return void
66
     */
67 View Code Duplication
    public function applyIdentityAccreditedAsRaForInstitutionEvent(IdentityAccreditedAsRaForInstitutionEvent $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...
68
    {
69
        $identity = $this->identityRepository->find((string) $event->identityId);
70
71
        $raListing = RaListing::create(
72
            (string) $event->identityId,
73
            $event->identityInstitution,
74
            $identity->commonName,
75
            $identity->email,
76
            AuthorityRole::fromRegistrationAuthorityRole($event->registrationAuthorityRole),
77
            $event->location,
78
            $event->contactInformation,
79
            $event->raInstitution
80
        );
81
82
        $this->raListingRepository->save($raListing);
83
    }
84
85
    /**
86
     * @param IdentityAccreditedAsRaaForInstitutionEvent $event
87
     * @return void
88
     */
89 View Code Duplication
    public function applyIdentityAccreditedAsRaaForInstitutionEvent(IdentityAccreditedAsRaaForInstitutionEvent $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...
90
    {
91
        $identity = $this->identityRepository->find((string) $event->identityId);
92
93
        $raListing = RaListing::create(
94
            (string) $event->identityId,
95
            $event->identityInstitution,
96
            $identity->commonName,
97
            $identity->email,
98
            AuthorityRole::fromRegistrationAuthorityRole($event->registrationAuthorityRole),
99
            $event->location,
100
            $event->contactInformation,
101
            $event->raInstitution
102
        );
103
104
        $this->raListingRepository->save($raListing);
105
    }
106
107
    public function applyRegistrationAuthorityInformationAmendedForInstitutionEvent(
108
        RegistrationAuthorityInformationAmendedForInstitutionEvent $event
109
    ) {
110
        /** @var RaListing $raListing */
111
        $raListing = $this->raListingRepository->findByIdentityIdAndRaInstitution($event->identityId, $event->raInstitution);
112
113
        if (!$raListing) {
114
            throw new RuntimeException(
115
                "Tried to amend an RaListing's registration authority location and contact information, " .
116
                "but the listing could not be found"
117
            );
118
        }
119
120
        $raListing->location = $event->location;
121
        $raListing->contactInformation = $event->contactInformation;
122
123
        $this->raListingRepository->save($raListing);
124
    }
125
126
    public function applyAppointedAsRaForInstitutionEvent(AppointedAsRaForInstitutionEvent $event)
127
    {
128
        /** @var RaListing $raListing */
129
        $raListing = $this->raListingRepository->findByIdentityIdAndRaInstitution($event->identityId, $event->raInstitution);
130
131
        $raListing->role = AuthorityRole::ra();
132
133
        $this->raListingRepository->save($raListing);
134
    }
135
136
    public function applyAppointedAsRaaForInstitutionEvent(AppointedAsRaaForInstitutionEvent $event)
137
    {
138
        /** @var RaListing $raListing */
139
        $raListing = $this->raListingRepository->findByIdentityIdAndRaInstitution($event->identityId, $event->raInstitution);
140
141
        $raListing->role = AuthorityRole::raa();
142
143
        $this->raListingRepository->save($raListing);
144
    }
145
146
    public function applyRegistrationAuthorityRetractedForInstitutionEvent(RegistrationAuthorityRetractedForInstitutionEvent $event)
147
    {
148
        $this->raListingRepository->removeByIdentityIdAndInstitution($event->identityId, $event->raInstitution);
149
    }
150
151
152
    protected function applyIdentityForgottenEvent(IdentityForgottenEvent $event)
153
    {
154
        $this->raListingRepository->removeByIdentityId($event->identityId);
155
    }
156
157
    /**
158
     * This method is kept to be backwards compatible for changes before FGA
159
     *
160
     * @param IdentityAccreditedAsRaEvent $event
161
     * @return void
162
     */
163 View Code Duplication
    public function applyIdentityAccreditedAsRaEvent(IdentityAccreditedAsRaEvent $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...
164
    {
165
        $identity = $this->identityRepository->find((string) $event->identityId);
166
167
        $raListing = RaListing::create(
168
            (string) $event->identityId,
169
            $event->identityInstitution,
170
            $identity->commonName,
171
            $identity->email,
172
            AuthorityRole::fromRegistrationAuthorityRole($event->registrationAuthorityRole),
173
            $event->location,
174
            $event->contactInformation,
175
            $event->identityInstitution
176
        );
177
178
        $this->raListingRepository->save($raListing);
179
    }
180
181
    /**
182
     * This method is kept to be backwards compatible for changes before FGA
183
     *
184
     * @param IdentityAccreditedAsRaaEvent $event
185
     * @return void
186
     */
187 View Code Duplication
    public function applyIdentityAccreditedAsRaaEvent(IdentityAccreditedAsRaaEvent $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...
188
    {
189
        $identity = $this->identityRepository->find((string) $event->identityId);
190
191
        $raListing = RaListing::create(
192
            (string) $event->identityId,
193
            $event->identityInstitution,
194
            $identity->commonName,
195
            $identity->email,
196
            AuthorityRole::fromRegistrationAuthorityRole($event->registrationAuthorityRole),
197
            $event->location,
198
            $event->contactInformation,
199
            $event->identityInstitution
200
        );
201
202
        $this->raListingRepository->save($raListing);
203
    }
204
205
    /**
206
     * This method is kept to be backwards compatible for changes before FGA
207
     *
208
     * @param RegistrationAuthorityInformationAmendedEvent $event
209
     */
210
    public function applyRegistrationAuthorityInformationAmendedEvent(
211
        RegistrationAuthorityInformationAmendedEvent $event
212
    ) {
213
        /** @var RaListing $raListing */
214
        $raListing = $this->raListingRepository->findByIdentityIdAndInstitution($event->identityId, $event->identityInstitution);
215
216
        if (!$raListing) {
217
            throw new RuntimeException(
218
                "Tried to amend an RaListing's registration authority location and contact information, " .
219
                "but the listing could not be found"
220
            );
221
        }
222
223
        $raListing->location = $event->location;
224
        $raListing->contactInformation = $event->contactInformation;
225
226
        $this->raListingRepository->save($raListing);
227
    }
228
229
    /**
230
     * This method is kept to be backwards compatible for changes before FGA
231
     *
232
     * @param AppointedAsRaEvent $event
233
     */
234
    public function applyAppointedAsRaEvent(AppointedAsRaEvent $event)
235
    {
236
        /** @var RaListing $raListing */
237
        $raListing = $this->raListingRepository->findByIdentityIdAndInstitution($event->identityId, $event->identityInstitution);
238
239
        $raListing->role = AuthorityRole::ra();
240
241
        $this->raListingRepository->save($raListing);
242
    }
243
244
    /**
245
     * This method is kept to be backwards compatible for changes before FGA
246
     *
247
     * @param AppointedAsRaaEvent $event
248
     */
249
    public function applyAppointedAsRaaEvent(AppointedAsRaaEvent $event)
250
    {
251
        /** @var RaListing $raListing */
252
        $raListing = $this->raListingRepository->findByIdentityIdAndInstitution($event->identityId, $event->identityInstitution);
253
254
        $raListing->role = AuthorityRole::raa();
255
256
        $this->raListingRepository->save($raListing);
257
    }
258
259
    /**
260
     * This method is kept to be backwards compatible for changes before FGA
261
     *
262
     * @param RegistrationAuthorityRetractedEvent $event
263
     */
264
    public function applyRegistrationAuthorityRetractedEvent(RegistrationAuthorityRetractedEvent $event)
265
    {
266
        $this->raListingRepository->removeByIdentityIdAndInstitution($event->identityId, $event->identityInstitution);
267
    }
268
}
269