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'])) { |
|
|
|
|
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'])) { |
|
|
|
|
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'])) { |
|
|
|
|
85
|
|
|
throw new BadCommandRequestException(["Required command parameter 'payload' is not set or not an object."]); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
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.