Completed
Push — feature/fix-ra-cerdential-endp... ( 9ea673 )
by
unknown
03:06
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[] $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
     * @param string $nameId
142
     * @param string $identityNameId
143
     * @return void
144
     */
145
    private static function assertEquals($nameId, $identityNameId)
146
    {
147
        Assertion::eq($nameId, $identityNameId);
148
    }
149
150
    /**
151
     * @return RegistrationAuthorityCredentials
152
     */
153
    public function grantSraa()
154
    {
155
        $copy = clone $this;
156
        $copy->isSraa = true;
157
158
        return $copy;
159
    }
160
161
    /**
162
     * @param RegistrationAuthorityCredentials $other
163
     * @return bool
164
     */
165
    public function equals(RegistrationAuthorityCredentials $other)
166
    {
167
        return $other->jsonSerialize() === $this->jsonSerialize();
168
    }
169
170
    public function jsonSerialize()
171
    {
172
        return [
173
            'id' => $this->identityId,
174
            'attributes' => [
175
                'institution'         => $this->institution,
176
                'common_name'         => $this->commonName,
177
                'location'            => $this->location,
178
                'contact_information' => $this->contactInformation,
179
                'is_ra'               => ($this->isRa || $this->isSraa),
180
                'is_raa'              => ($this->isRaa || $this->isSraa),
181
                'is_sraa'             => $this->isSraa,
182
            ]
183
        ];
184
    }
185
186
    /**
187
     * @return string
188
     */
189
    public function getIdentityId()
190
    {
191
        return $this->identityId;
192
    }
193
194
    /**
195
     * @return Institution
196
     */
197
    public function getInstitution()
198
    {
199
        return $this->institution;
200
    }
201
202
    /**
203
     * @return CommonName
204
     */
205
    public function getCommonName()
206
    {
207
        return $this->commonName;
208
    }
209
210
    /**
211
     * @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...
212
     */
213
    public function getLocation()
214
    {
215
        return $this->location;
216
    }
217
218
    /**
219
     * @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...
220
     */
221
    public function getContactInformation()
222
    {
223
        return $this->contactInformation;
224
    }
225
226
    /**
227
     * @return boolean
228
     */
229
    public function isRa()
230
    {
231
        return $this->isRa;
232
    }
233
234
    /**
235
     * @return boolean
236
     */
237
    public function isRaa()
238
    {
239
        return $this->isRaa;
240
    }
241
242
    /**
243
     * @return boolean
244
     */
245
    public function isSraa()
246
    {
247
        return $this->isSraa;
248
    }
249
}
250