Completed
Push — develop ( 8a6508...c3baa7 )
by Nic
18s
created

IdentityCommandHandler   C

Complexity

Total Complexity 18

Size/Duplication

Total Lines 270
Duplicated Lines 22.96 %

Coupling/Cohesion

Components 1
Dependencies 39

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 39
dl 62
loc 270
rs 5
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A handleCreateIdentityCommand() 0 16 1
A handleUpdateIdentityCommand() 0 10 1
B handleBootstrapIdentityWithYubikeySecondFactorCommand() 0 29 2
A handleProveYubikeyPossessionCommand() 15 15 1
A handleProvePhonePossessionCommand() 15 15 1
A handleProveGssfPossessionCommand() 17 17 1
A handleProveU2fDevicePossessionCommand() 15 15 1
A handleVerifyEmailCommand() 0 9 1
B handleVetSecondFactorCommand() 0 26 1
A handleRevokeOwnSecondFactorCommand() 0 8 1
A handleRevokeRegistrantsSecondFactorCommand() 0 11 1
A handleExpressLocalePreferenceCommand() 0 11 1
A assertIsValidLocale() 0 8 2
A assertSecondFactorIsAllowedFor() 0 14 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\CommandHandlingBundle\Identity\CommandHandler;
20
21
use Broadway\CommandHandling\CommandHandler;
22
use Broadway\Repository\RepositoryInterface;
23
use Surfnet\Stepup\Configuration\Value\Institution as ConfigurationInstitution;
24
use Surfnet\Stepup\Identity\Api\Identity as IdentityApi;
25
use Surfnet\Stepup\Identity\Entity\ConfigurableSettings;
26
use Surfnet\Stepup\Identity\Identity;
27
use Surfnet\Stepup\Identity\Value\CommonName;
28
use Surfnet\Stepup\Identity\Value\DocumentNumber;
29
use Surfnet\Stepup\Identity\Value\Email;
30
use Surfnet\Stepup\Identity\Value\GssfId;
31
use Surfnet\Stepup\Identity\Value\IdentityId;
32
use Surfnet\Stepup\Identity\Value\Institution;
33
use Surfnet\Stepup\Identity\Value\Locale;
34
use Surfnet\Stepup\Identity\Value\NameId;
35
use Surfnet\Stepup\Identity\Value\PhoneNumber;
36
use Surfnet\Stepup\Identity\Value\SecondFactorId;
37
use Surfnet\Stepup\Identity\Value\SecondFactorIdentifierFactory;
38
use Surfnet\Stepup\Identity\Value\StepupProvider;
39
use Surfnet\Stepup\Identity\Value\U2fKeyHandle;
40
use Surfnet\Stepup\Identity\Value\YubikeyPublicId;
41
use Surfnet\StepupBundle\Value\SecondFactorType;
42
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Service\AllowedSecondFactorListService;
43
use Surfnet\StepupMiddleware\ApiBundle\Identity\Repository\IdentityRepository;
44
use Surfnet\StepupMiddleware\CommandHandlingBundle\Exception\SecondFactorNotAllowedException;
45
use Surfnet\StepupMiddleware\CommandHandlingBundle\Exception\UnsupportedLocaleException;
46
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\BootstrapIdentityWithYubikeySecondFactorCommand;
47
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\CreateIdentityCommand;
48
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\ExpressLocalePreferenceCommand;
49
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\ProveGssfPossessionCommand;
50
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\ProvePhonePossessionCommand;
51
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\ProveU2fDevicePossessionCommand;
52
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\ProveYubikeyPossessionCommand;
53
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\RevokeOwnSecondFactorCommand;
54
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\RevokeRegistrantsSecondFactorCommand;
55
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\UpdateIdentityCommand;
56
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\VerifyEmailCommand;
57
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\VetSecondFactorCommand;
58
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\CommandHandler\Exception\DuplicateIdentityException;
59
60
/**
61
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
62
 * @SuppressWarnings(PHPMD.TooManyMethods)
63
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
64
 */
65
class IdentityCommandHandler extends CommandHandler
66
{
67
    /**
68
     * @var \Surfnet\Stepup\Identity\EventSourcing\IdentityRepository
69
     */
70
    private $eventSourcedRepository;
71
72
    /**
73
     * @var IdentityRepository
74
     */
75
    private $identityProjectionRepository;
76
77
    /**
78
     * @var \Surfnet\Stepup\Identity\Entity\ConfigurableSettings
79
     */
80
    private $configurableSettings;
81
82
    /**
83
     * @var AllowedSecondFactorListService
84
     */
85
    private $allowedSecondFactorListService;
86
87
    /**
88
     * @param RepositoryInterface            $eventSourcedRepository
89
     * @param IdentityRepository             $identityProjectionRepository
90
     * @param ConfigurableSettings           $configurableSettings
91
     * @param AllowedSecondFactorListService $allowedSecondFactorListService
92
     */
93
    public function __construct(
94
        RepositoryInterface $eventSourcedRepository,
95
        IdentityRepository $identityProjectionRepository,
96
        ConfigurableSettings $configurableSettings,
97
        AllowedSecondFactorListService $allowedSecondFactorListService
98
    ) {
99
        $this->eventSourcedRepository = $eventSourcedRepository;
0 ignored issues
show
Documentation Bug introduced by
$eventSourcedRepository is of type object<Broadway\Repository\RepositoryInterface>, but the property $eventSourcedRepository was declared to be of type object<Surfnet\Stepup\Id...ing\IdentityRepository>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
100
        $this->identityProjectionRepository = $identityProjectionRepository;
101
        $this->configurableSettings = $configurableSettings;
102
        $this->allowedSecondFactorListService = $allowedSecondFactorListService;
103
    }
104
105
    public function handleCreateIdentityCommand(CreateIdentityCommand $command)
106
    {
107
        $preferredLocale = new Locale($command->preferredLocale);
108
        $this->assertIsValidLocale($preferredLocale);
109
110
        $identity = Identity::create(
111
            new IdentityId($command->id),
112
            new Institution($command->institution),
113
            new NameId($command->nameId),
114
            new CommonName($command->commonName),
115
            new Email($command->email),
116
            $preferredLocale
117
        );
118
119
        $this->eventSourcedRepository->save($identity);
120
    }
121
122
    public function handleUpdateIdentityCommand(UpdateIdentityCommand $command)
123
    {
124
        /** @var IdentityApi $identity */
125
        $identity = $this->eventSourcedRepository->load(new IdentityId($command->id));
126
127
        $identity->rename(new CommonName($command->commonName));
128
        $identity->changeEmail(new Email($command->email));
129
130
        $this->eventSourcedRepository->save($identity);
131
    }
132
133
    public function handleBootstrapIdentityWithYubikeySecondFactorCommand(
134
        BootstrapIdentityWithYubikeySecondFactorCommand $command
135
    ) {
136
        $preferredLocale = new Locale($command->preferredLocale);
137
        $this->assertIsValidLocale($preferredLocale);
138
139
        $institution = new Institution($command->institution);
140
        $nameId = new NameId($command->nameId);
141
142
        if ($this->identityProjectionRepository->hasIdentityWithNameIdAndInstitution($nameId, $institution)) {
143
            throw DuplicateIdentityException::forBootstrappingWithYubikeySecondFactor($nameId, $institution);
144
        }
145
146
        $identity = Identity::create(
147
            new IdentityId($command->identityId),
148
            $institution,
149
            $nameId,
150
            new CommonName($command->commonName),
151
            new Email($command->email),
152
            $preferredLocale
153
        );
154
155
        $identity->bootstrapYubikeySecondFactor(
156
            new SecondFactorId($command->secondFactorId),
157
            new YubikeyPublicId($command->yubikeyPublicId)
158
        );
159
160
        $this->eventSourcedRepository->save($identity);
161
    }
162
163 View Code Duplication
    public function handleProveYubikeyPossessionCommand(ProveYubikeyPossessionCommand $command)
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
        /** @var IdentityApi $identity */
166
        $identity = $this->eventSourcedRepository->load(new IdentityId($command->identityId));
167
168
        $this->assertSecondFactorIsAllowedFor(new SecondFactorType('yubikey'), $identity->getInstitution());
169
170
        $identity->provePossessionOfYubikey(
171
            new SecondFactorId($command->secondFactorId),
172
            new YubikeyPublicId($command->yubikeyPublicId),
173
            $this->configurableSettings->createNewEmailVerificationWindow()
174
        );
175
176
        $this->eventSourcedRepository->save($identity);
177
    }
178
179
    /**
180
     * @param ProvePhonePossessionCommand $command
181
     */
182 View Code Duplication
    public function handleProvePhonePossessionCommand(ProvePhonePossessionCommand $command)
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...
183
    {
184
        /** @var IdentityApi $identity */
185
        $identity = $this->eventSourcedRepository->load(new IdentityId($command->identityId));
186
187
        $this->assertSecondFactorIsAllowedFor(new SecondFactorType('sms'), $identity->getInstitution());
188
189
        $identity->provePossessionOfPhone(
190
            new SecondFactorId($command->secondFactorId),
191
            new PhoneNumber($command->phoneNumber),
192
            $this->configurableSettings->createNewEmailVerificationWindow()
193
        );
194
195
        $this->eventSourcedRepository->save($identity);
196
    }
197
198
    /**
199
     * @param ProveGssfPossessionCommand $command
200
     */
201 View Code Duplication
    public function handleProveGssfPossessionCommand(ProveGssfPossessionCommand $command)
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...
202
    {
203
        /** @var IdentityApi $identity */
204
        $identity = $this->eventSourcedRepository->load(new IdentityId($command->identityId));
205
206
        // Assume tiqr is being used as it is the only GSSF currently supported
207
        $this->assertSecondFactorIsAllowedFor(new SecondFactorType('tiqr'), $identity->getInstitution());
208
209
        $identity->provePossessionOfGssf(
210
            new SecondFactorId($command->secondFactorId),
211
            new StepupProvider($command->stepupProvider),
212
            new GssfId($command->gssfId),
213
            $this->configurableSettings->createNewEmailVerificationWindow()
214
        );
215
216
        $this->eventSourcedRepository->save($identity);
217
    }
218
219 View Code Duplication
    public function handleProveU2fDevicePossessionCommand(ProveU2fDevicePossessionCommand $command)
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...
220
    {
221
        /** @var IdentityApi $identity */
222
        $identity = $this->eventSourcedRepository->load(new IdentityId($command->identityId));
223
224
        $this->assertSecondFactorIsAllowedFor(new SecondFactorType('u2f'), $identity->getInstitution());
225
226
        $identity->provePossessionOfU2fDevice(
227
            new SecondFactorId($command->secondFactorId),
228
            new U2fKeyHandle($command->keyHandle),
229
            $this->configurableSettings->createNewEmailVerificationWindow()
230
        );
231
232
        $this->eventSourcedRepository->save($identity);
233
    }
234
235
    /**
236
     * @param VerifyEmailCommand $command
237
     */
238
    public function handleVerifyEmailCommand(VerifyEmailCommand $command)
239
    {
240
        /** @var IdentityApi $identity */
241
        $identity = $this->eventSourcedRepository->load(new IdentityId($command->identityId));
242
243
        $identity->verifyEmail($command->verificationNonce);
244
245
        $this->eventSourcedRepository->save($identity);
246
    }
247
248
    public function handleVetSecondFactorCommand(VetSecondFactorCommand $command)
249
    {
250
        /** @var IdentityApi $authority */
251
        $authority = $this->eventSourcedRepository->load(new IdentityId($command->authorityId));
252
        /** @var IdentityApi $registrant */
253
        $registrant = $this->eventSourcedRepository->load(new IdentityId($command->identityId));
254
255
        $secondFactorType = new SecondFactorType($command->secondFactorType);
256
        $secondFactorIdentifier = SecondFactorIdentifierFactory::forType(
257
            $secondFactorType,
258
            $command->secondFactorIdentifier
259
        );
260
261
        $authority->vetSecondFactor(
262
            $registrant,
263
            new SecondFactorId($command->secondFactorId),
264
            $secondFactorType,
265
            $secondFactorIdentifier,
266
            $command->registrationCode,
267
            new DocumentNumber($command->documentNumber),
268
            $command->identityVerified
269
        );
270
271
        $this->eventSourcedRepository->save($authority);
272
        $this->eventSourcedRepository->save($registrant);
273
    }
274
275
    public function handleRevokeOwnSecondFactorCommand(RevokeOwnSecondFactorCommand $command)
276
    {
277
        /** @var IdentityApi $identity */
278
        $identity = $this->eventSourcedRepository->load(new IdentityId($command->identityId));
279
        $identity->revokeSecondFactor(new SecondFactorId($command->secondFactorId));
280
281
        $this->eventSourcedRepository->save($identity);
282
    }
283
284
    public function handleRevokeRegistrantsSecondFactorCommand(RevokeRegistrantsSecondFactorCommand $command)
285
    {
286
        /** @var IdentityApi $identity */
287
        $identity = $this->eventSourcedRepository->load(new IdentityId($command->identityId));
288
        $identity->complyWithSecondFactorRevocation(
289
            new SecondFactorId($command->secondFactorId),
290
            new IdentityId($command->authorityId)
291
        );
292
293
        $this->eventSourcedRepository->save($identity);
294
    }
295
296
    public function handleExpressLocalePreferenceCommand(ExpressLocalePreferenceCommand $command)
297
    {
298
        $preferredLocale = new Locale($command->preferredLocale);
299
        $this->assertIsValidLocale($preferredLocale);
300
301
        /** @var IdentityApi $identity */
302
        $identity = $this->eventSourcedRepository->load(new IdentityId($command->identityId));
303
        $identity->expressPreferredLocale($preferredLocale);
304
305
        $this->eventSourcedRepository->save($identity);
306
    }
307
308
    /**
309
     * @param Locale $locale
310
     */
311
    private function assertIsValidLocale(Locale $locale)
312
    {
313
        if (!$this->configurableSettings->isSupportedLocale($locale)) {
314
            throw new UnsupportedLocaleException(
315
                sprintf('Given locale "%s" is not a supported locale', (string) $locale)
316
            );
317
        }
318
    }
319
320
    private function assertSecondFactorIsAllowedFor(SecondFactorType $secondFactor, Institution $institution)
321
    {
322
        $allowedSecondFactorList = $this->allowedSecondFactorListService->getAllowedSecondFactorListFor(
323
            new ConfigurationInstitution($institution->getInstitution())
324
        );
325
326
        if (!$allowedSecondFactorList->allows($secondFactor)) {
327
            throw new SecondFactorNotAllowedException(sprintf(
328
                'Institution "%s" does not support second factor "%s"',
329
                $institution->getInstitution(),
330
                $secondFactor->getSecondFactorType()
331
            ));
332
        }
333
    }
334
}
335