Completed
Pull Request — master (#102)
by Boy
18:16
created

SignRequestParamConverter::apply()   B

Complexity

Conditions 7
Paths 30

Size

Total Lines 53
Code Lines 32

Duplication

Lines 53
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 53
loc 53
rs 7.5251
cc 7
eloc 32
nc 30
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\SignRequest;
25
use Symfony\Component\HttpFoundation\Request;
26
use Symfony\Component\Validator\Validator\ValidatorInterface;
27
28 View Code Duplication
class SignRequestParamConverter 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']['request'])) {
64
            $errors[] = sprintf('Missing parameter "authentication.request"');
65
        } else {
66
            $actualPropertyNames     = array_keys($object['authentication']['request']);
67
            $expectedPropertyNames   = ['app_id', 'challenge', 'version', 'key_handle'];
68
            $missingPropertyNames    = array_diff($expectedPropertyNames, $actualPropertyNames);
69
            $extraneousPropertyNames = array_diff($actualPropertyNames, $expectedPropertyNames);
70
71
            if (count($missingPropertyNames)) {
72
                $errors[] = sprintf('Missing authentication request properties: %s', join(', ', $missingPropertyNames));
73
            }
74
75
            if (count($extraneousPropertyNames)) {
76
                $errors[] = sprintf(
77
                    'Extraneous authentication request properties: %s',
78
                    join(', ', $extraneousPropertyNames)
79
                );
80
            }
81
        }
82
83
        if (count($errors) > 0) {
84
            throw new BadJsonRequestException($errors);
85
        }
86
87
        $signRequest = new SignRequest();
88
        $signRequest->appId = $object['authentication']['request']['app_id'];
89
        $signRequest->challenge = $object['authentication']['request']['challenge'];
90
        $signRequest->version = $object['authentication']['request']['version'];
91
        $signRequest->keyHandle = $object['authentication']['request']['key_handle'];
92
93
        $violations = $this->validator->validate($signRequest);
94
95
        if (count($violations) > 0) {
96
            throw BadJsonRequestException::createForViolationsAndErrors($violations, $name, []);
97
        }
98
99
        $request->attributes->set($name, $signRequest);
100
101
        return true;
102
    }
103
104
    public function supports(ParamConverter $configuration)
105
    {
106
        return $configuration->getClass() === 'Surfnet\StepupU2fBundle\Dto\SignRequest';
107
    }
108
}
109