RaService::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
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\Service\RaService as LibraryRaService;
22
use Surfnet\StepupMiddlewareClientBundle\Exception\InvalidResponseException;
23
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\RegistrationAuthorityCredentialsCollection;
24
use Symfony\Component\Validator\Validator\ValidatorInterface;
25
26
/**
27
 * Provides access to the Middleware API resources.
28
 */
29
class RaService
30
{
31
    /**
32
     * @var LibraryRaService
33
     */
34
    private $service;
35
36
    /**
37
     * @var ValidatorInterface
38
     */
39
    private $validator;
40
41
    /**
42
     * @param LibraryRaService $service
43
     * @param ValidatorInterface $validator
44
     */
45
    public function __construct(LibraryRaService $service, ValidatorInterface $validator)
46
    {
47
        $this->service = $service;
48
        $this->validator = $validator;
49
    }
50
51
    /**
52
     * @param string $institution
53
     * @return RegistrationAuthorityCredentialsCollection
54
     */
55
    public function listRas($institution)
56
    {
57
        $data = $this->service->listRas($institution);
58
59
        $collection = RegistrationAuthorityCredentialsCollection::fromData($data);
0 ignored issues
show
Bug introduced by
It seems like $data defined by $this->service->listRas($institution) on line 57 can also be of type null; however, Surfnet\StepupMiddleware...llectionDto::fromData() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
60
61
        $this->assertIsValid($collection, 'Invalid elements received in collection');
62
63
        return $collection;
64
    }
65
66
    /**
67
     * @param object      $value
68
     * @param null|string $message
69
     */
70
    private function assertIsValid($value, $message = null)
71
    {
72
        $violations = $this->validator->validate($value);
73
74
        $message = $message ?: 'Invalid Response Received';
75
76
        if (count($violations) > 0) {
77
            throw InvalidResponseException::withViolations($message, $violations);
78
        }
79
    }
80
}
81