Completed
Push — bugfix/fix-auth-filter ( 33070f...354f8d )
by
unknown
02:26
created

RegistrationAuthorityCredentials::isRa()   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 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\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 $isRaa;
61
62
    /**
63
     * @var bool
64
     */
65
    private $isSraa;
66
67
    /**
68
     * @param string $identityId
69
     * @param bool   $isRaa
70
     * @param bool   $isSraa
71
     */
72
    private function __construct(
73
        $identityId,
74
        $isRaa,
75
        $isSraa
76
    ) {
77
        $this->identityId = $identityId;
78
        $this->isRaa = $isRaa;
79
        $this->isSraa = $isSraa;
80
    }
81
82
    /**
83
     * @param Sraa     $sraa
84
     * @param Identity $identity
85
     * @return RegistrationAuthorityCredentials
86
     */
87
    public static function fromSraa(Sraa $sraa, Identity $identity)
88
    {
89
        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...
90
91
        $credentials = new self($identity->id, true, true);
92
        $credentials->commonName = $identity->commonName;
93
94
        return $credentials;
95
    }
96
97
    /**
98
     * @param RaListing $raListing
99
     * @return RegistrationAuthorityCredentials
100
     */
101
    public static function fromRaListing(RaListing $raListing)
102
    {
103
        $credentials = new self(
104
            $raListing->identityId,
105
            $raListing->role->equals(AuthorityRole::raa()),
106
            false
107
        );
108
109
        $credentials->institution        = $raListing->institution;
110
        $credentials->commonName         = $raListing->commonName;
111
        $credentials->location           = $raListing->location;
112
        $credentials->contactInformation = $raListing->contactInformation;
113
114
        return $credentials;
115
    }
116
117
    /**
118
     * @param string $nameId
119
     * @param string $identityNameId
120
     * @return void
121
     */
122
    private static function assertEquals($nameId, $identityNameId)
123
    {
124
        Assertion::eq($nameId, $identityNameId);
125
    }
126
127
    /**
128
     * @return RegistrationAuthorityCredentials
129
     */
130
    public function grantSraa()
131
    {
132
        $copy = clone $this;
133
        $copy->isSraa = true;
134
135
        return $copy;
136
    }
137
138
    /**
139
     * @param RegistrationAuthorityCredentials $other
140
     * @return bool
141
     */
142
    public function equals(RegistrationAuthorityCredentials $other)
143
    {
144
        return $other->jsonSerialize() === $this->jsonSerialize();
145
    }
146
147 View Code Duplication
    public function jsonSerialize()
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...
148
    {
149
        return [
150
            'id' => $this->identityId,
151
            'attributes' => [
152
                'institution'         => $this->institution,
153
                'common_name'         => $this->commonName,
154
                'location'            => $this->location,
155
                'contact_information' => $this->contactInformation,
156
                'is_raa'              => ($this->isRaa || $this->isSraa),
157
                'is_sraa'             => $this->isSraa,
158
            ]
159
        ];
160
    }
161
162
    /**
163
     * @return string
164
     */
165
    public function getIdentityId()
166
    {
167
        return $this->identityId;
168
    }
169
170
    /**
171
     * @return Institution
172
     */
173
    public function getInstitution()
174
    {
175
        return $this->institution;
176
    }
177
178
    /**
179
     * @return CommonName
180
     */
181
    public function getCommonName()
182
    {
183
        return $this->commonName;
184
    }
185
186
    /**
187
     * @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...
188
     */
189
    public function getLocation()
190
    {
191
        return $this->location;
192
    }
193
194
    /**
195
     * @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...
196
     */
197
    public function getContactInformation()
198
    {
199
        return $this->contactInformation;
200
    }
201
202
    /**
203
     * @return boolean
204
     */
205
    public function isRaa()
206
    {
207
        return $this->isRaa;
208
    }
209
210
    /**
211
     * @return boolean
212
     */
213
    public function isSraa()
214
    {
215
        return $this->isSraa;
216
    }
217
}
218