SecondFactorVettedEvent::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 10
dl 0
loc 16
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 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\IdentityId;
25
use Surfnet\Stepup\Identity\Value\Institution;
26
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...
27
use Surfnet\Stepup\Identity\Value\NameId;
28
use Surfnet\Stepup\Identity\Value\SecondFactorId;
29
use Surfnet\Stepup\Identity\Value\SecondFactorIdentifier;
30
use Surfnet\Stepup\Identity\Value\SecondFactorIdentifierFactory;
31
use Surfnet\Stepup\Identity\Value\UnknownVettingType;
32
use Surfnet\Stepup\Identity\Value\VettingType;
33
use Surfnet\StepupBundle\Value\SecondFactorType;
34
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\Forgettable;
35
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\RightToObtainDataInterface;
36
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\SensitiveData;
37
38
/**
39
 * @SuppressWarnings("PHPMD.CouplingBetweenObjects")
40
 */
41
class SecondFactorVettedEvent extends IdentityEvent implements Forgettable, RightToObtainDataInterface
42
{
43
    /**
44
     * @var string[]
45
     */
46
    private array $allowlist = [
47
        'identity_id',
48
        'name_id',
49
        'identity_institution',
50
        'second_factor_id',
51
        'second_factor_type',
52
        'preferred_locale',
53
        'email',
54
        'common_name',
55
        'second_factor_identifier',
56
        'vetting_type',
57
    ];
58
59
    /**
60
     * @SuppressWarnings("PHPMD.ExcessiveParameterList")
61
     */
62
    public function __construct(
63
        IdentityId $identityId,
64
        public NameId $nameId,
65
        Institution $institution,
66
        public SecondFactorId $secondFactorId,
67
        public SecondFactorType $secondFactorType,
68
        public SecondFactorIdentifier $secondFactorIdentifier,
69
        public CommonName $commonName,
70
        public Email $email,
71
        /**
72
         * @var Locale Eg. "en_GB"
73
         */
74
        public Locale $preferredLocale,
75
        public ?VettingType $vettingType,
76
    ) {
77
        parent::__construct($identityId, $institution);
78
    }
79
80
    public function getAuditLogMetadata(): Metadata
81
    {
82
        $metadata = new Metadata();
83
        $metadata->identityId = $this->identityId;
84
        $metadata->identityInstitution = $this->identityInstitution;
85
        $metadata->secondFactorId = $this->secondFactorId;
86
        $metadata->secondFactorType = $this->secondFactorType;
87
        $metadata->secondFactorIdentifier = $this->secondFactorIdentifier;
88
        $metadata->vettingType = $this->vettingType;
89
90
        return $metadata;
91
    }
92
93
    public static function deserialize(array $data): self
94
    {
95
        $secondFactorType = new SecondFactorType($data['second_factor_type']);
96
        return new self(
97
            new IdentityId($data['identity_id']),
98
            new NameId($data['name_id']),
99
            new Institution($data['identity_institution']),
100
            new SecondFactorId($data['second_factor_id']),
101
            $secondFactorType,
102
            SecondFactorIdentifierFactory::unknownForType($secondFactorType),
103
            CommonName::unknown(),
104
            Email::unknown(),
105
            new Locale($data['preferred_locale']),
106
            new UnknownVettingType(),
107
        );
108
    }
109
110
    /**
111
     * The data ending up in the event_stream, be careful not to include sensitive data here!
112
     *
113
     * @return array<string, mixed>
114
     */
115
    public function serialize(): array
116
    {
117
        return [
118
            'identity_id' => (string)$this->identityId,
119
            'name_id' => (string)$this->nameId,
120
            'identity_institution' => (string)$this->identityInstitution,
121
            'second_factor_id' => (string)$this->secondFactorId,
122
            'second_factor_type' => (string)$this->secondFactorType,
123
            'preferred_locale' => (string)$this->preferredLocale,
124
        ];
125
    }
126
127
    public function getSensitiveData(): SensitiveData
128
    {
129
        return (new SensitiveData)
0 ignored issues
show
Coding Style introduced by
Parentheses must be used when instantiating a new class
Loading history...
130
            ->withCommonName($this->commonName)
131
            ->withEmail($this->email)
132
            ->withSecondFactorIdentifier($this->secondFactorIdentifier, $this->secondFactorType)
133
            ->withVettingType($this->vettingType);
0 ignored issues
show
Bug introduced by
It seems like $this->vettingType can also be of type null; however, parameter $vettingType of Surfnet\StepupMiddleware...Data::withVettingType() does only seem to accept Surfnet\Stepup\Identity\Value\VettingType, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

133
            ->withVettingType(/** @scrutinizer ignore-type */ $this->vettingType);
Loading history...
134
    }
135
136
    public function setSensitiveData(SensitiveData $sensitiveData): void
137
    {
138
        $this->email = $sensitiveData->getEmail();
139
        $this->commonName = $sensitiveData->getCommonName();
140
        $this->secondFactorIdentifier = $sensitiveData->getSecondFactorIdentifier();
141
        $this->vettingType = $sensitiveData->getVettingType();
142
    }
143
144
    public function obtainUserData(): array
145
    {
146
        $serializedPublicUserData = $this->serialize();
147
        $serializedSensitiveUserData = $this->getSensitiveData()->serialize();
148
        return array_merge($serializedPublicUserData, $serializedSensitiveUserData);
149
    }
150
151
    /**
152
     * @return string[]
153
     */
154
    public function getAllowlist(): array
155
    {
156
        return $this->allowlist;
157
    }
158
}
159