Completed
Push — release-1.x ( c1f7bf...3be149 )
by Boy
09:15 queued 05:00
created

U2fDevicePossessionProvenEvent   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 124
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
lcom 1
cbo 12
dl 124
loc 124
rs 10
c 1
b 0
f 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 21 21 1
A getAuditLogMetadata() 11 11 1
A deserialize() 14 14 1
A serialize() 11 11 1
A getSensitiveData() 7 7 1
A setSensitiveData() 6 6 1

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\Stepup\Identity\Event;
20
21
use Surfnet\Stepup\Identity\AuditLog\Metadata;
22
use Surfnet\Stepup\Identity\Value\CommonName;
23
use Surfnet\Stepup\Identity\Value\Email;
24
use Surfnet\Stepup\Identity\Value\EmailVerificationWindow;
25
use Surfnet\Stepup\Identity\Value\IdentityId;
26
use Surfnet\Stepup\Identity\Value\Institution;
27
use Surfnet\Stepup\Identity\Value\Locale;
28
use Surfnet\Stepup\Identity\Value\U2fKeyHandle;
29
use Surfnet\Stepup\Identity\Value\SecondFactorId;
30
use Surfnet\StepupBundle\Value\SecondFactorType;
31
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\Forgettable;
32
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\SensitiveData;
33
34 View Code Duplication
class U2fDevicePossessionProvenEvent extends IdentityEvent implements Forgettable
0 ignored issues
show
Duplication introduced by
This class 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...
35
{
36
    /**
37
     * @var \Surfnet\Stepup\Identity\Value\SecondFactorId
38
     */
39
    public $secondFactorId;
40
41
    /**
42
     * @var \Surfnet\Stepup\Identity\Value\U2fKeyHandle
43
     */
44
    public $keyHandle;
45
46
    /**
47
     * @var \Surfnet\Stepup\Identity\Value\EmailVerificationWindow
48
     */
49
    public $emailVerificationWindow;
50
51
    /**
52
     * @var string
53
     */
54
    public $emailVerificationNonce;
55
56
    /**
57
     * @var \Surfnet\Stepup\Identity\Value\CommonName
58
     */
59
    public $commonName;
60
61
    /**
62
     * @var \Surfnet\Stepup\Identity\Value\Email
63
     */
64
    public $email;
65
66
    /**
67
     * @var \Surfnet\Stepup\Identity\Value\Locale Eg. "en_GB"
68
     */
69
    public $preferredLocale;
70
71
    /**
72
     * @param IdentityId $identityId
73
     * @param Institution $identityInstitution
74
     * @param SecondFactorId $secondFactorId
75
     * @param U2fKeyHandle $keyHandle
76
     * @param EmailVerificationWindow $emailVerificationWindow
77
     * @param string $emailVerificationNonce
78
     * @param CommonName $commonName
79
     * @param Email $email
80
     * @param Locale $preferredLocale
81
     */
82
    public function __construct(
83
        IdentityId $identityId,
84
        Institution $identityInstitution,
85
        SecondFactorId $secondFactorId,
86
        U2fKeyHandle $keyHandle,
87
        EmailVerificationWindow $emailVerificationWindow,
88
        $emailVerificationNonce,
89
        CommonName $commonName,
90
        Email $email,
91
        Locale $preferredLocale
92
    ) {
93
        parent::__construct($identityId, $identityInstitution);
94
95
        $this->secondFactorId = $secondFactorId;
96
        $this->keyHandle = $keyHandle;
97
        $this->emailVerificationWindow = $emailVerificationWindow;
98
        $this->emailVerificationNonce = $emailVerificationNonce;
99
        $this->commonName = $commonName;
100
        $this->email = $email;
101
        $this->preferredLocale = $preferredLocale;
102
    }
103
104
    public function getAuditLogMetadata()
105
    {
106
        $metadata                         = new Metadata();
107
        $metadata->identityId             = $this->identityId;
108
        $metadata->identityInstitution    = $this->identityInstitution;
109
        $metadata->secondFactorId         = $this->secondFactorId;
110
        $metadata->secondFactorType       = new SecondFactorType('sms');
111
        $metadata->secondFactorIdentifier = $this->keyHandle;
112
113
        return $metadata;
114
    }
115
116
    public static function deserialize(array $data)
117
    {
118
        return new self(
119
            new IdentityId($data['identity_id']),
120
            new Institution($data['identity_institution']),
121
            new SecondFactorId($data['second_factor_id']),
122
            U2fKeyHandle::unknown(),
123
            EmailVerificationWindow::deserialize($data['email_verification_window']),
124
            $data['email_verification_nonce'],
125
            CommonName::unknown(),
126
            Email::unknown(),
127
            new Locale($data['preferred_locale'])
128
        );
129
    }
130
131
    public function serialize()
132
    {
133
        return [
134
            'identity_id'               => (string) $this->identityId,
135
            'identity_institution'      => (string) $this->identityInstitution,
136
            'second_factor_id'          => (string) $this->secondFactorId,
137
            'email_verification_window' => $this->emailVerificationWindow->serialize(),
138
            'email_verification_nonce'  => (string) $this->emailVerificationNonce,
139
            'preferred_locale'          => (string) $this->preferredLocale,
140
        ];
141
    }
142
143
    public function getSensitiveData()
144
    {
145
        return (new SensitiveData)
146
            ->withCommonName($this->commonName)
147
            ->withEmail($this->email)
148
            ->withSecondFactorIdentifier($this->keyHandle, new SecondFactorType('u2f'));
149
    }
150
151
    public function setSensitiveData(SensitiveData $sensitiveData)
152
    {
153
        $this->email       = $sensitiveData->getEmail();
154
        $this->commonName  = $sensitiveData->getCommonName();
155
        $this->keyHandle   = $sensitiveData->getSecondFactorIdentifier();
156
    }
157
}
158