Completed
Push — feature/self-vet ( 66ee82...7edffb )
by Michiel
03:58
created

SecondFactorVettedEvent::deserialize()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 34

Duplication

Lines 8
Ratio 23.53 %

Importance

Changes 0
Metric Value
dl 8
loc 34
rs 8.4426
c 0
b 0
f 0
cc 7
nc 8
nop 1
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\DocumentNumber;
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;
28
use Surfnet\Stepup\Identity\Value\NameId;
29
use Surfnet\Stepup\Identity\Value\OnPremiseVettingType;
30
use Surfnet\Stepup\Identity\Value\SecondFactorId;
31
use Surfnet\Stepup\Identity\Value\SecondFactorIdentifier;
32
use Surfnet\Stepup\Identity\Value\SecondFactorIdentifierFactory;
33
use Surfnet\Stepup\Identity\Value\SelfVetVettingType;
34
use Surfnet\Stepup\Identity\Value\UnknownVettingType;
35
use Surfnet\Stepup\Identity\Value\VettingType;
36
use Surfnet\StepupBundle\Value\SecondFactorType;
37
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\Forgettable;
38
use Surfnet\StepupMiddleware\CommandHandlingBundle\SensitiveData\SensitiveData;
39
40
/**
41
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
42
 */
43
class SecondFactorVettedEvent extends IdentityEvent implements Forgettable
44
{
45
    /**
46
     * @var \Surfnet\Stepup\Identity\Value\NameId
47
     */
48
    public $nameId;
49
50
    /**
51
     * @var \Surfnet\Stepup\Identity\Value\SecondFactorId
52
     */
53
    public $secondFactorId;
54
55
    /**
56
     * @var \Surfnet\StepupBundle\Value\SecondFactorType
57
     */
58
    public $secondFactorType;
59
60
    /**
61
     * @var \Surfnet\Stepup\Identity\Value\SecondFactorIdentifier
62
     */
63
    public $secondFactorIdentifier;
64
65
    /**
66
     * @var \Surfnet\Stepup\Identity\Value\CommonName
67
     */
68
    public $commonName;
69
70
    /**
71
     * @var \Surfnet\Stepup\Identity\Value\Email
72
     */
73
    public $email;
74
75
    /**
76
     * @var \Surfnet\Stepup\Identity\Value\Locale Eg. "en_GB"
77
     */
78
    public $preferredLocale;
79
80
    /** @var VettingType */
81
    public $vettingType;
82
83
    /**
84
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
85
     */
86
    public function __construct(
87
        IdentityId $identityId,
88
        NameId $nameId,
89
        Institution $institution,
90
        SecondFactorId $secondFactorId,
91
        SecondFactorType $secondFactorType,
92
        SecondFactorIdentifier $secondFactorIdentifier,
93
        CommonName $commonName,
94
        Email $email,
95
        Locale $preferredLocale,
96
        VettingType $vettingType
97
    ) {
98
        parent::__construct($identityId, $institution);
99
100
        $this->nameId                 = $nameId;
101
        $this->secondFactorId         = $secondFactorId;
102
        $this->secondFactorType       = $secondFactorType;
103
        $this->secondFactorIdentifier = $secondFactorIdentifier;
104
        $this->commonName             = $commonName;
105
        $this->email                  = $email;
106
        $this->preferredLocale        = $preferredLocale;
107
        $this->vettingType = $vettingType;
108
    }
109
110
    public function getAuditLogMetadata()
111
    {
112
        $metadata                         = new Metadata();
113
        $metadata->identityId             = $this->identityId;
114
        $metadata->identityInstitution    = $this->identityInstitution;
115
        $metadata->secondFactorId         = $this->secondFactorId;
116
        $metadata->secondFactorType       = $this->secondFactorType;
117
        $metadata->secondFactorIdentifier = $this->secondFactorIdentifier;
118
        $metadata->vettingType = $this->vettingType;
119
120
        return $metadata;
121
    }
122
123
    public static function deserialize(array $data)
124
    {
125
        $secondFactorType = new SecondFactorType($data['second_factor_type']);
126
        $vettingType = new UnknownVettingType();
127
        if (isset($data['vetting_type'])) {
128 View Code Duplication
            switch ($data['vetting_type']['type']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
129
                case VettingType::TYPE_SELF_VET:
130
                    $vettingType = SelfVetVettingType::deserialize($data['vetting_type']);
131
                    break;
132
                case VettingType::TYPE_ON_PREMISE:
133
                    $vettingType = OnPremiseVettingType::deserialize($data['vetting_type']);
134
                    break;
135
            }
136
        }
137
        // BC fix for older events without a vetting type, they default back to ON_PREMISE.
138
        if ($vettingType instanceof UnknownVettingType &&
139
            isset($data['document_number']) &&
140
            $data['document_number'] !== null
141
        ) {
142
            $vettingType = new OnPremiseVettingType(new DocumentNumber($data['document_number']));
143
        }
144
        return new self(
145
            new IdentityId($data['identity_id']),
146
            new NameId($data['name_id']),
147
            new Institution($data['identity_institution']),
148
            new SecondFactorId($data['second_factor_id']),
149
            $secondFactorType,
150
            SecondFactorIdentifierFactory::unknownForType($secondFactorType),
151
            CommonName::unknown(),
152
            Email::unknown(),
153
            new Locale($data['preferred_locale']),
154
            $vettingType
155
        );
156
    }
157
158
    public function serialize(): array
159
    {
160
        return [
161
            'identity_id'              => (string) $this->identityId,
162
            'name_id'                  => (string) $this->nameId,
163
            'identity_institution'     => (string) $this->identityInstitution,
164
            'second_factor_id'         => (string) $this->secondFactorId,
165
            'second_factor_type'       => (string) $this->secondFactorType,
166
            'preferred_locale'         => (string) $this->preferredLocale,
167
            'vetting_type' => $this->vettingType->jsonSerialize(),
168
        ];
169
    }
170
171 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...
172
    {
173
        return (new SensitiveData)
174
            ->withCommonName($this->commonName)
175
            ->withEmail($this->email)
176
            ->withSecondFactorIdentifier($this->secondFactorIdentifier, $this->secondFactorType)
177
            ->withVettingType($this->vettingType);
178
    }
179
180 View Code Duplication
    public function setSensitiveData(SensitiveData $sensitiveData)
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...
181
    {
182
        $this->email          = $sensitiveData->getEmail();
183
        $this->commonName     = $sensitiveData->getCommonName();
184
        $this->secondFactorIdentifier = $sensitiveData->getSecondFactorIdentifier();
185
        $this->vettingType = $sensitiveData->getVettingType();
186
    }
187
}
188