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\RaSecondFactorExportQuery; |
25
|
|
|
use Surfnet\StepupMiddlewareClient\Identity\Dto\RaSecondFactorSearchQuery; |
26
|
|
|
use Surfnet\StepupMiddlewareClient\Identity\Service\RaSecondFactorService as LibraryRaSecondFactorService; |
27
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Exception\InvalidResponseException; |
28
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\RaSecondFactorCollection; |
29
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\RaSecondFactorExportCollection; |
30
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
34
|
|
|
*/ |
35
|
|
|
class RaSecondFactorService |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var LibraryRaSecondFactorService |
39
|
|
|
*/ |
40
|
|
|
private $service; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var ValidatorInterface |
44
|
|
|
*/ |
45
|
|
|
private $validator; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param LibraryRaSecondFactorService $service |
49
|
|
|
* @param ValidatorInterface $validator |
50
|
|
|
*/ |
51
|
|
|
public function __construct(LibraryRaSecondFactorService $service, ValidatorInterface $validator) |
52
|
|
|
{ |
53
|
|
|
$this->service = $service; |
54
|
|
|
$this->validator = $validator; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param RaSecondFactorSearchQuery $query |
59
|
|
|
* @return RaSecondFactorCollection |
|
|
|
|
60
|
|
|
* @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource. |
61
|
|
|
* @throws InvalidResponseException When the API responded with invalid data. |
62
|
|
|
* @throws ResourceReadException When the API doesn't respond with the resource. |
63
|
|
|
* @throws MalformedResponseException When the API doesn't respond with a proper response. |
64
|
|
|
*/ |
65
|
|
|
public function search(RaSecondFactorSearchQuery $query) |
66
|
|
|
{ |
67
|
|
|
$data = $this->service->search($query); |
68
|
|
|
|
69
|
|
|
if ($data === null) { |
70
|
|
|
return null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$secondFactors = RaSecondFactorCollection::fromData($data); |
74
|
|
|
$violations = $this->validator->validate($secondFactors); |
75
|
|
|
|
76
|
|
|
if (count($violations) > 0) { |
77
|
|
|
throw InvalidResponseException::withViolations( |
78
|
|
|
"One or more second factors retrieved from the Middleware were invalid", |
79
|
|
|
$violations |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $secondFactors; |
84
|
|
|
} |
85
|
|
|
/** |
86
|
|
|
* @param RaSecondFactorExportQuery $query |
87
|
|
|
* @return RaSecondFactorExportCollection |
|
|
|
|
88
|
|
|
* @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource. |
89
|
|
|
* @throws InvalidResponseException When the API responded with invalid data. |
90
|
|
|
* @throws ResourceReadException When the API doesn't respond with the resource. |
91
|
|
|
* @throws MalformedResponseException When the API doesn't respond with a proper response. |
92
|
|
|
*/ |
93
|
|
|
public function searchForExport(RaSecondFactorExportQuery $query) |
94
|
|
|
{ |
95
|
|
|
$data = $this->service->searchForExport($query); |
96
|
|
|
|
97
|
|
|
if ($data === null) { |
98
|
|
|
return null; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$secondFactors = RaSecondFactorExportCollection::fromData($data); |
102
|
|
|
$violations = $this->validator->validate($secondFactors); |
103
|
|
|
|
104
|
|
|
if (count($violations) > 0) { |
105
|
|
|
throw InvalidResponseException::withViolations( |
106
|
|
|
"One or more second factors retrieved from the Middleware were invalid", |
107
|
|
|
$violations |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $secondFactors; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
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.