Completed
Push — feature/add-skip-vetting-endpo... ( 09cafc...ff51c5 )
by
unknown
01:37
created

SecondFactorService   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 244
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 9
dl 0
loc 244
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getUnverified() 0 20 3
A getVerified() 0 20 3
A getVerifiedSkipVetting() 0 10 2
A getVetted() 0 20 3
A searchUnverified() 0 20 3
A searchVerified() 0 20 3
A searchOwnVerified() 0 20 3
A searchVetted() 0 20 3
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\StepupMiddlewareClientBundle\Identity\Service;
20
21
use Surfnet\StepupMiddlewareClient\Exception\AccessDeniedToResourceException;
22
use Surfnet\StepupMiddlewareClient\Exception\MalformedResponseException;
23
use Surfnet\StepupMiddlewareClient\Exception\ResourceReadException;
24
use Surfnet\StepupMiddlewareClient\Identity\Dto\UnverifiedSecondFactorSearchQuery;
25
use Surfnet\StepupMiddlewareClient\Identity\Dto\VerifiedSecondFactorOfIdentitySearchQuery;
26
use Surfnet\StepupMiddlewareClient\Identity\Dto\VerifiedSecondFactorSearchQuery;
27
use Surfnet\StepupMiddlewareClient\Identity\Dto\VettedSecondFactorSearchQuery;
28
use Surfnet\StepupMiddlewareClient\Identity\Service\SecondFactorService as LibrarySecondFactorService;
29
use Surfnet\StepupMiddlewareClientBundle\Exception\InvalidResponseException;
30
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\UnverifiedSecondFactor;
31
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\UnverifiedSecondFactorCollection;
32
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\VerifiedSecondFactor;
33
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\VerifiedSecondFactorCollection;
34
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\VettedSecondFactor;
35
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\VettedSecondFactorCollection;
36
use Symfony\Component\Validator\Validator\ValidatorInterface;
37
38
/**
39
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
40
 */
41
class SecondFactorService
42
{
43
    /**
44
     * @var LibrarySecondFactorService
45
     */
46
    private $service;
47
48
    /**
49
     * @var ValidatorInterface
50
     */
51
    private $validator;
52
53
    /**
54
     * @param LibrarySecondFactorService $service
55
     * @param ValidatorInterface $validator
56
     */
57
    public function __construct(LibrarySecondFactorService $service, ValidatorInterface $validator)
58
    {
59
        $this->service = $service;
60
        $this->validator = $validator;
61
    }
62
63
    /**
64
     * @param string $secondFactorId
65
     * @return UnverifiedSecondFactor|null
66
     * @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource.
67
     * @throws InvalidResponseException When the API responded with invalid data.
68
     * @throws ResourceReadException When the API doesn't respond with the resource.
69
     * @throws MalformedResponseException When the API doesn't respond with a proper response.
70
     */
71
    public function getUnverified($secondFactorId)
72
    {
73
        $data = $this->service->getUnverified($secondFactorId);
74
75
        if ($data === null) {
76
            return null;
77
        }
78
79
        $secondFactor = UnverifiedSecondFactor::fromData($data);
80
        $violations = $this->validator->validate($secondFactor);
81
82
        if (count($violations) > 0) {
83
            throw InvalidResponseException::withViolations(
84
                "Unverified second factor retrieved from the Middleware was invalid",
85
                $violations
86
            );
87
        }
88
89
        return $secondFactor;
90
    }
91
92
    /**
93
     * @param string $secondFactorId
94
     * @return VerifiedSecondFactor|null
95
     * @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource.
96
     * @throws InvalidResponseException When the API responded with invalid data.
97
     * @throws ResourceReadException When the API doesn't respond with the resource.
98
     * @throws MalformedResponseException When the API doesn't respond with a proper response.
99
     */
100
    public function getVerified($secondFactorId)
101
    {
102
        $data = $this->service->getVerified($secondFactorId);
103
104
        if ($data === null) {
105
            return null;
106
        }
107
108
        $secondFactor = VerifiedSecondFactor::fromData($data);
109
        $violations = $this->validator->validate($secondFactor);
110
111
        if (count($violations) > 0) {
112
            throw InvalidResponseException::withViolations(
113
                "Verified second factor retrieved from the Middleware was invalid",
114
                $violations
115
            );
116
        }
117
118
        return $secondFactor;
119
    }
120
121
    /**
122
     * @param string $secondFactorId
123
     * @return VerifiedSecondFactor|null
0 ignored issues
show
Documentation introduced by
Should the return type not be null|boolean?

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...
124
     * @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource.
125
     * @throws InvalidResponseException When the API responded with invalid data.
126
     * @throws ResourceReadException When the API doesn't respond with the resource.
127
     * @throws MalformedResponseException When the API doesn't respond with a proper response.
128
     */
129
    public function getVerifiedSkipVetting($secondFactorId)
130
    {
131
        $data = $this->service->getVerifiedSkipVetting($secondFactorId);
132
133
        if ($data === null) {
134
            return null;
135
        }
136
137
        return (bool)$data;
138
    }
139
140
    /**
141
     * @param string $secondFactorId
142
     * @return VettedSecondFactor|null
143
     * @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource.
144
     * @throws InvalidResponseException When the API responded with invalid data.
145
     * @throws ResourceReadException When the API doesn't respond with the resource.
146
     * @throws MalformedResponseException When the API doesn't respond with a proper response.
147
     */
148
    public function getVetted($secondFactorId)
149
    {
150
        $data = $this->service->getVetted($secondFactorId);
151
152
        if ($data === null) {
153
            return null;
154
        }
155
156
        $secondFactor = VettedSecondFactor::fromData($data);
157
        $violations = $this->validator->validate($secondFactor);
158
159
        if (count($violations) > 0) {
160
            throw InvalidResponseException::withViolations(
161
                "Vetted second factor retrieved from the Middleware was invalid",
162
                $violations
163
            );
164
        }
165
166
        return $secondFactor;
167
    }
168
169
    /**
170
     * @param UnverifiedSecondFactorSearchQuery $query
171
     * @return UnverifiedSecondFactorCollection
0 ignored issues
show
Documentation introduced by
Should the return type not be UnverifiedSecondFactorCollection|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...
172
     * @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource.
173
     * @throws InvalidResponseException When the API responded with invalid data.
174
     * @throws ResourceReadException When the API doesn't respond with the resource.
175
     * @throws MalformedResponseException When the API doesn't respond with a proper response.
176
     */
177
    public function searchUnverified(UnverifiedSecondFactorSearchQuery $query)
178
    {
179
        $data = $this->service->searchUnverified($query);
180
181
        if ($data === null) {
182
            return null;
183
        }
184
185
        $secondFactors = UnverifiedSecondFactorCollection::fromData($data);
186
        $violations = $this->validator->validate($secondFactors);
187
188
        if (count($violations) > 0) {
189
            throw InvalidResponseException::withViolations(
190
                "One or more second factors retrieved from the Middleware were invalid",
191
                $violations
192
            );
193
        }
194
195
        return $secondFactors;
196
    }
197
198
    /**
199
     * @param VerifiedSecondFactorSearchQuery $query
200
     * @return VerifiedSecondFactorCollection
0 ignored issues
show
Documentation introduced by
Should the return type not be VerifiedSecondFactorCollection|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...
201
     * @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource.
202
     * @throws InvalidResponseException When the API responded with invalid data.
203
     * @throws ResourceReadException When the API doesn't respond with the resource.
204
     * @throws MalformedResponseException When the API doesn't respond with a proper response.
205
     */
206
    public function searchVerified(VerifiedSecondFactorSearchQuery $query)
207
    {
208
        $data = $this->service->searchVerified($query);
209
210
        if ($data === null) {
211
            return null;
212
        }
213
214
        $secondFactors = VerifiedSecondFactorCollection::fromData($data);
215
        $violations = $this->validator->validate($secondFactors);
216
217
        if (count($violations) > 0) {
218
            throw InvalidResponseException::withViolations(
219
                "One or more second factors retrieved from the Middleware were invalid",
220
                $violations
221
            );
222
        }
223
224
        return $secondFactors;
225
    }
226
227
    /**
228
     * @param VerifiedSecondFactorOfIdentitySearchQuery $query
229
     * @return VerifiedSecondFactorCollection
0 ignored issues
show
Documentation introduced by
Should the return type not be VerifiedSecondFactorCollection|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...
230
     * @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource.
231
     * @throws InvalidResponseException When the API responded with invalid data.
232
     * @throws ResourceReadException When the API doesn't respond with the resource.
233
     * @throws MalformedResponseException When the API doesn't respond with a proper response.
234
     */
235
    public function searchOwnVerified(VerifiedSecondFactorOfIdentitySearchQuery $query)
236
    {
237
        $data = $this->service->searchOwnVerified($query);
238
239
        if ($data === null) {
240
            return null;
241
        }
242
243
        $secondFactors = VerifiedSecondFactorCollection::fromData($data);
244
        $violations = $this->validator->validate($secondFactors);
245
246
        if (count($violations) > 0) {
247
            throw InvalidResponseException::withViolations(
248
                "One or more second factors retrieved from the Middleware were invalid",
249
                $violations
250
            );
251
        }
252
253
        return $secondFactors;
254
    }
255
256
    /**
257
     * @param VettedSecondFactorSearchQuery $query
258
     * @return VettedSecondFactorCollection
0 ignored issues
show
Documentation introduced by
Should the return type not be VettedSecondFactorCollection|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...
259
     * @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource.
260
     * @throws InvalidResponseException When the API responded with invalid data.
261
     * @throws ResourceReadException When the API doesn't respond with the resource.
262
     * @throws MalformedResponseException When the API doesn't respond with a proper response.
263
     */
264
    public function searchVetted(VettedSecondFactorSearchQuery $query)
265
    {
266
        $data = $this->service->searchVetted($query);
267
268
        if ($data === null) {
269
            return null;
270
        }
271
272
        $secondFactors = VettedSecondFactorCollection::fromData($data);
273
        $violations = $this->validator->validate($secondFactors);
274
275
        if (count($violations) > 0) {
276
            throw InvalidResponseException::withViolations(
277
                "One or more second factors retrieved from the Middleware were invalid",
278
                $violations
279
            );
280
        }
281
282
        return $secondFactors;
283
    }
284
}
285