CreateCartAction   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 5
dl 0
loc 58
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A __invoke() 0 21 2
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.io and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusVueStorefrontPlugin\Controller\Cart;
14
15
use BitBag\SyliusVueStorefrontPlugin\Command\Cart\CreateCart;
16
use BitBag\SyliusVueStorefrontPlugin\Factory\GenericSuccessViewFactoryInterface;
17
use BitBag\SyliusVueStorefrontPlugin\Factory\ValidationErrorViewFactoryInterface;
18
use BitBag\SyliusVueStorefrontPlugin\Generator\UuidGeneratorInteface;
19
use BitBag\SyliusVueStorefrontPlugin\Processor\RequestProcessorInterface;
20
use FOS\RestBundle\View\View;
21
use FOS\RestBundle\View\ViewHandlerInterface;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\HttpFoundation\Response;
24
use Symfony\Component\Messenger\MessageBusInterface;
25
26
final class CreateCartAction
27
{
28
    /** @var RequestProcessorInterface */
29
    private $createCartRequestProcessor;
30
31
    /** @var MessageBusInterface */
32
    private $bus;
33
34
    /** @var ViewHandlerInterface */
35
    private $viewHandler;
36
37
    /** @var ValidationErrorViewFactoryInterface */
38
    private $validationErrorViewFactory;
39
40
    /** @var UuidGeneratorInteface */
41
    private $uuidGenerator;
42
43
    /** @var GenericSuccessViewFactoryInterface */
44
    private $genericSuccessViewFactory;
45
46
    public function __construct(
47
        RequestProcessorInterface $createCartRequestProcessor,
48
        MessageBusInterface $bus,
49
        ViewHandlerInterface $viewHandler,
50
        ValidationErrorViewFactoryInterface $validationErrorViewFactory,
51
        UuidGeneratorInteface $uuidGenerator,
52
        GenericSuccessViewFactoryInterface $genericSuccessViewFactory
53
    ) {
54
        $this->createCartRequestProcessor = $createCartRequestProcessor;
55
        $this->bus = $bus;
56
        $this->viewHandler = $viewHandler;
57
        $this->validationErrorViewFactory = $validationErrorViewFactory;
58
        $this->uuidGenerator = $uuidGenerator;
59
        $this->genericSuccessViewFactory = $genericSuccessViewFactory;
60
    }
61
62
    public function __invoke(Request $request): Response
63
    {
64
        $validationResults = $this->createCartRequestProcessor->validate($request);
65
66
        if (0 !== count($validationResults)) {
67
            return $this->viewHandler->handle(View::create(
68
                $this->validationErrorViewFactory->create($validationResults),
69
                Response::HTTP_BAD_REQUEST
70
            ));
71
        }
72
73
        /** @var CreateCart $createCartCommand */
74
        $createCartCommand = $this->createCartRequestProcessor->getCommand($request);
75
        $createCartCommand->setCartId($this->uuidGenerator->generate());
76
77
        $this->bus->dispatch($createCartCommand);
78
79
        return $this->viewHandler->handle(View::create(
80
            $this->genericSuccessViewFactory->create($createCartCommand->cartId())
81
        ));
82
    }
83
}
84