Completed
Push — master ( 9dfe4a...9f73a9 )
by Alexey
03:27
created

RequestBodyObjectValueResolver::serialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 11
c 1
b 0
f 1
nc 2
nop 2
dl 0
loc 15
ccs 10
cts 10
cp 1
crap 2
rs 9.9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nelexa\RequestDtoBundle\ArgumentResolver;
6
7
use Nelexa\RequestDtoBundle\Dto\RequestBodyObjectInterface;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
10
use Symfony\Component\HttpKernel\Exception\HttpException;
11
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
12
13
final class RequestBodyObjectValueResolver extends AbstractObjectValueResolver
14
{
15 19
    public function supports(Request $request, ArgumentMetadata $argument): bool
16
    {
17 19
        return is_a($argument->getType(), RequestBodyObjectInterface::class, true);
18
    }
19
20 7
    protected function serialize(Request $request, ArgumentMetadata $argument): object
21
    {
22
        try {
23 7
            return $this->serializer->deserialize(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->serializer...ializeFormat($request)) could return the type array which is incompatible with the type-hinted return object. Consider adding an additional type-check to rule them out.
Loading history...
24 7
                $request->getContent(),
25 7
                $argument->getType(),
0 ignored issues
show
Bug introduced by
It seems like $argument->getType() can also be of type null; however, parameter $type of Symfony\Component\Serial...nterface::deserialize() 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

25
                /** @scrutinizer ignore-type */ $argument->getType(),
Loading history...
26 7
                $this->getSerializeFormat($request)
27
            );
28 1
        } catch (NotEncodableValueException $e) {
29 1
            throw new HttpException(
30 1
                400,
31 1
                'Bad Request',
32
                $e,
33
                [
34 1
                    'Content-Type' => 'application/problem+json',
35
                ]
36
            );
37
        }
38
    }
39
}
40