DeserializedObjectArgumentResolver   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 23
c 2
b 0
f 0
dl 0
loc 61
rs 10
wmc 13

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isDeserializationObjectType() 0 7 2
A supports() 0 20 5
A isDeserializationObjectArgumentName() 0 7 2
A hasDeserializedObjectAttribute() 0 7 2
A resolve() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the OpenapiBundle package.
7
 *
8
 * (c) Niels Nijens <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Nijens\OpenapiBundle\Deserialization\ArgumentResolver;
15
16
use Nijens\OpenapiBundle\Deserialization\Attribute\DeserializedObject;
17
use Nijens\OpenapiBundle\Deserialization\DeserializationContext;
18
use Nijens\OpenapiBundle\Routing\RouteContext;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
21
22
class DeserializedObjectArgumentResolver implements ValueResolverInterface
23
{
24
    /**
25
     * TODO: Remove when support for Symfony 6.4 is dropped.
26
     */
27
    public function supports(Request $request, ArgumentMetadata $argument): bool
28
    {
29
        if ($request->attributes->has(DeserializationContext::REQUEST_ATTRIBUTE) === false) {
30
            return false;
31
        }
32
33
        $routeContext = $request->attributes->get(RouteContext::REQUEST_ATTRIBUTE);
34
        if ($this->isDeserializationObjectType($argument, $routeContext)) {
35
            return true;
36
        }
37
38
        if ($this->hasDeserializedObjectAttribute($argument)) {
39
            return true;
40
        }
41
42
        if ($this->isDeserializationObjectArgumentName($argument, $routeContext)) {
43
            return true;
44
        }
45
46
        return false;
47
    }
48
49
    public function resolve(Request $request, ArgumentMetadata $argument): iterable
50
    {
51
        if ($this->supports($request, $argument) === false) {
52
            return;
53
        }
54
55
        yield $request->attributes->get(DeserializationContext::REQUEST_ATTRIBUTE);
56
    }
57
58
    private function isDeserializationObjectType(ArgumentMetadata $argument, ?array $routeContext): bool
59
    {
60
        if (isset($routeContext[RouteContext::DESERIALIZATION_OBJECT]) === false) {
61
            return false;
62
        }
63
64
        return $routeContext[RouteContext::DESERIALIZATION_OBJECT] === $argument->getType();
65
    }
66
67
    private function hasDeserializedObjectAttribute(ArgumentMetadata $argument): bool
68
    {
69
        if (method_exists($argument, 'getAttributes')) {
70
            return count($argument->getAttributes(DeserializedObject::class, ArgumentMetadata::IS_INSTANCEOF)) > 0;
71
        }
72
73
        return false;
74
    }
75
76
    private function isDeserializationObjectArgumentName(ArgumentMetadata $argument, ?array $routeContext): bool
77
    {
78
        if (isset($routeContext[RouteContext::DESERIALIZATION_OBJECT_ARGUMENT_NAME]) === false) {
79
            return false;
80
        }
81
82
        return $routeContext[RouteContext::DESERIALIZATION_OBJECT_ARGUMENT_NAME] === $argument->getName();
83
    }
84
}
85