Completed
Push — master ( d2aa7f...78f4d4 )
by Michiel
03:30 queued 02:21
created

RaCandidateService::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 2
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\Identity\Dto\RaCandidateSearchQuery;
22
use Surfnet\StepupMiddlewareClient\Identity\Service\RaCandidateService as LibraryRaCandidateService;
23
use Surfnet\StepupMiddlewareClientBundle\Exception\InvalidResponseException;
24
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\RaCandidate;
25
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\RaCandidateCollection;
26
use Symfony\Component\Validator\Validator\ValidatorInterface;
27
28
class RaCandidateService
29
{
30
    /**
31
     * @var LibraryRaCandidateService
32
     */
33
    private $libraryService;
34
35
    /**
36
     * @var ValidatorInterface
37
     */
38
    private $validator;
39
40
    /**
41
     * @param LibraryRaCandidateService $libraryService
42
     * @param ValidatorInterface        $validator
43
     */
44
    public function __construct(LibraryRaCandidateService $libraryService, ValidatorInterface $validator)
45
    {
46
        $this->libraryService = $libraryService;
47
        $this->validator      = $validator;
48
    }
49
50
    /**
51
     * @param RaCandidateSearchQuery $query
52
     * @return RaCandidateCollection
53
     */
54
    public function search(RaCandidateSearchQuery $query)
55
    {
56
        $data = $this->libraryService->search($query);
57
58
        if ($data === null) {
59
            throw new InvalidResponseException(
60
                'Received a "null" as data when searching for RaCandidates, is the library service set up correctly?'
61
            );
62
        }
63
64
        $collection = RaCandidateCollection::fromData($data);
65
66
        $this->assertIsValid($collection, 'One or more RaCandidates are not valid');
67
68
        return $collection;
69
    }
70
71
    /**
72
     * @param string $identityId
73
     * @param string $institution
74
     * @return null|RaCandidate
75
     */
76
    public function get($identityId, $institution)
77
    {
78
        $data = $this->libraryService->get($identityId, $institution);
79
80
        if ($data === null) {
81
            return null;
82
        }
83
84
        $raCandidate = RaCandidate::fromData($data);
85
86
        $this->assertIsValid($raCandidate, 'Received invalid RaCandidate');
87
88
        return $raCandidate;
89
    }
90
91
    /**
92
     * @param mixed $value
93
     * @param string $message
94
     */
95
    private function assertIsValid($value, $message)
96
    {
97
        $violations = $this->validator->validate($value);
98
99
        if (count($violations)) {
100
            throw InvalidResponseException::withViolations($message, $violations);
101
        }
102
    }
103
}
104