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

RaService   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 19.23 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 5
c 3
b 0
f 2
lcom 1
cbo 4
dl 10
loc 52
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A listRas() 0 10 1
A assertIsValid() 10 10 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 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...
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