Passed
Pull Request — master (#3263)
by
unknown
04:12
created

PayloadArgumentResolver   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 34
rs 10
c 1
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 7 2
A supports() 0 23 5
1
<?php
2
3
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\ArgumentResolver;
4
5
use ApiPlatform\Core\Util\RequestAttributesExtractor;
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
8
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
9
10
class PayloadArgumentResolver implements ArgumentValueResolverInterface
11
{
12
    public function supports(Request $request, ArgumentMetadata $argument): bool
13
    {
14
        if ($argument->isVariadic()) {
15
            return false;
16
        }
17
18
        $class = $argument->getType();
19
20
        if (null === $class) {
21
            return false;
22
        }
23
24
        if (null === $request->attributes->get('data')) {
25
            return false;
26
        }
27
28
        $attributes = RequestAttributesExtractor::extractAttributes($request);
29
30
        if ($attributes['resource_class'] === $class) {
31
            return true;
32
        }
33
34
        return is_subclass_of($attributes['resource_class'], $class);
35
    }
36
37
    public function resolve(Request $request, ArgumentMetadata $argument): \Generator
38
    {
39
        if (!$this->supports($request, $argument)) {
40
            throw new \InvalidArgumentException('Given request and argument not supported.');
41
        }
42
43
        yield $request->attributes->get('data');
44
    }
45
}
46