SerializerCommandExtractor   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 53
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A extractFromRequest() 0 14 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Eps\Req2CmdBundle\CommandExtractor;
5
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\Serializer\Encoder\DecoderInterface;
8
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
9
use Symfony\Component\Serializer\SerializerInterface;
10
11
class SerializerCommandExtractor implements CommandExtractorInterface
12
{
13
    /**
14
     * @var SerializerInterface
15
     */
16
    private $serializer;
17
18
    /**
19
     * @var DecoderInterface
20
     */
21
    private $decoder;
22
23
    /**
24
     * @var DenormalizerInterface
25
     */
26
    private $denormalizer;
27
28
    /**
29
     * SerializerCommandExtractor constructor.
30
     * @param SerializerInterface $serializer
31
     * @param DecoderInterface $decoder
32
     * @param DenormalizerInterface $denormalizer
33
     */
34
    public function __construct(
35
        SerializerInterface $serializer,
36
        DecoderInterface $decoder,
37
        DenormalizerInterface $denormalizer
38
    ) {
39
        $this->serializer = $serializer;
40
        $this->decoder = $decoder;
41
        $this->denormalizer = $denormalizer;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     * @throws \LogicException
47
     * @throws \Symfony\Component\Serializer\Exception\UnexpectedValueException
48
     */
49
    public function extractFromRequest(Request $request, string $commandClass, array $additionalProps = [])
50
    {
51
        $requestContent = $request->getContent();
52
        $requestFormat = $request->getRequestFormat();
53
54
        if (empty($requestContent)) {
55
            return $this->denormalizer->denormalize($additionalProps, $commandClass, $requestFormat);
56
        }
57
58
        $decodedContent = $this->decoder->decode($requestContent, $requestFormat);
0 ignored issues
show
Bug introduced by
It seems like $requestContent defined by $request->getContent() on line 51 can also be of type resource; however, Symfony\Component\Serial...oderInterface::decode() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
59
        $finalProps = array_merge($decodedContent, $additionalProps);
60
61
        return $this->denormalizer->denormalize($finalProps, $commandClass, $requestFormat);
62
    }
63
}
64