MetadataValueResolver   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A resolve() 0 26 5
A assertIsValidMetadataStructure() 0 21 4
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 Surfnet\StepupMiddleware\ApiBundle\Exception\BadCommandRequestException;
22
use Surfnet\StepupMiddleware\CommandHandlingBundle\Command\Metadata;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
25
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
26
use Symfony\Component\Validator\Validator\ValidatorInterface;
27
28
class MetadataValueResolver implements ValueResolverInterface
29
{
30
    public function __construct(private readonly ValidatorInterface $validator)
31
    {
32
    }
33
34
    /**
35
     * @return Metadata[]
36
     */
37
    public function resolve(Request $request, ArgumentMetadata $argument): iterable
38
    {
39
        $argumentType = $argument->getType();
40
        if (!$argumentType
0 ignored issues
show
Coding Style introduced by
The first expression of a multi-line control structure must be on the line after the opening parenthesis
Loading history...
41
            || $argumentType !== Metadata::class
42
        ) {
43
            return [];
44
        }
45
46
        $data = json_decode($request->getContent());
47
48
        $this->assertIsValidMetadataStructure($data);
49
50
        $metadata = new Metadata();
51
52
        foreach ((array)$data->meta as $property => $value) {
53
            $properlyCasedProperty = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', (string)$property))));
54
            $metadata->$properlyCasedProperty = $value;
55
        }
56
57
        $violations = $this->validator->validate($metadata);
58
        if (count($violations) > 0) {
59
            throw BadCommandRequestException::withViolations('Command metadata is not valid', $violations);
60
        }
61
62
        return [$metadata];
63
    }
64
65
    /**
66
     * @throws BadCommandRequestException
67
     */
68
    private function assertIsValidMetadataStructure(mixed $data): void
69
    {
70
        if (!is_object($data)) {
71
            $type = gettype($data);
72
73
            throw new BadCommandRequestException(
74
                [sprintf('Command metadata is not valid: body must be a JSON object, but is of type %s', $type)],
75
            );
76
        }
77
78
        if (!isset($data->meta)) {
79
            throw new BadCommandRequestException(["Required parameter 'meta' is not set."]);
80
        }
81
82
        if (!is_object($data->meta)) {
83
            $type = gettype($data);
84
85
            throw new BadCommandRequestException([
86
                sprintf(
87
                    "Command metadata is not valid: 'meta' key value must be a JSON object, but is of type %s",
88
                    $type,
89
                ),
90
            ]);
91
        }
92
    }
93
}
94