RequestDtoValueResolver::supports()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nelexa\RequestDtoBundle\ArgumentResolver;
6
7
use Nelexa\RequestDtoBundle\Dto\RequestDtoInterface;
8
use Nelexa\RequestDtoBundle\Transform\RequestDtoTransform;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
11
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
12
use Symfony\Component\HttpKernel\Exception\HttpException;
13
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
14
use Symfony\Component\Validator\Validator\ValidatorInterface;
15
16
final class RequestDtoValueResolver implements ArgumentValueResolverInterface
17
{
18
    private RequestDtoTransform $transformer;
19
20
    private ValidatorInterface $validator;
21
22 26
    public function __construct(RequestDtoTransform $transformer, ValidatorInterface $validator)
23
    {
24 26
        $this->transformer = $transformer;
25 26
        $this->validator = $validator;
26 26
    }
27
28 26
    public function supports(Request $request, ArgumentMetadata $argument): bool
29
    {
30 26
        return is_a($argument->getType(), RequestDtoInterface::class, true);
31
    }
32
33 25
    private function getSerializeFormat(Request $request): string
34
    {
35 25
        static $supportFormats = ['json', 'xml'];
36 25
        static $defaultFormat = 'json';
37
38 25
        $format = $request->getContentType() ?? $defaultFormat;
39
40 25
        if (!\in_array($format, $supportFormats, true)) {
41 10
            $format = $defaultFormat;
42
        }
43
44 25
        return $format;
45
    }
46
47
    /**
48
     * @return iterable
49
     */
50 25
    public function resolve(Request $request, ArgumentMetadata $argument)
51
    {
52 25
        $format = $this->getSerializeFormat($request);
53
54
        try {
55 25
            $obj = $this->transformer->transform($request, $argument->getType(), $format);
0 ignored issues
show
Bug introduced by
It seems like $argument->getType() can also be of type null; however, parameter $className of Nelexa\RequestDtoBundle\...oTransform::transform() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
            $obj = $this->transformer->transform($request, /** @scrutinizer ignore-type */ $argument->getType(), $format);
Loading history...
56 3
        } catch (\TypeError | NotEncodableValueException $e) {
57 2
            $problemMimeType = 'application/problem+' . $format;
58
59 2
            throw new HttpException(
60 2
                400,
61 2
                'Bad Request',
62
                $e,
63
                [
64 2
                    'Content-Type' => $problemMimeType,
65
                ]
66
            );
67
        }
68
69 22
        $request->attributes->set(
70 22
            ConstraintViolationListValueResolver::REQUEST_ATTR_KEY,
71 22
            $this->validator->validate($obj)
72
        );
73
74 22
        yield $obj;
75 22
    }
76
}
77