SyncTotalsAction   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A __invoke() 0 29 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\Factory\Cart\Totals\TotalsViewFactory;
16
use BitBag\SyliusVueStorefrontPlugin\Factory\GenericSuccessViewFactoryInterface;
17
use BitBag\SyliusVueStorefrontPlugin\Factory\ValidationErrorViewFactoryInterface;
18
use BitBag\SyliusVueStorefrontPlugin\Processor\RequestProcessorInterface;
19
use BitBag\SyliusVueStorefrontPlugin\Query\Cart\SyncTotals;
20
use FOS\RestBundle\View\View;
21
use FOS\RestBundle\View\ViewHandlerInterface;
22
use Sylius\Component\Core\Model\OrderInterface;
23
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
24
use Symfony\Component\HttpFoundation\Request;
25
use Symfony\Component\HttpFoundation\Response;
26
27
final class SyncTotalsAction
28
{
29
    /** @var RequestProcessorInterface */
30
    private $syncTotalsRequestProcessor;
31
32
    /** @var ViewHandlerInterface */
33
    private $viewHandler;
34
35
    /** @var ValidationErrorViewFactoryInterface */
36
    private $validationErrorViewFactory;
37
38
    /** @var OrderRepositoryInterface */
39
    private $orderRepository;
40
41
    /** @var GenericSuccessViewFactoryInterface */
42
    private $genericSuccessViewFactory;
43
44
    /** @var TotalsViewFactory */
45
    private $totalsViewFactory;
46
47
    public function __construct(
48
        RequestProcessorInterface $syncTotalsRequestProcessor,
49
        ViewHandlerInterface $viewHandler,
50
        ValidationErrorViewFactoryInterface $validationErrorViewFactory,
51
        OrderRepositoryInterface $orderRepository,
52
        GenericSuccessViewFactoryInterface $genericSuccessViewFactory,
53
        TotalsViewFactory $totalsViewFactory
54
    ) {
55
        $this->syncTotalsRequestProcessor = $syncTotalsRequestProcessor;
56
        $this->viewHandler = $viewHandler;
57
        $this->validationErrorViewFactory = $validationErrorViewFactory;
58
        $this->orderRepository = $orderRepository;
59
        $this->genericSuccessViewFactory = $genericSuccessViewFactory;
60
        $this->totalsViewFactory = $totalsViewFactory;
61
    }
62
63
    public function __invoke(Request $request): Response
64
    {
65
        $validationResults = $this->syncTotalsRequestProcessor->validate($request);
66
67
        if (0 !== count($validationResults)) {
68
            return $this->viewHandler->handle(View::create(
69
                $this->validationErrorViewFactory->create($validationResults),
70
                Response::HTTP_BAD_REQUEST
71
            ));
72
        }
73
74
        /** @var SyncTotals $syncTotalsQuery */
75
        $syncTotalsQuery = $this->syncTotalsRequestProcessor->getQuery($request);
76
77
        /** @var OrderInterface $order */
78
        $order = $this->orderRepository->findOneBy(
79
            [
80
                'tokenValue' => $syncTotalsQuery->cartId(),
81
                'state' => OrderInterface::STATE_CART,
82
            ]
83
        );
84
85
        return $this->viewHandler->handle(View::create(
86
            $this->genericSuccessViewFactory->create(
87
                $this->totalsViewFactory->create($order)
88
            ),
89
            Response::HTTP_OK
90
        ));
91
    }
92
}
93