Completed
Push — master ( 00f21d...76f138 )
by Boy
19:52 queued 16:05
created

SignResponseParamConverter   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 84
loc 84
rs 10
c 3
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A supports() 4 4 1
B apply() 56 56 7

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 2015 SURFnet B.V.
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\StepupGateway\ApiBundle\Request;
20
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
22
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
23
use Surfnet\StepupBundle\Exception\BadJsonRequestException;
24
use Surfnet\StepupU2fBundle\Dto\SignResponse;
25
use Symfony\Component\HttpFoundation\Request;
26
use Symfony\Component\Validator\Validator\ValidatorInterface;
27
28 View Code Duplication
class SignResponseParamConverter implements ParamConverterInterface
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...
29
{
30
    /**
31
     * @var ValidatorInterface
32
     */
33
    private $validator;
34
35
    public function __construct(ValidatorInterface $validator)
36
    {
37
        $this->validator = $validator;
38
    }
39
40
    /**
41
     * Stores the object in the request.
42
     *
43
     * @param Request        $request       The request
44
     * @param ParamConverter $configuration Contains the name, class and options of the object
45
     *
46
     * @return bool    True if the object has been successfully set, else false
47
     *
48
     * @SuppressWarnings(PHPMD.NPathComplexity) -- Simply a lot of isset() calls.
49
     */
50
    public function apply(Request $request, ParamConverter $configuration)
51
    {
52
        $name = $configuration->getName();
53
54
        $json = $request->getContent();
55
        $object = json_decode($json, true);
56
57
        $errors = [];
58
59
        if (!isset($object['authentication'])) {
60
            $errors[] = sprintf('Missing parameter "authentication"');
61
        }
62
63
        if (!isset($object['authentication']['response'])) {
64
            $errors[] = sprintf('Missing parameter "authentication.response"');
65
        } else {
66
            $actualPropertyNames     = array_keys($object['authentication']['response']);
67
            $expectedPropertyNames   = ['error_code', 'client_data', 'signature_data', 'key_handle'];
68
            $missingPropertyNames    = array_diff($expectedPropertyNames, $actualPropertyNames);
69
            $extraneousPropertyNames = array_diff($actualPropertyNames, $expectedPropertyNames);
70
71
            if (count($missingPropertyNames)) {
72
                $errors[] = sprintf(
73
                    'Missing authentication response properties: %s',
74
                    join(', ', $missingPropertyNames)
75
                );
76
            }
77
78
            if (count($extraneousPropertyNames)) {
79
                $errors[] = sprintf(
80
                    'Extraneous authentication response properties: %s',
81
                    join(', ', $extraneousPropertyNames)
82
                );
83
            }
84
        }
85
86
        if (count($errors) > 0) {
87
            throw new BadJsonRequestException($errors);
88
        }
89
90
        $signResponse = new SignResponse();
91
        $signResponse->errorCode = $object['authentication']['response']['error_code'];
92
        $signResponse->clientData = $object['authentication']['response']['client_data'];
93
        $signResponse->signatureData = $object['authentication']['response']['signature_data'];
94
        $signResponse->keyHandle = $object['authentication']['response']['key_handle'];
95
96
        $violations = $this->validator->validate($signResponse);
97
98
        if (count($violations) > 0) {
99
            throw BadJsonRequestException::createForViolationsAndErrors($violations, $name, []);
100
        }
101
102
        $request->attributes->set($name, $signResponse);
103
104
        return true;
105
    }
106
107
    public function supports(ParamConverter $configuration)
108
    {
109
        return $configuration->getClass() === 'Surfnet\StepupU2fBundle\Dto\SignResponse';
110
    }
111
}
112