Completed
Push — remote-vetting ( 4132ed...71cf35 )
by
unknown
01:30
created

RaCandidateService::getByIdentityId()   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 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\Identity\Dto\RaCandidateSearchQuery;
22
use Surfnet\StepupMiddlewareClient\Identity\Service\RaCandidateService as LibraryRaCandidateService;
23
use Surfnet\StepupMiddlewareClientBundle\Exception\InvalidResponseException;
24
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\RaCandidateCollection;
25
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\RaCandidateInstitutions;
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
0 ignored issues
show
Bug introduced by
There is no parameter named $institution. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
74
     * @param string $actorId
75
     * @return RaCandidateInstitutions
0 ignored issues
show
Documentation introduced by
Should the return type not be RaCandidateInstitutions|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...
76
     */
77
    public function get($identityId, $actorId)
78
    {
79
        $data = $this->libraryService->get($identityId, $actorId);
80
81
        if ($data === null) {
82
            return null;
83
        }
84
85
        $raCandidateInstitutions = RaCandidateInstitutions::fromData($data);
86
87
        $this->assertIsValid($raCandidateInstitutions, 'Received invalid RaCandidate');
88
89
        return $raCandidateInstitutions;
90
    }
91
92
    /**
93
     * @param mixed $value
94
     * @param string $message
95
     */
96
    private function assertIsValid($value, $message)
97
    {
98
        $violations = $this->validator->validate($value);
99
100
        if (count($violations)) {
101
            throw InvalidResponseException::withViolations($message, $violations);
102
        }
103
    }
104
}
105