Completed
Push — feature/ra-locations ( 8e7bb5 )
by Boy
12:38
created

getRegistrationAuthorityCredentials()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 16
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 16
loc 16
rs 9.4285
cc 2
eloc 8
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\IdentitySearchQuery;
22
use Surfnet\StepupMiddlewareClient\Identity\Service\IdentityService as LibraryIdentityService;
23
use Surfnet\StepupMiddlewareClientBundle\Exception\InvalidResponseException;
24
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\Identity;
25
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\IdentityCollection;
26
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\RegistrationAuthorityCredentials;
27
use Symfony\Component\Validator\Validator\ValidatorInterface;
28
29
/**
30
 * Provides access to the Middleware API resources.
31
 */
32
class IdentityService
33
{
34
    /**
35
     * @var LibraryIdentityService
36
     */
37
    private $service;
38
39
    /**
40
     * @var ValidatorInterface
41
     */
42
    private $validator;
43
44
    /**
45
     * @param LibraryIdentityService $service
46
     * @param ValidatorInterface $validator
47
     */
48
    public function __construct(LibraryIdentityService $service, ValidatorInterface $validator)
49
    {
50
        $this->service = $service;
51
        $this->validator = $validator;
52
    }
53
54
    /**
55
     * @param string $id
56
     * @return null|Identity
57
     */
58 View Code Duplication
    public function get($id)
0 ignored issues
show
Duplication introduced by
This method 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...
59
    {
60
        $data = $this->service->get($id);
61
62
        if ($data === null) {
63
            return null;
64
        }
65
66
        $identity = Identity::fromData($data);
67
68
        $message = sprintf("Identity '%s' retrieved from the Middleware is invalid", $id);
69
        $this->assertIsValid($identity, $message);
70
71
        return $identity;
72
    }
73
74
    /**
75
     * @param IdentitySearchQuery $searchQuery
76
     * @return \Surfnet\StepupMiddlewareClientBundle\Identity\Dto\IdentityCollection
77
     */
78
    public function search(IdentitySearchQuery $searchQuery)
79
    {
80
        $data = $this->service->search($searchQuery);
81
82
        $collection = IdentityCollection::fromData($data);
0 ignored issues
show
Bug introduced by
It seems like $data defined by $this->service->search($searchQuery) on line 80 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...
83
84
        $this->assertIsValid($collection, 'Invalid elements received in collection');
85
86
        return $collection;
87
    }
88
89
    /**
90
     * @param Identity $identity
91
     * @return RegistrationAuthorityCredentials|null
92
     */
93 View Code Duplication
    public function getRegistrationAuthorityCredentials(Identity $identity)
0 ignored issues
show
Duplication introduced by
This method 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...
94
    {
95
        $data = $this->service->getRegistrationAuthorityCredentials($identity->id);
96
97
        // 404 Not Found is a valid case.
98
        if (!$data) {
99
            return null;
100
        }
101
102
        $credentials = RegistrationAuthorityCredentials::fromData($data);
103
104
        $message = sprintf('Registration Authority Credentials for Identity[%s] are invalid', $identity->id);
105
        $this->assertIsValid($credentials, $message);
106
107
        return $credentials;
108
    }
109
110
    /**
111
     * @param object      $value
112
     * @param null|string $message
113
     */
114 View Code Duplication
    private function assertIsValid($value, $message = null)
0 ignored issues
show
Duplication introduced by
This method 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...
115
    {
116
        $violations = $this->validator->validate($value);
117
118
        $message = $message ?: 'Invalid Response Received';
119
120
        if (count($violations) > 0) {
121
            throw InvalidResponseException::withViolations($message, $violations);
122
        }
123
    }
124
}
125