applyIdentityAccreditedAsRaEvent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 1
dl 0
loc 15
rs 9.9
c 0
b 0
f 0
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 Surfnet\Stepup\Identity\Event\AppointedAsRaaEvent;
22
use Surfnet\Stepup\Identity\Event\AppointedAsRaaForInstitutionEvent;
23
use Surfnet\Stepup\Identity\Event\AppointedAsRaEvent;
24
use Surfnet\Stepup\Identity\Event\AppointedAsRaForInstitutionEvent;
25
use Surfnet\Stepup\Identity\Event\IdentityAccreditedAsRaaEvent;
26
use Surfnet\Stepup\Identity\Event\IdentityAccreditedAsRaaForInstitutionEvent;
27
use Surfnet\Stepup\Identity\Event\IdentityAccreditedAsRaEvent;
28
use Surfnet\Stepup\Identity\Event\IdentityAccreditedAsRaForInstitutionEvent;
29
use Surfnet\Stepup\Identity\Event\IdentityForgottenEvent;
30
use Surfnet\Stepup\Identity\Event\RegistrationAuthorityInformationAmendedEvent;
31
use Surfnet\Stepup\Identity\Event\RegistrationAuthorityInformationAmendedForInstitutionEvent;
32
use Surfnet\Stepup\Identity\Event\RegistrationAuthorityRetractedEvent;
33
use Surfnet\Stepup\Identity\Event\RegistrationAuthorityRetractedForInstitutionEvent;
34
use Surfnet\Stepup\Projector\Projector;
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
    public function __construct(
48
        private readonly RaListingRepository $raListingRepository,
49
        private readonly IdentityRepository $identityRepository,
50
    ) {
51
    }
52
53
    public function applyIdentityAccreditedAsRaForInstitutionEvent(IdentityAccreditedAsRaForInstitutionEvent $event,): void
54
    {
55
        $identity = $this->identityRepository->find((string)$event->identityId);
56
57
        $raListing = RaListing::create(
58
            (string)$event->identityId,
59
            $event->identityInstitution,
60
            $identity->commonName,
61
            $identity->email,
62
            AuthorityRole::fromRegistrationAuthorityRole($event->registrationAuthorityRole),
63
            $event->location,
64
            $event->contactInformation,
65
            $event->raInstitution,
66
        );
67
68
        $this->raListingRepository->save($raListing);
69
    }
70
71
    public function applyIdentityAccreditedAsRaaForInstitutionEvent(IdentityAccreditedAsRaaForInstitutionEvent $event,): void
72
    {
73
        $identity = $this->identityRepository->find((string)$event->identityId);
74
75
        $raListing = RaListing::create(
76
            (string)$event->identityId,
77
            $event->identityInstitution,
78
            $identity->commonName,
79
            $identity->email,
80
            AuthorityRole::fromRegistrationAuthorityRole($event->registrationAuthorityRole),
81
            $event->location,
82
            $event->contactInformation,
83
            $event->raInstitution,
84
        );
85
86
        $this->raListingRepository->save($raListing);
87
    }
88
89
    public function applyRegistrationAuthorityInformationAmendedForInstitutionEvent(
90
        RegistrationAuthorityInformationAmendedForInstitutionEvent $event,
91
    ): void {
92
        $raListing = $this->raListingRepository->findByIdentityIdAndRaInstitution(
93
            $event->identityId,
94
            $event->raInstitution,
95
        );
96
97
        if (!$raListing instanceof RaListing) {
98
            throw new RuntimeException(
99
                "Tried to amend an RaListing's registration authority location and contact information, " .
100
                "but the listing could not be found",
101
            );
102
        }
103
104
        $raListing->location = $event->location;
105
        $raListing->contactInformation = $event->contactInformation;
106
107
        $this->raListingRepository->save($raListing);
108
    }
109
110
    public function applyAppointedAsRaForInstitutionEvent(AppointedAsRaForInstitutionEvent $event): void
111
    {
112
        $raListing = $this->raListingRepository->findByIdentityIdAndRaInstitution(
113
            $event->identityId,
114
            $event->raInstitution,
115
        );
116
117
        $raListing->role = AuthorityRole::ra();
118
119
        $this->raListingRepository->save($raListing);
0 ignored issues
show
Bug introduced by
It seems like $raListing can also be of type null; however, parameter $raListingEntry of Surfnet\StepupMiddleware...stingRepository::save() does only seem to accept Surfnet\StepupMiddleware...entity\Entity\RaListing, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

119
        $this->raListingRepository->save(/** @scrutinizer ignore-type */ $raListing);
Loading history...
120
    }
121
122
    public function applyAppointedAsRaaForInstitutionEvent(AppointedAsRaaForInstitutionEvent $event): void
123
    {
124
        $raListing = $this->raListingRepository->findByIdentityIdAndRaInstitution(
125
            $event->identityId,
126
            $event->raInstitution,
127
        );
128
129
        $raListing->role = AuthorityRole::raa();
130
131
        $this->raListingRepository->save($raListing);
0 ignored issues
show
Bug introduced by
It seems like $raListing can also be of type null; however, parameter $raListingEntry of Surfnet\StepupMiddleware...stingRepository::save() does only seem to accept Surfnet\StepupMiddleware...entity\Entity\RaListing, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

131
        $this->raListingRepository->save(/** @scrutinizer ignore-type */ $raListing);
Loading history...
132
    }
133
134
    public function applyRegistrationAuthorityRetractedForInstitutionEvent(
135
        RegistrationAuthorityRetractedForInstitutionEvent $event,
136
    ): void {
137
        $this->raListingRepository->removeByIdentityIdAndRaInstitution($event->identityId, $event->raInstitution);
138
    }
139
140
141
    protected function applyIdentityForgottenEvent(IdentityForgottenEvent $event): void
142
    {
143
        $this->raListingRepository->removeByIdentityId($event->identityId, $event->identityInstitution);
144
    }
145
146
    /**
147
     * This method is kept to be backwards compatible for changes before FGA
148
     *
149
     * @return void
150
     */
151
    public function applyIdentityAccreditedAsRaEvent(IdentityAccreditedAsRaEvent $event): void
152
    {
153
        $identity = $this->identityRepository->find((string)$event->identityId);
154
        $raListing = RaListing::create(
155
            (string)$event->identityId,
156
            $event->identityInstitution,
157
            $identity->commonName,
158
            $identity->email,
159
            AuthorityRole::fromRegistrationAuthorityRole($event->registrationAuthorityRole),
160
            $event->location,
161
            $event->contactInformation,
162
            $event->identityInstitution,
163
        );
164
165
        $this->raListingRepository->save($raListing);
166
    }
167
168
    /**
169
     * This method is kept to be backwards compatible for changes before FGA
170
     *
171
     * @return void
172
     */
173
    public function applyIdentityAccreditedAsRaaEvent(IdentityAccreditedAsRaaEvent $event): void
174
    {
175
        $identity = $this->identityRepository->find((string)$event->identityId);
176
        $raListing = RaListing::create(
177
            (string)$event->identityId,
178
            $event->identityInstitution,
179
            $identity->commonName,
180
            $identity->email,
181
            AuthorityRole::fromRegistrationAuthorityRole($event->registrationAuthorityRole),
182
            $event->location,
183
            $event->contactInformation,
184
            $event->identityInstitution,
185
        );
186
187
        $this->raListingRepository->save($raListing);
188
    }
189
190
    /**
191
     * This method is kept to be backwards compatible for changes before FGA
192
     */
193
    public function applyRegistrationAuthorityInformationAmendedEvent(
194
        RegistrationAuthorityInformationAmendedEvent $event,
195
    ): void {
196
        $raListing = $this->raListingRepository->findByIdentityIdAndRaInstitution(
197
            $event->identityId,
198
            $event->identityInstitution,
199
        );
200
201
        if (!$raListing instanceof RaListing) {
202
            throw new RuntimeException(
203
                "Tried to amend an RaListing's registration authority location and contact information, " .
204
                "but the listing could not be found",
205
            );
206
        }
207
208
        $raListing->location = $event->location;
209
        $raListing->contactInformation = $event->contactInformation;
210
211
        $this->raListingRepository->save($raListing);
212
    }
213
214
    /**
215
     * This method is kept to be backwards compatible for changes before FGA
216
     */
217
    public function applyAppointedAsRaEvent(AppointedAsRaEvent $event): void
218
    {
219
        $raListing = $this->raListingRepository->findByIdentityIdAndInstitution(
220
            $event->identityId,
221
            $event->identityInstitution,
222
        );
223
224
        foreach ($raListing as $listing) {
225
            $listing->role = AuthorityRole::ra();
226
            $this->raListingRepository->save($listing);
227
        }
228
    }
229
230
    /**
231
     * This method is kept to be backwards compatible for changes before FGA
232
     */
233
    public function applyAppointedAsRaaEvent(AppointedAsRaaEvent $event): void
234
    {
235
        $raListing = $this->raListingRepository->findByIdentityIdAndInstitution(
236
            $event->identityId,
237
            $event->identityInstitution,
238
        );
239
240
        foreach ($raListing as $listing) {
241
            $listing->role = AuthorityRole::raa();
242
            $this->raListingRepository->save($listing);
243
        }
244
    }
245
246
    /**
247
     * This method is kept to be backwards compatible for changes before FGA
248
     */
249
    public function applyRegistrationAuthorityRetractedEvent(RegistrationAuthorityRetractedEvent $event): void
250
    {
251
        $this->raListingRepository->removeByIdentityIdAndInstitution($event->identityId, $event->identityInstitution);
252
    }
253
}
254