Completed
Pull Request — develop (#225)
by
unknown
04:56 queued 02:32
created

RemoteVettingProcessDto::deserialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Copyright 2010 SURFnet B.V.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
namespace Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\Dto;
19
20
use Serializable;
21
use Surfnet\StepupSelfService\SelfServiceBundle\Assert;
22
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\State\RemoteVettingState;
23
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\State\RemoteVettingStateInitialised;
24
use Surfnet\StepupSelfService\SelfServiceBundle\Service\RemoteVetting\Value\ProcessId;
25
26
class RemoteVettingProcessDto implements Serializable
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
27
{
28
    /**
29
     * @var ProcessId
30
     */
31
    private $processId;
32
    /**
33
     * @var RemoteVettingTokenDto
34
     */
35
    private $token;
36
    /**
37
     * @var RemoteVettingState|null
38
     */
39
    private $state = null;
40
    /**
41
     * @var AttributeListDto
42
     */
43
    private $attributes;
44
    /**
45
     * @var string
46
     */
47
    private $identityProviderName;
48
49
    /**
50
     * @param ProcessId $processId
51
     * @param RemoteVettingTokenDto $token
52
     * @param string $identityProviderName
53
     * @return RemoteVettingProcessDto
54
     * @throws \Assert\AssertionFailedException
55
     */
56
    public static function create(ProcessId $processId, RemoteVettingTokenDto $token, $identityProviderName)
57
    {
58
        return new self($processId, $token, new RemoteVettingStateInitialised(), AttributeListDto::notSet(), $identityProviderName);
59
    }
60
61
    /**
62
     * @param string $serialized
63
     * @return RemoteVettingProcessDto
64
     */
65
    public static function deserialize($serialized)
66
    {
67
        $instance = new self(
68
            ProcessId::notSet(),
69
            RemoteVettingTokenDto::notSet(),
70
            new RemoteVettingStateInitialised(),
71
            AttributeListDto::notSet(),
72
            ''
73
        );
74
        $instance->unserialize($serialized);
75
        return $instance;
76
    }
77
78
    /**
79
     * @param RemoteVettingProcessDto $process
80
     * @param RemoteVettingState $state
81
     * @return RemoteVettingProcessDto
82
     */
83
    public static function updateState(RemoteVettingProcessDto $process, RemoteVettingState $state)
84
    {
85
        return new self($process->getProcessId(), $process->getToken(), $state, $process->getAttributes(), $process->getIdentityProviderName());
86
    }
87
88
    /**
89
     * @param ProcessId $processId
90
     * @param RemoteVettingTokenDto $token
91
     * @param RemoteVettingState $state
92
     * @param AttributeListDto $attributes
93
     * @param string $identityProviderName
94
     * @throws \Assert\AssertionFailedException
95
     */
96
    private function __construct(
97
        ProcessId $processId,
98
        RemoteVettingTokenDto $token,
99
        RemoteVettingState $state,
100
        AttributeListDto $attributes,
101
        $identityProviderName
102
    ) {
103
        Assert::string($identityProviderName, 'The $identityProviderName in an RemoteVettingProcessDto must be a string value');
104
105
        $this->processId = $processId;
106
        $this->token = $token;
107
        $this->state = $state;
108
        $this->attributes = $attributes;
109
        $this->identityProviderName = $identityProviderName;
110
    }
111
112
    /**
113
     * @return ProcessId
114
     */
115
    public function getProcessId()
116
    {
117
        return $this->processId;
118
    }
119
120
    /**
121
     * @return RemoteVettingTokenDto
122
     */
123
    public function getToken()
124
    {
125
        return $this->token;
126
    }
127
128
    /**
129
     * @return RemoteVettingState
0 ignored issues
show
Documentation introduced by
Should the return type not be RemoteVettingState|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
130
     */
131
    public function getState()
132
    {
133
        return $this->state;
134
    }
135
136
    /**
137
     * @return AttributeListDto
138
     */
139
    public function getAttributes()
140
    {
141
        return $this->attributes;
142
    }
143
144
    /**
145
     * @param AttributeListDto $attributes
146
     */
147
    public function setAttributes($attributes)
148
    {
149
        $this->attributes = $attributes;
150
    }
151
152
    /**
153
     * @return string
154
     */
155
    public function getIdentityProviderName()
156
    {
157
        return $this->identityProviderName;
158
    }
159
160
    /**
161
     * @inheritDoc
162
     */
163
    public function serialize()
164
    {
165
        $stateClass = !is_null($this->state) ? get_class($this->state) : null;
166
167
        $data = [
168
            'processId' => json_encode($this->processId->getProcessId()),
169
            'token' => $this->token->serialize(),
170
            'state' => json_encode($stateClass),
171
            'attributes' => $this->attributes->serialize(),
172
            'identityProvider' => json_encode($this->identityProviderName),
173
        ];
174
175
        $params = [];
176
        foreach ($data as $key => $value) {
177
            $params[] = json_encode($key).":{$value}";
178
        }
179
180
        return '{'.implode(',', $params).'}';
181
    }
182
183
    /**
184
     * @inheritDoc
185
     */
186
    public function unserialize($serialized)
187
    {
188
        $data = json_decode($serialized, true);
189
190
        $stateClass = !is_null($data['state']) ? new $data['state']() : null;
191
192
        $this->processId = ProcessId::create($data['processId']);
193
        $this->token = RemoteVettingTokenDto::deserialize(json_encode($data['token']));
194
        $this->state = $stateClass;
195
        $this->attributes = AttributeListDto::deserialize(json_encode($data['attributes']));
196
        $this->identityProviderName = $data['identityProvider'];
197
    }
198
}
199