Completed
Push — feature/verify-email-new-prove... ( 83179e )
by
unknown
10:19 queued 07:11
created

GssfPossessionProvenEvent::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
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\EmailVerificationWindow;
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 GssfPossessionProvenEvent extends IdentityEvent implements Forgettable
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 bool
54
     */
55
    public $emailVerificationRequired;
56
57
    /**
58
     * @var \Surfnet\Stepup\Identity\Value\EmailVerificationWindow
59
     */
60
    public $emailVerificationWindow;
61
62
    /**
63
     * @var string
64
     */
65
    public $emailVerificationNonce;
66
67
    /**
68
     * @var \Surfnet\Stepup\Identity\Value\CommonName
69
     */
70
    public $commonName;
71
72
    /**
73
     * @var \Surfnet\Stepup\Identity\Value\Email
74
     */
75
    public $email;
76
77
    /**
78
     * @var \Surfnet\Stepup\Identity\Value\Locale Eg. "en_GB"
79
     */
80
    public $preferredLocale;
81
82
    /**
83
     * @SuppressWarnings(PHPMD.ExcessiveParameterList)
84
     *
85
     * @param IdentityId              $identityId
86
     * @param Institution             $identityInstitution
87
     * @param SecondFactorId          $secondFactorId
88
     * @param StepupProvider          $stepupProvider
89
     * @param GssfId                  $gssfId
90
     * @param bool                    $emailVerificationRequired
91
     * @param EmailVerificationWindow $emailVerificationWindow
92
     * @param string                  $emailVerificationNonce
93
     * @param CommonName              $commonName
94
     * @param Email                   $email
95
     * @param Locale                  $preferredLocale
96
     */
97
    public function __construct(
98
        IdentityId $identityId,
99
        Institution $identityInstitution,
100
        SecondFactorId $secondFactorId,
101
        StepupProvider $stepupProvider,
102
        GssfId $gssfId,
103
        $emailVerificationRequired,
104
        EmailVerificationWindow $emailVerificationWindow,
105
        $emailVerificationNonce,
106
        CommonName $commonName,
107
        Email $email,
108
        Locale $preferredLocale
109
    ) {
110
        parent::__construct($identityId, $identityInstitution);
111
112
        $this->secondFactorId            = $secondFactorId;
113
        $this->stepupProvider            = $stepupProvider;
114
        $this->gssfId                    = $gssfId;
115
        $this->emailVerificationRequired = $emailVerificationRequired;
116
        $this->emailVerificationWindow   = $emailVerificationWindow;
117
        $this->emailVerificationNonce    = $emailVerificationNonce;
118
        $this->commonName                = $commonName;
119
        $this->email                     = $email;
120
        $this->preferredLocale           = $preferredLocale;
121
    }
122
123
    public function getAuditLogMetadata()
124
    {
125
        $metadata = new Metadata();
126
        $metadata->identityId = $this->identityId;
127
        $metadata->identityInstitution = $this->identityInstitution;
128
        $metadata->secondFactorId = $this->secondFactorId;
129
        $metadata->secondFactorType = new SecondFactorType((string) $this->stepupProvider);
130
        $metadata->secondFactorIdentifier = $this->gssfId;
131
132
        return $metadata;
133
    }
134
135
    public static function deserialize(array $data)
136
    {
137
        if (!isset($data['email_verification_required'])) {
138
            $data['email_verification_required'] = true;
139
        }
140
141
        return new self(
142
            new IdentityId($data['identity_id']),
143
            new Institution($data['identity_institution']),
144
            new SecondFactorId($data['second_factor_id']),
145
            new StepupProvider($data['stepup_provider']),
146
            GssfId::unknown(),
147
            $data['email_verification_required'],
148
            EmailVerificationWindow::deserialize($data['email_verification_window']),
149
            $data['email_verification_nonce'],
150
            CommonName::unknown(),
151
            Email::unknown(),
152
            new Locale($data['preferred_locale'])
153
        );
154
    }
155
156
    public function serialize()
157
    {
158
        return [
159
            'identity_id'                 => (string) $this->identityId,
160
            'identity_institution'        => (string) $this->identityInstitution,
161
            'second_factor_id'            => (string) $this->secondFactorId,
162
            'stepup_provider'             => (string) $this->stepupProvider,
163
            'email_verification_required' => (bool) $this->emailVerificationRequired,
164
            'email_verification_window'   => $this->emailVerificationWindow->serialize(),
165
            'email_verification_nonce'    => (string) $this->emailVerificationNonce,
166
            'preferred_locale'            => (string) $this->preferredLocale,
167
        ];
168
    }
169
170 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...
171
    {
172
        return (new SensitiveData)
173
            ->withCommonName($this->commonName)
174
            ->withEmail($this->email)
175
            ->withSecondFactorIdentifier($this->gssfId, new SecondFactorType((string) $this->stepupProvider));
176
    }
177
178
    public function setSensitiveData(SensitiveData $sensitiveData)
179
    {
180
        $this->email      = $sensitiveData->getEmail();
181
        $this->commonName = $sensitiveData->getCommonName();
182
        $this->gssfId     = $sensitiveData->getSecondFactorIdentifier();
183
    }
184
}
185