Completed
Pull Request — develop (#260)
by Michiel
06:23 queued 02:49
created

RegistrationAuthorityCredentials::fromRaListing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
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\StepupMiddleware\ApiBundle\Identity\Value;
20
21
use Assert\Assertion;
22
use Surfnet\Stepup\Identity\Value\CommonName;
23
use Surfnet\Stepup\Identity\Value\ContactInformation;
24
use Surfnet\Stepup\Identity\Value\Institution;
25
use Surfnet\Stepup\Identity\Value\Location;
26
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\Identity;
27
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\RaListing;
28
use Surfnet\StepupMiddleware\ApiBundle\Identity\Entity\Sraa;
29
30
final class RegistrationAuthorityCredentials implements \JsonSerializable
31
{
32
    /**
33
     * @var string
34
     */
35
    private $identityId;
36
37
    /**
38
     * @var Institution
39
     */
40
    private $institution;
41
42
    /**
43
     * @var CommonName
44
     */
45
    private $commonName;
46
47
    /**
48
     * @var Location|null
49
     */
50
    private $location;
51
52
    /**
53
     * @var ContactInformation|null
54
     */
55
    private $contactInformation;
56
57
    /**
58
     * @var bool
59
     */
60
    private $isRa;
61
62
    /**
63
     * @var bool
64
     */
65
    private $isRaa;
66
67
    /**
68
     * @var bool
69
     */
70
    private $isSraa;
71
72
    /**
73
     * @param string $identityId
74
     * @param bool   $isRa
75
     * @param bool   $isRaa
76
     * @param bool   $isSraa
77
     */
78
    private function __construct(
79
        $identityId,
80
        $isRa,
81
        $isRaa,
82
        $isSraa
83
    ) {
84
        $this->identityId = $identityId;
85
        $this->isRa = $isRa;
86
        $this->isRaa = $isRaa;
87
        $this->isSraa = $isSraa;
88
    }
89
90
    /**
91
     * @param Sraa     $sraa
92
     * @param Identity $identity
93
     * @return RegistrationAuthorityCredentials
94
     */
95
    public static function fromSraa(Sraa $sraa, Identity $identity)
96
    {
97
        static::assertEquals($sraa->nameId, $identity->nameId);
0 ignored issues
show
Comprehensibility introduced by
Since Surfnet\StepupMiddleware...ionAuthorityCredentials is declared final, using late-static binding will have no effect. You might want to replace static with self instead.

Late static binding only has effect in subclasses. A final class cannot be extended anymore so late static binding cannot occurr. Consider replacing static:: with self::.

To learn more about late static binding, please refer to the PHP core documentation.

Loading history...
98
99
        $credentials = new self($identity->id, true, true, true);
100
        $credentials->commonName = $identity->commonName;
101
102
        return $credentials;
103
    }
104
105
    /**
106
     * @param RaListing $raListing
107
     * @return RegistrationAuthorityCredentials
108
     */
109
    public static function fromRaListing(RaListing $raListing)
110
    {
111
        $credentials = new self(
112
            $raListing->identityId,
113
            $raListing->role->equals(AuthorityRole::ra()),
114
            $raListing->role->equals(AuthorityRole::raa()),
115
            false
116
        );
117
118
        $credentials->institution        = $raListing->institution;
119
        $credentials->commonName         = $raListing->commonName;
120
        $credentials->location           = $raListing->location;
121
        $credentials->contactInformation = $raListing->contactInformation;
122
123
        return $credentials;
124
    }
125
126
    /**
127
     * @param string $nameId
128
     * @param string $identityNameId
129
     * @return void
130
     */
131
    private static function assertEquals($nameId, $identityNameId)
132
    {
133
        Assertion::eq($nameId, $identityNameId);
134
    }
135
136
    /**
137
     * @return RegistrationAuthorityCredentials
138
     */
139
    public function grantSraa()
140
    {
141
        $copy = clone $this;
142
        $copy->isSraa = true;
143
144
        return $copy;
145
    }
146
147
    /**
148
     * @param RegistrationAuthorityCredentials $other
149
     * @return bool
150
     */
151
    public function equals(RegistrationAuthorityCredentials $other)
152
    {
153
        return $other->jsonSerialize() === $this->jsonSerialize();
154
    }
155
156
    public function jsonSerialize()
157
    {
158
        return [
159
            'id' => $this->identityId,
160
            'attributes' => [
161
                'institution'         => $this->institution,
162
                'common_name'         => $this->commonName,
163
                'location'            => $this->location,
164
                'contact_information' => $this->contactInformation,
165
                'is_ra'               => ($this->isRa || $this->isSraa),
166
                'is_raa'              => ($this->isRaa || $this->isSraa),
167
                'is_sraa'             => $this->isSraa,
168
            ]
169
        ];
170
    }
171
172
    /**
173
     * @return string
174
     */
175
    public function getIdentityId()
176
    {
177
        return $this->identityId;
178
    }
179
180
    /**
181
     * @return Institution
182
     */
183
    public function getInstitution()
184
    {
185
        return $this->institution;
186
    }
187
188
    /**
189
     * @return CommonName
190
     */
191
    public function getCommonName()
192
    {
193
        return $this->commonName;
194
    }
195
196
    /**
197
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be Location|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...
198
     */
199
    public function getLocation()
200
    {
201
        return $this->location;
202
    }
203
204
    /**
205
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be ContactInformation|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...
206
     */
207
    public function getContactInformation()
208
    {
209
        return $this->contactInformation;
210
    }
211
212
    /**
213
     * @return boolean
214
     */
215
    public function isRa()
216
    {
217
        return $this->isRa;
218
    }
219
220
    /**
221
     * @return boolean
222
     */
223
    public function isRaa()
224
    {
225
        return $this->isRaa;
226
    }
227
228
    /**
229
     * @return boolean
230
     */
231
    public function isSraa()
232
    {
233
        return $this->isSraa;
234
    }
235
}
236