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

RaSecondFactorService   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 51
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 4
dl 51
loc 51
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A search() 20 20 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\Exception\AccessDeniedToResourceException;
22
use Surfnet\StepupMiddlewareClient\Exception\MalformedResponseException;
23
use Surfnet\StepupMiddlewareClient\Exception\ResourceReadException;
24
use Surfnet\StepupMiddlewareClient\Identity\Dto\RaSecondFactorSearchQuery;
25
use Surfnet\StepupMiddlewareClient\Identity\Service\RaSecondFactorService as LibraryRaSecondFactorService;
26
use Surfnet\StepupMiddlewareClientBundle\Exception\InvalidResponseException;
27
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\RaSecondFactorCollection;
28
use Symfony\Component\Validator\Validator\ValidatorInterface;
29
30
/**
31
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
32
 */
33 View Code Duplication
class RaSecondFactorService
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...
34
{
35
    /**
36
     * @var LibraryRaSecondFactorService
37
     */
38
    private $service;
39
40
    /**
41
     * @var ValidatorInterface
42
     */
43
    private $validator;
44
45
    /**
46
     * @param LibraryRaSecondFactorService $service
47
     * @param ValidatorInterface $validator
48
     */
49
    public function __construct(LibraryRaSecondFactorService $service, ValidatorInterface $validator)
50
    {
51
        $this->service = $service;
52
        $this->validator = $validator;
53
    }
54
55
    /**
56
     * @param RaSecondFactorSearchQuery $query
57
     * @return RaSecondFactorCollection
0 ignored issues
show
Documentation introduced by
Should the return type not be RaSecondFactorCollection|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...
58
     * @throws AccessDeniedToResourceException When the consumer isn't authorised to access given resource.
59
     * @throws InvalidResponseException When the API responded with invalid data.
60
     * @throws ResourceReadException When the API doesn't respond with the resource.
61
     * @throws MalformedResponseException When the API doesn't respond with a proper response.
62
     */
63
    public function search(RaSecondFactorSearchQuery $query)
64
    {
65
        $data = $this->service->search($query);
66
67
        if ($data === null) {
68
            return null;
69
        }
70
71
        $secondFactors = RaSecondFactorCollection::fromData($data);
72
        $violations = $this->validator->validate($secondFactors);
73
74
        if (count($violations) > 0) {
75
            throw InvalidResponseException::withViolations(
76
                "One or more second factors retrieved from the Middleware were invalid",
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal One or more second facto...Middleware were invalid does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
77
                $violations
78
            );
79
        }
80
81
        return $secondFactors;
82
    }
83
}
84