ApiResponderAction   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 4
dl 0
loc 42
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 10 2
A getResponseStatusCode() 0 18 4
1
<?php
2
declare(strict_types=1);
3
4
namespace Eps\Req2CmdBundle\Action;
5
6
use Eps\Req2CmdBundle\CommandBus\CommandBusInterface;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpFoundation\Response;
9
10
class ApiResponderAction
11
{
12
    /**
13
     * @var CommandBusInterface
14
     */
15
    private $commandBus;
16
17
    public function __construct(CommandBusInterface $commandBus)
18
    {
19
        $this->commandBus = $commandBus;
20
    }
21
22
    public function __invoke(Request $request): Response
23
    {
24
        $command = $request->attributes->get('_command');
25
        if ($command === null) {
26
            throw new \InvalidArgumentException('No _command argument found for action');
27
        }
28
        $this->commandBus->handleCommand($command);
29
30
        return new Response('', $this->getResponseStatusCode($request));
31
    }
32
33
    private function getResponseStatusCode(Request $request): int
34
    {
35
        switch ($request->getMethod()) {
36
            case Request::METHOD_PUT:
37
                $statusCode = Response::HTTP_OK;
38
                break;
39
            case Request::METHOD_POST:
40
                $statusCode = Response::HTTP_CREATED;
41
                break;
42
            case Request::METHOD_DELETE:
43
                $statusCode = Response::HTTP_NO_CONTENT;
44
                break;
45
            default:
46
                $statusCode = Response::HTTP_OK;
47
        }
48
49
        return $statusCode;
50
    }
51
}
52