Completed
Pull Request — develop (#223)
by
unknown
12:46 queued 04:45
created

__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 1
eloc 20
nc 1
nop 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * Copyright 2018 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\DateTime\DateTime;
22
use Surfnet\Stepup\Identity\AuditLog\Metadata;
23
use Surfnet\Stepup\Identity\Value\CommonName;
24
use Surfnet\Stepup\Identity\Value\Email;
25
use Surfnet\Stepup\Identity\Value\GssfId;
26
use Surfnet\Stepup\Identity\Value\IdentityId;
27
use Surfnet\Stepup\Identity\Value\Institution;
28
use Surfnet\Stepup\Identity\Value\Locale;
29
use Surfnet\Stepup\Identity\Value\SecondFactorId;
30
use Surfnet\Stepup\Identity\Value\StepupProvider;
31
use Surfnet\StepupBundle\Value\SecondFactorType;
32
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\Forgettable;
33
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\SensitiveData;
34
35
class GssfPossessionProvenAndVerifiedEvent extends IdentityEvent implements Forgettable, PossessionProvenAndVerified
36
{
37
    /**
38
     * @var \Surfnet\Stepup\Identity\Value\SecondFactorId
39
     */
40
    public $secondFactorId;
41
42
    /**
43
     * @var \Surfnet\Stepup\Identity\Value\StepupProvider
44
     */
45
    public $stepupProvider;
46
47
    /**
48
     * @var \Surfnet\Stepup\Identity\Value\GssfId
49
     */
50
    public $gssfId;
51
52
    /**
53
     * @var \Surfnet\Stepup\Identity\Value\CommonName
54
     */
55
    public $commonName;
56
57
    /**
58
     * @var \Surfnet\Stepup\Identity\Value\Email
59
     */
60
    public $email;
61
62
    /**
63
     * @var \Surfnet\Stepup\Identity\Value\Locale Eg. "en_GB"
64
     */
65
    public $preferredLocale;
66
67
    /**
68
     * @var \Surfnet\Stepup\DateTime\DateTime
69
     */
70
    public $registrationRequestedAt;
71
72
    /**
73
     * @var string
74
     */
75
    public $registrationCode;
76
77
    /**
78
     * @param IdentityId              $identityId
79
     * @param Institution             $identityInstitution
80
     * @param SecondFactorId          $secondFactorId
81
     * @param StepupProvider          $stepupProvider
82
     * @param GssfId                  $gssfId
83
     * @param CommonName              $commonName
84
     * @param Email                   $email
85
     * @param Locale                  $locale
86
     * @param DateTime                $registrationRequestedAt
87
     * @param string                  $registrationCode
88
     */
89
    public function __construct(
90
        IdentityId $identityId,
91
        Institution $identityInstitution,
92
        SecondFactorId $secondFactorId,
93
        StepupProvider $stepupProvider,
94
        GssfId $gssfId,
95
        CommonName $commonName,
96
        Email $email,
97
        Locale $locale,
98
        DateTime $registrationRequestedAt,
99
        $registrationCode
100
    ) {
101
        parent::__construct($identityId, $identityInstitution);
102
103
        $this->secondFactorId            = $secondFactorId;
104
        $this->stepupProvider            = $stepupProvider;
105
        $this->gssfId                    = $gssfId;
106
        $this->commonName                = $commonName;
107
        $this->email                     = $email;
108
        $this->preferredLocale           = $locale;
109
        $this->registrationRequestedAt   = $registrationRequestedAt;
110
        $this->registrationCode          = $registrationCode;
111
    }
112
113
    public function getAuditLogMetadata()
114
    {
115
        $metadata = new Metadata();
116
        $metadata->identityId = $this->identityId;
117
        $metadata->identityInstitution = $this->identityInstitution;
118
        $metadata->secondFactorId = $this->secondFactorId;
119
        $metadata->secondFactorType = new SecondFactorType((string) $this->stepupProvider);
120
        $metadata->secondFactorIdentifier = $this->gssfId;
121
122
        return $metadata;
123
    }
124
125
    public static function deserialize(array $data)
126
    {
127
        // BC compatibility for test-environment only (2.8.0, fixed in 2.8.1)
128
        if (!isset($data['preferred_locale'])) {
129
            $data['preferred_locale'] = 'en_GB';
130
        }
131
132
        return new self(
133
            new IdentityId($data['identity_id']),
134
            new Institution($data['identity_institution']),
135
            new SecondFactorId($data['second_factor_id']),
136
            new StepupProvider($data['stepup_provider']),
137
            GssfId::unknown(),
138
            CommonName::unknown(),
139
            Email::unknown(),
140
            new Locale($data['preferred_locale']),
141
            DateTime::fromString($data['registration_requested_at']),
142
            (string) $data['registration_code']
143
        );
144
    }
145
146
    public function serialize()
147
    {
148
        return [
149
            'identity_id'                 => (string) $this->identityId,
150
            'identity_institution'        => (string) $this->identityInstitution,
151
            'second_factor_id'            => (string) $this->secondFactorId,
152
            'stepup_provider'             => (string) $this->stepupProvider,
153
            'registration_requested_at'   => (string) $this->registrationRequestedAt,
154
            'registration_code'           => $this->registrationCode,
155
            'preferred_locale'            => (string) $this->preferredLocale,
156
        ];
157
    }
158
159 View Code Duplication
    public function getSensitiveData()
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...
160
    {
161
        return (new SensitiveData)
162
            ->withCommonName($this->commonName)
163
            ->withEmail($this->email)
164
            ->withSecondFactorIdentifier($this->gssfId, new SecondFactorType((string) $this->stepupProvider));
165
    }
166
167
    public function setSensitiveData(SensitiveData $sensitiveData)
168
    {
169
        $this->gssfId = $sensitiveData->getSecondFactorIdentifier();
170
        $this->email = $sensitiveData->getEmail();
171
        $this->commonName = $sensitiveData->getCommonName();
172
    }
173
}
174