__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

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;
0 ignored issues
show
Bug introduced by
The type Surfnet\Stepup\Identity\Value\Locale was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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\RightToObtainDataInterface;
34
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\SensitiveData;
35
36
class GssfPossessionProvenAndVerifiedEvent extends IdentityEvent implements
37
    Forgettable,
38
    PossessionProvenAndVerified,
39
    RightToObtainDataInterface
40
{
41
    /**
42
     * @var string[]
43
     */
44
    private array $allowlist = [
45
        'identity_id',
46
        'identity_institution',
47
        'stepup_provider',
48
        'registration_requested_at',
49
        'preferred_locale',
50
        'second_factor_identifier',
51
        'type',
52
        'common_name',
53
        'email',
54
    ];
55
56
    /**
57
     * @param IdentityId $identityId
58
     * @param Institution $identityInstitution
59
     * @param SecondFactorId $secondFactorId
60
     * @param StepupProvider $stepupProvider
61
     * @param GssfId $gssfId
62
     * @param CommonName $commonName
63
     * @param Email $email
64
     * @param Locale $preferredLocale
65
     * @param DateTime $registrationRequestedAt
66
     * @param string $registrationCode
67
     *
68
     * @SuppressWarnings("PHPMD.ExcessiveParameterList")
69
     */
70
    public function __construct(
71
        IdentityId     $identityId,
0 ignored issues
show
Coding Style introduced by
Expected 1 space between type hint and argument "$identityId"; 5 found
Loading history...
72
        Institution    $identityInstitution,
0 ignored issues
show
Coding Style introduced by
Expected 1 space between type hint and argument "$identityInstitution"; 4 found
Loading history...
73
        public SecondFactorId $secondFactorId,
74
        public StepupProvider $stepupProvider,
75
        public GssfId         $gssfId,
0 ignored issues
show
Coding Style introduced by
Expected 1 space between type hint and argument "$gssfId"; 9 found
Loading history...
76
        public CommonName     $commonName,
0 ignored issues
show
Coding Style introduced by
Expected 1 space between type hint and argument "$commonName"; 5 found
Loading history...
77
        public Email          $email,
0 ignored issues
show
Coding Style introduced by
Expected 1 space between type hint and argument "$email"; 10 found
Loading history...
78
        public Locale         $preferredLocale,
0 ignored issues
show
Coding Style introduced by
Expected 1 space between type hint and argument "$preferredLocale"; 9 found
Loading history...
79
        public DateTime       $registrationRequestedAt,
0 ignored issues
show
Coding Style introduced by
Expected 1 space between type hint and argument "$registrationRequestedAt"; 7 found
Loading history...
80
        public string  $registrationCode,
0 ignored issues
show
Coding Style introduced by
Expected 1 space between type hint and argument "$registrationCode"; 2 found
Loading history...
81
    ) {
82
        parent::__construct($identityId, $identityInstitution);
83
    }
84
85
    public function getAuditLogMetadata(): Metadata
86
    {
87
        $metadata = new Metadata();
88
        $metadata->identityId = $this->identityId;
89
        $metadata->identityInstitution = $this->identityInstitution;
90
        $metadata->secondFactorId = $this->secondFactorId;
91
        $metadata->secondFactorType = new SecondFactorType((string)$this->stepupProvider);
92
        $metadata->secondFactorIdentifier = $this->gssfId;
93
94
        return $metadata;
95
    }
96
97
    public static function deserialize(array $data): self
98
    {
99
        // BC compatibility for event replay in test-environment only (2.8.0, fixed in 2.8.1)
100
        if (!isset($data['preferred_locale'])) {
101
            $data['preferred_locale'] = 'en_GB';
102
        }
103
104
        return new self(
105
            new IdentityId($data['identity_id']),
106
            new Institution($data['identity_institution']),
107
            new SecondFactorId($data['second_factor_id']),
108
            new StepupProvider($data['stepup_provider']),
109
            GssfId::unknown(),
110
            CommonName::unknown(),
111
            Email::unknown(),
112
            new Locale($data['preferred_locale']),
113
            DateTime::fromString($data['registration_requested_at']),
114
            (string)$data['registration_code'],
115
        );
116
    }
117
118
    /**
119
     * The data ending up in the event_stream, be careful not to include sensitive data here!
120
     *
121
     * @return array<string, mixed>
122
     */
123
    public function serialize(): array
124
    {
125
        return [
126
            'identity_id' => (string)$this->identityId,
127
            'identity_institution' => (string)$this->identityInstitution,
128
            'second_factor_id' => (string)$this->secondFactorId,
129
            'stepup_provider' => (string)$this->stepupProvider,
130
            'registration_requested_at' => (string)$this->registrationRequestedAt,
131
            'registration_code' => $this->registrationCode,
132
            'preferred_locale' => (string)$this->preferredLocale,
133
        ];
134
    }
135
136
    public function getSensitiveData(): SensitiveData
137
    {
138
        return (new SensitiveData)
0 ignored issues
show
Coding Style introduced by
Parentheses must be used when instantiating a new class
Loading history...
139
            ->withCommonName($this->commonName)
140
            ->withEmail($this->email)
141
            ->withSecondFactorIdentifier($this->gssfId, new SecondFactorType((string)$this->stepupProvider));
142
    }
143
144
    public function setSensitiveData(SensitiveData $sensitiveData): void
145
    {
146
        $gssfId = $sensitiveData->getSecondFactorIdentifier();
147
        assert($gssfId instanceof GssfId);
148
        $this->gssfId = $gssfId;
149
        $this->email = $sensitiveData->getEmail();
150
        $this->commonName = $sensitiveData->getCommonName();
151
    }
152
153
    public function obtainUserData(): array
154
    {
155
        $serializedPublicUserData = $this->serialize();
156
        $serializedSensitiveUserData = $this->getSensitiveData()->serialize();
157
        return array_merge($serializedPublicUserData, $serializedSensitiveUserData);
158
    }
159
160
    /**
161
     * @return string[]
162
     */
163
    public function getAllowlist(): array
164
    {
165
        return $this->allowlist;
166
    }
167
}
168