Completed
Push — master ( 99fd04...6a3a72 )
by Jakub
02:38
created

ExtractCommandFromRequestListener   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A onKernelRequest() 0 17 3
1
<?php
2
declare(strict_types=1);
3
4
namespace Eps\Req2CmdBundle\EventListener;
5
6
use Eps\Req2CmdBundle\CommandExtractor\CommandExtractorInterface;
7
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
8
9
class ExtractCommandFromRequestListener
10
{
11
    public const API_RESPONDER_CONTROLLER = 'eps.req2cmd.action.api_responder';
12
    public const CMD_CLASS_PARAM = '_command_class';
13
    public const CMD_PARAM = '_command';
14
    private const CONTROLLER_PARAM = '_controller';
15
16
    /**
17
     * @var CommandExtractorInterface
18
     */
19
    private $extractor;
20
21
    public function __construct(CommandExtractorInterface $extractor)
22
    {
23
        $this->extractor = $extractor;
24
    }
25
26
    public function onKernelRequest(GetResponseEvent $event): void
27
    {
28
        $request = $event->getRequest();
29
        $commandClass = $request->attributes->get(self::CMD_CLASS_PARAM);
30
        if ($commandClass === null) {
31
            return;
32
        }
33
34
        $command = $this->extractor->extractFromRequest($request, $commandClass);
35
36
        $request->attributes->set(self::CMD_PARAM, $command);
37
        $request->attributes->remove(self::CMD_CLASS_PARAM);
38
39
        if (!$request->attributes->has(self::CONTROLLER_PARAM)) {
40
            $request->attributes->set(self::CONTROLLER_PARAM, self::API_RESPONDER_CONTROLLER);
41
        }
42
    }
43
}
44