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

PayloadArgumentResolver::resolve()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 7
rs 10
c 1
b 0
f 0
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