deserialize()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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