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

RequestBodyObjectValueResolver   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 13
c 1
b 0
f 1
dl 0
loc 22
ccs 12
cts 12
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 3 1
A serialize() 0 15 2
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