Completed
Push — bugfix/fix-ra-listing ( 471828 )
by
unknown
02:47
created

RegistrationAuthorityCredentials::assertEquals()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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[] $raListings
107
     * @return RegistrationAuthorityCredentials
108
     */
109
    public static function fromRaListings(array $raListings)
110
    {
111
        $raListingCredentials = current($raListings);
112
        $isRa = false;
113
        $isRaa = false;
114
115
        foreach ($raListings as $raListing) {
116
            if ($raListing->role->equals(AuthorityRole::ra())) {
117
                $isRa = true;
118
            }
119
120
            if ($raListing->role->equals(AuthorityRole::raa())) {
121
                $isRaa = true;
122
            }
123
        }
124
125
        $credentials = new self(
126
            $raListingCredentials->identityId,
127
            $isRa,
128
            $isRaa,
129
            false
130
        );
131
132
        $credentials->institution = $raListingCredentials->institution;
133
        $credentials->commonName = $raListingCredentials->commonName;
134
        $credentials->location = $raListingCredentials->location;
135
        $credentials->contactInformation = $raListingCredentials->contactInformation;
136
137
        return $credentials;
138
    }
139
140
141
    /**
142
     * @param RaListing $raListing
143
     * @return RegistrationAuthorityCredentials
144
     */
145
    public static function fromRaListing(RaListing $raListing)
146
    {
147
        $credentials = new self(
148
            $raListing->identityId,
149
            $raListing->role->equals(AuthorityRole::ra()),
150
            $raListing->role->equals(AuthorityRole::raa()),
151
            false
152
        );
153
154
        $credentials->institution        = $raListing->institution;
155
        $credentials->commonName         = $raListing->commonName;
156
        $credentials->location           = $raListing->location;
157
        $credentials->contactInformation = $raListing->contactInformation;
158
159
        return $credentials;
160
    }
161
162
    /**
163
     * @param string $nameId
164
     * @param string $identityNameId
165
     * @return void
166
     */
167
    private static function assertEquals($nameId, $identityNameId)
168
    {
169
        Assertion::eq($nameId, $identityNameId);
170
    }
171
172
    /**
173
     * @return RegistrationAuthorityCredentials
174
     */
175
    public function grantSraa()
176
    {
177
        $copy = clone $this;
178
        $copy->isSraa = true;
179
180
        return $copy;
181
    }
182
183
    /**
184
     * @param RegistrationAuthorityCredentials $other
185
     * @return bool
186
     */
187
    public function equals(RegistrationAuthorityCredentials $other)
188
    {
189
        return $other->jsonSerialize() === $this->jsonSerialize();
190
    }
191
192
    public function jsonSerialize()
193
    {
194
        return [
195
            'id' => $this->identityId,
196
            'attributes' => [
197
                'institution'         => $this->institution,
198
                'common_name'         => $this->commonName,
199
                'location'            => $this->location,
200
                'contact_information' => $this->contactInformation,
201
                'is_ra'               => ($this->isRa || $this->isSraa),
202
                'is_raa'              => ($this->isRaa || $this->isSraa),
203
                'is_sraa'             => $this->isSraa,
204
            ]
205
        ];
206
    }
207
208
    /**
209
     * @return string
210
     */
211
    public function getIdentityId()
212
    {
213
        return $this->identityId;
214
    }
215
216
    /**
217
     * @return Institution
218
     */
219
    public function getInstitution()
220
    {
221
        return $this->institution;
222
    }
223
224
    /**
225
     * @return CommonName
226
     */
227
    public function getCommonName()
228
    {
229
        return $this->commonName;
230
    }
231
232
    /**
233
     * @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...
234
     */
235
    public function getLocation()
236
    {
237
        return $this->location;
238
    }
239
240
    /**
241
     * @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...
242
     */
243
    public function getContactInformation()
244
    {
245
        return $this->contactInformation;
246
    }
247
248
    /**
249
     * @return boolean
250
     */
251
    public function isRa()
252
    {
253
        return $this->isRa;
254
    }
255
256
    /**
257
     * @return boolean
258
     */
259
    public function isRaa()
260
    {
261
        return $this->isRaa;
262
    }
263
264
    /**
265
     * @return boolean
266
     */
267
    public function isSraa()
268
    {
269
        return $this->isSraa;
270
    }
271
}
272