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