Completed
Push — feature/self-service-verified-... ( 3ac932...0d092e )
by Michiel
07:47
created

SecondFactorService::searchVerified()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 3
nc 3
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\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\VerifiedSecondFactorSearchQuery;
26
use Surfnet\StepupMiddlewareClient\Identity\Dto\VettedSecondFactorSearchQuery;
27
use Surfnet\StepupMiddlewareClient\Identity\Service\SecondFactorService as LibrarySecondFactorService;
28
use Surfnet\StepupMiddlewareClientBundle\Exception\InvalidResponseException;
29
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\UnverifiedSecondFactor;
30
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\UnverifiedSecondFactorCollection;
31
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\VerifiedSecondFactor;
32
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\VerifiedSecondFactorCollection;
33
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\VettedSecondFactor;
34
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\VettedSecondFactorCollection;
35
use Symfony\Component\Validator\Validator\ValidatorInterface;
36
37
/**
38
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
39
 */
40
class SecondFactorService
41
{
42
    /**
43
     * @var LibrarySecondFactorService
44
     */
45
    private $service;
46
47
    /**
48
     * @var ValidatorInterface
49
     */
50
    private $validator;
51
52
    /**
53
     * @param LibrarySecondFactorService $service
54
     * @param ValidatorInterface $validator
55
     */
56
    public function __construct(LibrarySecondFactorService $service, ValidatorInterface $validator)
57
    {
58
        $this->service = $service;
59
        $this->validator = $validator;
60
    }
61
62
    /**
63
     * @param string $secondFactorId
64
     * @return UnverifiedSecondFactor|null
65
     * @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource.
66
     * @throws InvalidResponseException When the API responded with invalid data.
67
     * @throws ResourceReadException When the API doesn't respond with the resource.
68
     * @throws MalformedResponseException When the API doesn't respond with a proper response.
69
     */
70
    public function getUnverified($secondFactorId)
71
    {
72
        $data = $this->service->getUnverified($secondFactorId);
73
74
        if ($data === null) {
75
            return null;
76
        }
77
78
        $secondFactor = UnverifiedSecondFactor::fromData($data);
79
        $violations = $this->validator->validate($secondFactor);
80
81
        if (count($violations) > 0) {
82
            throw InvalidResponseException::withViolations(
83
                "Unverified second factor retrieved from the Middleware was invalid",
84
                $violations
85
            );
86
        }
87
88
        return $secondFactor;
89
    }
90
91
    /**
92
     * @param string $secondFactorId
93
     * @return VerifiedSecondFactor|null
94
     * @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource.
95
     * @throws InvalidResponseException When the API responded with invalid data.
96
     * @throws ResourceReadException When the API doesn't respond with the resource.
97
     * @throws MalformedResponseException When the API doesn't respond with a proper response.
98
     */
99
    public function getVerified($secondFactorId)
100
    {
101
        $data = $this->service->getVerified($secondFactorId);
102
103
        if ($data === null) {
104
            return null;
105
        }
106
107
        $secondFactor = VerifiedSecondFactor::fromData($data);
108
        $violations = $this->validator->validate($secondFactor);
109
110
        if (count($violations) > 0) {
111
            throw InvalidResponseException::withViolations(
112
                "Verified second factor retrieved from the Middleware was invalid",
113
                $violations
114
            );
115
        }
116
117
        return $secondFactor;
118
    }
119
120
    /**
121
     * @param string $secondFactorId
122
     * @return VettedSecondFactor|null
123
     * @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource.
124
     * @throws InvalidResponseException When the API responded with invalid data.
125
     * @throws ResourceReadException When the API doesn't respond with the resource.
126
     * @throws MalformedResponseException When the API doesn't respond with a proper response.
127
     */
128
    public function getVetted($secondFactorId)
129
    {
130
        $data = $this->service->getVetted($secondFactorId);
131
132
        if ($data === null) {
133
            return null;
134
        }
135
136
        $secondFactor = VettedSecondFactor::fromData($data);
137
        $violations = $this->validator->validate($secondFactor);
138
139
        if (count($violations) > 0) {
140
            throw InvalidResponseException::withViolations(
141
                "Vetted second factor retrieved from the Middleware was invalid",
142
                $violations
143
            );
144
        }
145
146
        return $secondFactor;
147
    }
148
149
    /**
150
     * @param UnverifiedSecondFactorSearchQuery $query
151
     * @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...
152
     * @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource.
153
     * @throws InvalidResponseException When the API responded with invalid data.
154
     * @throws ResourceReadException When the API doesn't respond with the resource.
155
     * @throws MalformedResponseException When the API doesn't respond with a proper response.
156
     */
157
    public function searchUnverified(UnverifiedSecondFactorSearchQuery $query)
158
    {
159
        $data = $this->service->searchUnverified($query);
160
161
        if ($data === null) {
162
            return null;
163
        }
164
165
        $secondFactors = UnverifiedSecondFactorCollection::fromData($data);
166
        $violations = $this->validator->validate($secondFactors);
167
168
        if (count($violations) > 0) {
169
            throw InvalidResponseException::withViolations(
170
                "One or more second factors retrieved from the Middleware were invalid",
171
                $violations
172
            );
173
        }
174
175
        return $secondFactors;
176
    }
177
178
    /**
179
     * @param VerifiedSecondFactorSearchQuery $query
180
     * @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...
181
     * @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource.
182
     * @throws InvalidResponseException When the API responded with invalid data.
183
     * @throws ResourceReadException When the API doesn't respond with the resource.
184
     * @throws MalformedResponseException When the API doesn't respond with a proper response.
185
     */
186
    public function searchVerified(VerifiedSecondFactorSearchQuery $query)
187
    {
188
        $data = $this->service->searchVerified($query);
189
190
        if ($data === null) {
191
            return null;
192
        }
193
194
        $secondFactors = VerifiedSecondFactorCollection::fromData($data);
195
        $violations = $this->validator->validate($secondFactors);
196
197
        if (count($violations) > 0) {
198
            throw InvalidResponseException::withViolations(
199
                "One or more second factors retrieved from the Middleware were invalid",
200
                $violations
201
            );
202
        }
203
204
        return $secondFactors;
205
    }
206
207
    /**
208
     * @param VerifiedSecondFactorSearchQuery $query
209
     * @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...
210
     * @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource.
211
     * @throws InvalidResponseException When the API responded with invalid data.
212
     * @throws ResourceReadException When the API doesn't respond with the resource.
213
     * @throws MalformedResponseException When the API doesn't respond with a proper response.
214
     */
215
    public function searchOwnVerified(VerifiedSecondFactorSearchQuery $query)
216
    {
217
        $data = $this->service->searchOwnVerified($query);
218
219
        if ($data === null) {
220
            return null;
221
        }
222
223
        $secondFactors = VerifiedSecondFactorCollection::fromData($data);
224
        $violations = $this->validator->validate($secondFactors);
225
226
        if (count($violations) > 0) {
227
            throw InvalidResponseException::withViolations(
228
                "One or more second factors retrieved from the Middleware were invalid",
229
                $violations
230
            );
231
        }
232
233
        return $secondFactors;
234
    }
235
236
    /**
237
     * @param VettedSecondFactorSearchQuery $query
238
     * @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...
239
     * @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource.
240
     * @throws InvalidResponseException When the API responded with invalid data.
241
     * @throws ResourceReadException When the API doesn't respond with the resource.
242
     * @throws MalformedResponseException When the API doesn't respond with a proper response.
243
     */
244
    public function searchVetted(VettedSecondFactorSearchQuery $query)
245
    {
246
        $data = $this->service->searchVetted($query);
247
248
        if ($data === null) {
249
            return null;
250
        }
251
252
        $secondFactors = VettedSecondFactorCollection::fromData($data);
253
        $violations = $this->validator->validate($secondFactors);
254
255
        if (count($violations) > 0) {
256
            throw InvalidResponseException::withViolations(
257
                "One or more second factors retrieved from the Middleware were invalid",
258
                $violations
259
            );
260
        }
261
262
        return $secondFactors;
263
    }
264
}
265