JMSSerializerCommandExtractor   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A extractFromRequest() 0 15 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Eps\Req2CmdBundle\CommandExtractor;
5
6
use JMS\Serializer\ArrayTransformerInterface;
7
use JMS\Serializer\SerializerInterface;
8
use Symfony\Component\HttpFoundation\Request;
9
10
class JMSSerializerCommandExtractor implements CommandExtractorInterface
11
{
12
    /**
13
     * @var SerializerInterface
14
     */
15
    private $jmsSerializer;
16
17
    /**
18
     * @var ArrayTransformerInterface
19
     */
20
    private $jmsArrayTransformer;
21
22
    public function __construct(SerializerInterface $jmsSerializer, ArrayTransformerInterface $jmsArrayTransformer)
23
    {
24
        $this->jmsSerializer = $jmsSerializer;
25
        $this->jmsArrayTransformer = $jmsArrayTransformer;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     * @throws \LogicException
31
     */
32
    public function extractFromRequest(Request $request, string $commandClass, array $additionalProps = [])
33
    {
34
        if (empty($request->getContent())) {
35
            return $this->jmsArrayTransformer->fromArray($additionalProps, $commandClass);
36
        }
37
38
        $decodedContent = $this->jmsSerializer->deserialize(
39
            $request->getContent(),
40
            'array',
41
            $request->getRequestFormat()
42
        );
43
        $finalProps = array_merge($decodedContent, $additionalProps);
44
45
        return $this->jmsArrayTransformer->fromArray($finalProps, $commandClass);
46
    }
47
}
48