Completed
Push — master ( 5467e4...183ea4 )
by Boy
05:06 queued 01:02
created

CommandParamConverter   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 63
Duplicated Lines 14.29 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 0
cbo 4
dl 9
loc 63
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 0 23 2
A supports() 0 5 2
D assertIsValidCommandStructure() 9 26 9

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\StepupMiddleware\ApiBundle\Request;
20
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
22
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
23
use Surfnet\StepupMiddleware\ApiBundle\Exception\BadCommandRequestException;
24
use Symfony\Component\HttpFoundation\Request;
25
26
class CommandParamConverter implements ParamConverterInterface
27
{
28
    public function apply(Request $request, ParamConverter $configuration)
29
    {
30
        $data = json_decode($request->getContent(), true);
31
32
        $this->assertIsValidCommandStructure($data);
33
34
        preg_match('~^(\w+):([\w\\.]+)$~', $data['command']['name'], $commandName);
35
        $commandClassName = sprintf(
36
            'Surfnet\StepupMiddleware\CommandHandlingBundle\%s\Command\%sCommand',
37
            $commandName[1],
38
            str_replace('.', '\\', $commandName[2])
39
        );
40
41
        $command = new $commandClassName;
42
        $command->UUID = $data['command']['uuid'];
43
44
        foreach ($data['command']['payload'] as $property => $value) {
45
            $properlyCasedProperty = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $property))));
46
            $command->$properlyCasedProperty = $value;
47
        }
48
49
        $request->attributes->set('command', $command);
50
    }
51
52
    public function supports(ParamConverter $configuration)
53
    {
54
        return $configuration->getName() === 'command'
55
            && $configuration->getClass() === 'Surfnet\StepupMiddleware\CommandHandlingBundle\Command\Command';
56
    }
57
58
    /**
59
     * @param mixed $data
60
     * @throws BadCommandRequestException
61
     */
62
    private function assertIsValidCommandStructure($data)
63
    {
64
        if (!is_array($data)) {
65
            $type = gettype($data);
66
67
            throw new BadCommandRequestException(
68
                [sprintf('Command is not valid: body must be a JSON object, but is of type %s', $type)]
69
            );
70
        }
71
72
        if (!isset($data['command'])) {
73
            throw new BadCommandRequestException(["Required parameter 'command' is not set."]);
74
        }
75
76 View Code Duplication
        if (!isset($data['command']['name']) || !is_string($data['command']['name'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
77
            throw new BadCommandRequestException(["Required command parameter 'name' is not set or not a string."]);
78
        }
79
80 View Code Duplication
        if (!isset($data['command']['uuid']) || !is_string($data['command']['uuid'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
81
            throw new BadCommandRequestException(["Required command parameter 'uuid' is not set or not a string."]);
82
        }
83
84 View Code Duplication
        if (!isset($data['command']['payload']) || !is_array($data['command']['payload'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
85
            throw new BadCommandRequestException(["Required command parameter 'payload' is not set or not an object."]);
86
        }
87
    }
88
}
89