Completed
Push — feature/ra-locations ( 8e7bb5...72222c )
by Boy
14:46 queued 11:53
created

RaLocationService::search()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 19
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 19
loc 19
rs 9.4285
cc 2
eloc 10
nc 2
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\Configuration\Service;
20
21
use Surfnet\StepupMiddlewareClient\Configuration\Dto\RaLocationSearchQuery;
22
use Surfnet\StepupMiddlewareClient\Configuration\Service\RaLocationService as LibraryRaLocationService;
23
use Surfnet\StepupMiddlewareClientBundle\Configuration\Dto\RaLocation;
24
use Surfnet\StepupMiddlewareClientBundle\Configuration\Dto\RaLocationCollection;
25
use Surfnet\StepupMiddlewareClientBundle\Exception\InvalidResponseException;
26
use Symfony\Component\Validator\Validator\ValidatorInterface;
27
28
/**
29
 * Provides access to the Middleware API resources.
30
 */
31 View Code Duplication
class RaLocationService
0 ignored issues
show
Duplication introduced by
This class 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...
32
{
33
    /**
34
     * @var LibraryRaLocationService
35
     */
36
    private $service;
37
38
    /**
39
     * @var ValidatorInterface
40
     */
41
    private $validator;
42
43
    /**
44
     * @param LibraryRaLocationService $service
45
     * @param ValidatorInterface      $validator
46
     */
47
    public function __construct(LibraryRaLocationService $service, ValidatorInterface $validator)
48
    {
49
        $this->service   = $service;
50
        $this->validator = $validator;
51
    }
52
53
    /**
54
     * @param string $id
55
     * @return null|RaLocation
56
     */
57
    public function get($id)
58
    {
59
        $data = $this->service->get($id);
60
61
        if ($data === null) {
62
            return null;
63
        }
64
65
        $raLocation = RaLocation::fromData($data);
66
        $message = sprintf("RaLocation '%s' retrieved from the Middleware is invalid", $id);
67
        $this->assertIsValid($raLocation, $message);
68
69
        return $raLocation;
70
    }
71
72
    /**
73
     * @param RaLocationSearchQuery $searchQuery
74
     * @return RaLocationCollection
75
     */
76
    public function search(RaLocationSearchQuery $searchQuery)
77
    {
78
        $data = $this->service->search($searchQuery);
79
80
        if ($data === null) {
81
            throw new InvalidResponseException(
82
                'Received a "null" as data when searching for RaLocations, is the library service set up correctly?'
83
            );
84
        }
85
86
        $registrationLocations = RaLocationCollection::fromData($data);
87
88
        $this->assertIsValid(
89
            $registrationLocations,
90
            'One or more registration authority listings retrieved from the Middleware were invalid'
91
        );
92
93
        return $registrationLocations;
94
    }
95
96
    /**
97
     * @param object      $value
98
     * @param null|string $message
99
     */
100
    private function assertIsValid($value, $message = null)
101
    {
102
        $violations = $this->validator->validate($value);
103
104
        $message = $message ?: 'Invalid Response Received';
105
106
        if (count($violations) > 0) {
107
            throw InvalidResponseException::withViolations($message, $violations);
108
        }
109
    }
110
}
111