Completed
Push — develop ( 4b76b9...136345 )
by
unknown
07:15 queued 01:31
created

GssfPossessionProvenAndVerifiedEvent::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 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\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
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
90
     */
91
    public function __construct(
92
        IdentityId $identityId,
93
        Institution $identityInstitution,
94
        SecondFactorId $secondFactorId,
95
        StepupProvider $stepupProvider,
96
        GssfId $gssfId,
97
        CommonName $commonName,
98
        Email $email,
99
        Locale $locale,
100
        DateTime $registrationRequestedAt,
101
        $registrationCode
102
    ) {
103
        parent::__construct($identityId, $identityInstitution);
104
105
        $this->secondFactorId            = $secondFactorId;
106
        $this->stepupProvider            = $stepupProvider;
107
        $this->gssfId                    = $gssfId;
108
        $this->commonName                = $commonName;
109
        $this->email                     = $email;
110
        $this->preferredLocale           = $locale;
111
        $this->registrationRequestedAt   = $registrationRequestedAt;
112
        $this->registrationCode          = $registrationCode;
113
    }
114
115
    public function getAuditLogMetadata()
116
    {
117
        $metadata = new Metadata();
118
        $metadata->identityId = $this->identityId;
119
        $metadata->identityInstitution = $this->identityInstitution;
120
        $metadata->secondFactorId = $this->secondFactorId;
121
        $metadata->secondFactorType = new SecondFactorType((string) $this->stepupProvider);
122
        $metadata->secondFactorIdentifier = $this->gssfId;
123
124
        return $metadata;
125
    }
126
127
    public static function deserialize(array $data)
128
    {
129
        // BC compatibility for event replay in test-environment only (2.8.0, fixed in 2.8.1)
130
        if (!isset($data['preferred_locale'])) {
131
            $data['preferred_locale'] = 'en_GB';
132
        }
133
134
        return new self(
135
            new IdentityId($data['identity_id']),
136
            new Institution($data['identity_institution']),
137
            new SecondFactorId($data['second_factor_id']),
138
            new StepupProvider($data['stepup_provider']),
139
            GssfId::unknown(),
140
            CommonName::unknown(),
141
            Email::unknown(),
142
            new Locale($data['preferred_locale']),
143
            DateTime::fromString($data['registration_requested_at']),
144
            (string) $data['registration_code']
145
        );
146
    }
147
148
    public function serialize()
149
    {
150
        return [
151
            'identity_id'                 => (string) $this->identityId,
152
            'identity_institution'        => (string) $this->identityInstitution,
153
            'second_factor_id'            => (string) $this->secondFactorId,
154
            'stepup_provider'             => (string) $this->stepupProvider,
155
            'registration_requested_at'   => (string) $this->registrationRequestedAt,
156
            'registration_code'           => $this->registrationCode,
157
            'preferred_locale'            => (string) $this->preferredLocale,
158
        ];
159
    }
160
161 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...
162
    {
163
        return (new SensitiveData)
164
            ->withCommonName($this->commonName)
165
            ->withEmail($this->email)
166
            ->withSecondFactorIdentifier($this->gssfId, new SecondFactorType((string) $this->stepupProvider));
167
    }
168
169
    public function setSensitiveData(SensitiveData $sensitiveData)
170
    {
171
        $this->gssfId = $sensitiveData->getSecondFactorIdentifier();
172
        $this->email = $sensitiveData->getEmail();
173
        $this->commonName = $sensitiveData->getCommonName();
174
    }
175
}
176