Completed
Push — master ( 5467e4...183ea4 )
by Boy
05:06 queued 01:02
created

IdentityCommandHandler   C

Complexity

Total Complexity 15

Size/Duplication

Total Lines 222
Duplicated Lines 17.57 %

Coupling/Cohesion

Components 1
Dependencies 33

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 15
c 5
b 0
f 1
lcom 1
cbo 33
dl 39
loc 222
rs 5

14 Methods

Rating   Name   Duplication   Size   Complexity  
A handleProveU2fDevicePossessionCommand() 13 13 1
B handleVetSecondFactorCommand() 0 26 1
A __construct() 0 5 1
A handleUpdateIdentityCommand() 0 10 1
A handleProveYubikeyPossessionCommand() 13 13 1
A handleProvePhonePossessionCommand() 13 13 1
A handleProveGssfPossessionCommand() 0 14 1
A handleVerifyEmailCommand() 0 9 1
A handleRevokeOwnSecondFactorCommand() 0 8 1
A handleRevokeRegistrantsSecondFactorCommand() 0 11 1
A handleCreateIdentityCommand() 0 16 1
A handleBootstrapIdentityWithYubikeySecondFactorCommand() 0 23 1
A handleExpressLocalePreferenceCommand() 0 11 1
A assertIsValidLocale() 0 8 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\Identity\Api\Identity as IdentityApi;
24
use Surfnet\Stepup\Identity\Entity\ConfigurableSettings;
25
use Surfnet\Stepup\Identity\Identity;
26
use Surfnet\Stepup\Identity\Value\CommonName;
27
use Surfnet\Stepup\Identity\Value\DocumentNumber;
28
use Surfnet\Stepup\Identity\Value\Email;
29
use Surfnet\Stepup\Identity\Value\GssfId;
30
use Surfnet\Stepup\Identity\Value\IdentityId;
31
use Surfnet\Stepup\Identity\Value\Institution;
32
use Surfnet\Stepup\Identity\Value\Locale;
33
use Surfnet\Stepup\Identity\Value\NameId;
34
use Surfnet\Stepup\Identity\Value\PhoneNumber;
35
use Surfnet\Stepup\Identity\Value\SecondFactorId;
36
use Surfnet\Stepup\Identity\Value\SecondFactorIdentifierFactory;
37
use Surfnet\Stepup\Identity\Value\StepupProvider;
38
use Surfnet\Stepup\Identity\Value\U2fKeyHandle;
39
use Surfnet\Stepup\Identity\Value\YubikeyPublicId;
40
use Surfnet\StepupBundle\Value\SecondFactorType;
41
use Surfnet\StepupMiddleware\CommandHandlingBundle\Exception\UnsupportedLocaleException;
42
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\BootstrapIdentityWithYubikeySecondFactorCommand;
43
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\CreateIdentityCommand;
44
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\ExpressLocalePreferenceCommand;
45
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\ProveGssfPossessionCommand;
46
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\ProvePhonePossessionCommand;
47
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\ProveU2fDevicePossessionCommand;
48
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\ProveYubikeyPossessionCommand;
49
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\RevokeOwnSecondFactorCommand;
50
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\RevokeRegistrantsSecondFactorCommand;
51
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\UpdateIdentityCommand;
52
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\VerifyEmailCommand;
53
use Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Command\VetSecondFactorCommand;
54
55
/**
56
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
57
 * @SuppressWarnings(PHPMD.TooManyMethods)
58
 * @SuppressWarnings(PHPMD.TooManyPublicMethods)
59
 */
60
class IdentityCommandHandler extends CommandHandler
61
{
62
    /**
63
     * @var \Surfnet\Stepup\Identity\EventSourcing\IdentityRepository
64
     */
65
    private $repository;
66
67
    /**
68
     * @var \Surfnet\Stepup\Identity\Entity\ConfigurableSettings
69
     */
70
    private $configurableSettings;
71
72
    /**
73
     * @param RepositoryInterface  $repository
74
     * @param ConfigurableSettings $configurableSettings
75
     */
76
    public function __construct(RepositoryInterface $repository, ConfigurableSettings $configurableSettings)
77
    {
78
        $this->repository           = $repository;
0 ignored issues
show
Documentation Bug introduced by
$repository is of type object<Broadway\Repository\RepositoryInterface>, but the property $repository 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...
79
        $this->configurableSettings = $configurableSettings;
80
    }
81
82
    public function handleCreateIdentityCommand(CreateIdentityCommand $command)
83
    {
84
        $preferredLocale = new Locale($command->preferredLocale);
85
        $this->assertIsValidLocale($preferredLocale);
86
87
        $identity = Identity::create(
88
            new IdentityId($command->id),
89
            new Institution($command->institution),
90
            new NameId($command->nameId),
91
            new CommonName($command->commonName),
92
            new Email($command->email),
93
            $preferredLocale
94
        );
95
96
        $this->repository->save($identity);
97
    }
98
99
    public function handleUpdateIdentityCommand(UpdateIdentityCommand $command)
100
    {
101
        /** @var IdentityApi $identity */
102
        $identity = $this->repository->load(new IdentityId($command->id));
103
104
        $identity->rename(new CommonName($command->commonName));
105
        $identity->changeEmail(new Email($command->email));
106
107
        $this->repository->save($identity);
108
    }
109
110
    public function handleBootstrapIdentityWithYubikeySecondFactorCommand(
111
        BootstrapIdentityWithYubikeySecondFactorCommand $command
112
    ) {
113
        $preferredLocale = new Locale($command->preferredLocale);
114
        $this->assertIsValidLocale($preferredLocale);
115
116
        // @todo add check if Identity does not already exist based on NameId
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
117
        $identity = Identity::create(
118
            new IdentityId($command->identityId),
119
            new Institution($command->institution),
120
            new NameId($command->nameId),
121
            new CommonName($command->commonName),
122
            new Email($command->email),
123
            $preferredLocale
124
        );
125
126
        $identity->bootstrapYubikeySecondFactor(
127
            new SecondFactorId($command->secondFactorId),
128
            new YubikeyPublicId($command->yubikeyPublicId)
129
        );
130
131
        $this->repository->save($identity);
132
    }
133
134 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...
135
    {
136
        /** @var IdentityApi $identity */
137
        $identity = $this->repository->load(new IdentityId($command->identityId));
138
139
        $identity->provePossessionOfYubikey(
140
            new SecondFactorId($command->secondFactorId),
141
            new YubikeyPublicId($command->yubikeyPublicId),
142
            $this->configurableSettings->createNewEmailVerificationWindow()
143
        );
144
145
        $this->repository->save($identity);
146
    }
147
148
    /**
149
     * @param ProvePhonePossessionCommand $command
150
     */
151 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...
152
    {
153
        /** @var IdentityApi $identity */
154
        $identity = $this->repository->load(new IdentityId($command->identityId));
155
156
        $identity->provePossessionOfPhone(
157
            new SecondFactorId($command->secondFactorId),
158
            new PhoneNumber($command->phoneNumber),
159
            $this->configurableSettings->createNewEmailVerificationWindow()
160
        );
161
162
        $this->repository->save($identity);
163
    }
164
165
    /**
166
     * @param ProveGssfPossessionCommand $command
167
     */
168
    public function handleProveGssfPossessionCommand(ProveGssfPossessionCommand $command)
169
    {
170
        /** @var IdentityApi $identity */
171
        $identity = $this->repository->load(new IdentityId($command->identityId));
172
173
        $identity->provePossessionOfGssf(
174
            new SecondFactorId($command->secondFactorId),
175
            new StepupProvider($command->stepupProvider),
176
            new GssfId($command->gssfId),
177
            $this->configurableSettings->createNewEmailVerificationWindow()
178
        );
179
180
        $this->repository->save($identity);
181
    }
182
183 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...
184
    {
185
        /** @var IdentityApi $identity */
186
        $identity = $this->repository->load(new IdentityId($command->identityId));
187
188
        $identity->provePossessionOfU2fDevice(
189
            new SecondFactorId($command->secondFactorId),
190
            new U2fKeyHandle($command->keyHandle),
191
            $this->configurableSettings->createNewEmailVerificationWindow()
192
        );
193
194
        $this->repository->save($identity);
195
    }
196
197
    /**
198
     * @param VerifyEmailCommand $command
199
     */
200
    public function handleVerifyEmailCommand(VerifyEmailCommand $command)
201
    {
202
        /** @var IdentityApi $identity */
203
        $identity = $this->repository->load(new IdentityId($command->identityId));
204
205
        $identity->verifyEmail($command->verificationNonce);
206
207
        $this->repository->save($identity);
208
    }
209
210
    public function handleVetSecondFactorCommand(VetSecondFactorCommand $command)
211
    {
212
        /** @var IdentityApi $authority */
213
        $authority = $this->repository->load(new IdentityId($command->authorityId));
214
        /** @var IdentityApi $registrant */
215
        $registrant = $this->repository->load(new IdentityId($command->identityId));
216
217
        $secondFactorType = new SecondFactorType($command->secondFactorType);
218
        $secondFactorIdentifier = SecondFactorIdentifierFactory::forType(
219
            $secondFactorType,
220
            $command->secondFactorIdentifier
221
        );
222
223
        $authority->vetSecondFactor(
224
            $registrant,
225
            new SecondFactorId($command->secondFactorId),
226
            $secondFactorType,
227
            $secondFactorIdentifier,
228
            $command->registrationCode,
229
            new DocumentNumber($command->documentNumber),
230
            $command->identityVerified
231
        );
232
233
        $this->repository->save($authority);
234
        $this->repository->save($registrant);
235
    }
236
237
    public function handleRevokeOwnSecondFactorCommand(RevokeOwnSecondFactorCommand $command)
238
    {
239
        /** @var IdentityApi $identity */
240
        $identity = $this->repository->load(new IdentityId($command->identityId));
241
        $identity->revokeSecondFactor(new SecondFactorId($command->secondFactorId));
242
243
        $this->repository->save($identity);
244
    }
245
246
    public function handleRevokeRegistrantsSecondFactorCommand(RevokeRegistrantsSecondFactorCommand $command)
247
    {
248
        /** @var IdentityApi $identity */
249
        $identity = $this->repository->load(new IdentityId($command->identityId));
250
        $identity->complyWithSecondFactorRevocation(
251
            new SecondFactorId($command->secondFactorId),
252
            new IdentityId($command->authorityId)
253
        );
254
255
        $this->repository->save($identity);
256
    }
257
258
    public function handleExpressLocalePreferenceCommand(ExpressLocalePreferenceCommand $command)
259
    {
260
        $preferredLocale = new Locale($command->preferredLocale);
261
        $this->assertIsValidLocale($preferredLocale);
262
263
        /** @var IdentityApi $identity */
264
        $identity = $this->repository->load(new IdentityId($command->identityId));
265
        $identity->expressPreferredLocale($preferredLocale);
266
267
        $this->repository->save($identity);
268
    }
269
270
    /**
271
     * @param Locale $locale
272
     */
273
    private function assertIsValidLocale(Locale $locale)
274
    {
275
        if (!$this->configurableSettings->isSupportedLocale($locale)) {
276
            throw new UnsupportedLocaleException(
277
                sprintf('Given locale "%s" is not a supported locale', (string) $locale)
278
            );
279
        }
280
    }
281
}
282