RaListingService   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 83
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 83
loc 83
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A search() 19 19 2
A assertIsValid() 10 10 3
A get() 14 14 2

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\Dto\RaListingSearchQuery;
22
use Surfnet\StepupMiddlewareClient\Identity\Service\RaListingService as LibraryRaListingService;
23
use Surfnet\StepupMiddlewareClientBundle\Exception\InvalidResponseException;
24
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\RaListing;
25
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\RaListingCollection;
26
use Symfony\Component\Validator\Validator\ValidatorInterface;
27
28
/**
29
 * Provides access to the Middleware API resources.
30
 */
31 View Code Duplication
class RaListingService
0 ignored issues
show
Duplication introduced by
This class 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...
32
{
33
    /**
34
     * @var LibraryRaListingService
35
     */
36
    private $service;
37
38
    /**
39
     * @var ValidatorInterface
40
     */
41
    private $validator;
42
43
    /**
44
     * @param LibraryRaListingService $service
45
     * @param ValidatorInterface      $validator
46
     */
47
    public function __construct(LibraryRaListingService $service, ValidatorInterface $validator)
48
    {
49
        $this->service   = $service;
50
        $this->validator = $validator;
51
    }
52
53
    /**
54
     * @param string $id
55
     * @param string $institution
56
     * @param string $actorInstitution
0 ignored issues
show
Documentation introduced by
There is no parameter named $actorInstitution. Did you maybe mean $institution?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

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

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

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

Loading history...
57
     * @param string $actorId
58
     * @return null|RaListing
59
     */
60
    public function get($id, $institution, $actorId)
61
    {
62
        $data = $this->service->get($id, $institution, $actorId);
63
64
        if ($data === null) {
65
            return null;
66
        }
67
68
        $raListing = RaListing::fromData($data);
69
        $message = sprintf("RaListing '%s' retrieved from the Middleware is invalid", $id);
70
        $this->assertIsValid($raListing, $message);
71
72
        return $raListing;
73
    }
74
75
    /**
76
     * @param RaListingSearchQuery $searchQuery
77
     * @return RaListingCollection
78
     */
79
    public function search(RaListingSearchQuery $searchQuery)
80
    {
81
        $data = $this->service->search($searchQuery);
82
83
        if ($data === null) {
84
            throw new InvalidResponseException(
85
                'Received a "null" as data when searching for RaListings, is the library service set up correctly?'
86
            );
87
        }
88
89
        $registrationAuthorities = RaListingCollection::fromData($data);
90
91
        $this->assertIsValid(
92
            $registrationAuthorities,
93
            'One or more registration authority listings retrieved from the Middleware were invalid'
94
        );
95
96
        return $registrationAuthorities;
97
    }
98
99
    /**
100
     * @param object      $value
101
     * @param null|string $message
102
     */
103
    private function assertIsValid($value, $message = null)
104
    {
105
        $violations = $this->validator->validate($value);
106
107
        $message = $message ?: 'Invalid Response Received';
108
109
        if (count($violations) > 0) {
110
            throw InvalidResponseException::withViolations($message, $violations);
111
        }
112
    }
113
}
114