ListStocksAction   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 5
dl 0
loc 68
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A __invoke() 0 31 3
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\Stock;
14
15
use BitBag\SyliusVueStorefrontPlugin\Factory\GenericSuccessViewFactoryInterface;
16
use BitBag\SyliusVueStorefrontPlugin\Factory\Stock\StockViewFactoryInterface;
17
use BitBag\SyliusVueStorefrontPlugin\Factory\ValidationErrorViewFactoryInterface;
18
use BitBag\SyliusVueStorefrontPlugin\Processor\RequestProcessorInterface;
19
use BitBag\SyliusVueStorefrontPlugin\Query\Stock\ListStocks;
20
use FOS\RestBundle\View\View;
21
use FOS\RestBundle\View\ViewHandlerInterface;
22
use Sylius\Component\Core\Repository\ProductVariantRepositoryInterface;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\HttpFoundation\Response;
25
26
final class ListStocksAction
27
{
28
    /** @var RequestProcessorInterface */
29
    private $listStocksRequestProcessor;
30
31
    /** @var ViewHandlerInterface */
32
    private $viewHandler;
33
34
    /** @var ValidationErrorViewFactoryInterface */
35
    private $validationErrorViewFactory;
36
37
    /** @var ProductVariantRepositoryInterface */
38
    private $productVariantRepository;
39
40
    /** @var GenericSuccessViewFactoryInterface */
41
    private $genericSuccessViewFactory;
42
43
    /** @var StockViewFactoryInterface */
44
    private $stockViewFactory;
45
46
    public function __construct(
47
        RequestProcessorInterface $listStocksRequestProcessor,
48
        ViewHandlerInterface $viewHandler,
49
        ValidationErrorViewFactoryInterface $validationErrorViewFactory,
50
        ProductVariantRepositoryInterface $productVariantRepository,
51
        GenericSuccessViewFactoryInterface $genericSuccessViewFactory,
52
        StockViewFactoryInterface $stockViewFactory
53
    ) {
54
        $this->listStocksRequestProcessor = $listStocksRequestProcessor;
55
        $this->viewHandler = $viewHandler;
56
        $this->validationErrorViewFactory = $validationErrorViewFactory;
57
        $this->productVariantRepository = $productVariantRepository;
58
        $this->genericSuccessViewFactory = $genericSuccessViewFactory;
59
        $this->stockViewFactory = $stockViewFactory;
60
    }
61
62
    public function __invoke(Request $request): Response
63
    {
64
        $validationResults = $this->listStocksRequestProcessor->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 ListStocks $listStocksQuery */
74
        $listStocksQuery = $this->listStocksRequestProcessor->getQuery($request);
75
76
        $productsVariantsCodes = $listStocksQuery->SKUsToArray();
77
78
        $productsVariants = $this->productVariantRepository->findby(['code' => $productsVariantsCodes]);
79
80
        if (null === $productsVariantsCodes) {
81
            return $this->viewHandler->handle(View::create(
82
                [], Response::HTTP_NOT_FOUND
83
            ));
84
        }
85
86
        return $this->viewHandler->handle(View::create(
87
            $this->genericSuccessViewFactory->create(
88
                $this->stockViewFactory->createList(...$productsVariants)
89
            ),
90
            Response::HTTP_OK
91
        ));
92
    }
93
}
94