GetOrderHistoryAction   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 6
dl 0
loc 61
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A __invoke() 0 24 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\User;
14
15
use BitBag\SyliusVueStorefrontPlugin\Factory\GenericSuccessViewFactoryInterface;
16
use BitBag\SyliusVueStorefrontPlugin\Factory\User\OrderHistoryViewFactoryInterface;
17
use BitBag\SyliusVueStorefrontPlugin\Factory\ValidationErrorViewFactoryInterface;
18
use BitBag\SyliusVueStorefrontPlugin\Processor\RequestProcessorInterface;
19
use BitBag\SyliusVueStorefrontPlugin\Query\User\GetOrderHistory;
20
use BitBag\SyliusVueStorefrontPlugin\Sylius\Provider\LoggedInShopUserProviderInterface;
21
use FOS\RestBundle\View\View;
22
use FOS\RestBundle\View\ViewHandlerInterface;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\HttpFoundation\Response;
25
26
final class GetOrderHistoryAction
27
{
28
    /** @var RequestProcessorInterface */
29
    private $getOrderHistoryRequestProcessor;
30
31
    /** @var ViewHandlerInterface */
32
    private $viewHandler;
33
34
    /** @var ValidationErrorViewFactoryInterface */
35
    private $validationErrorViewFactory;
36
37
    /** @var LoggedInShopUserProviderInterface */
38
    private $loggedInShopUserProvider;
39
40
    /** @var GenericSuccessViewFactoryInterface */
41
    private $genericSuccessViewFactory;
42
43
    /** @var OrderHistoryViewFactoryInterface */
44
    private $orderHistoryViewFactory;
45
46
    public function __construct(
47
        RequestProcessorInterface $getOrderHistoryRequestProcessor,
48
        ViewHandlerInterface $viewHandler,
49
        ValidationErrorViewFactoryInterface $validationErrorViewFactory,
50
        LoggedInShopUserProviderInterface $loggedInShopUserProvider,
51
        GenericSuccessViewFactoryInterface $genericSuccessViewFactory,
52
        OrderHistoryViewFactoryInterface $orderHistoryViewFactory
53
    ) {
54
        $this->getOrderHistoryRequestProcessor = $getOrderHistoryRequestProcessor;
55
        $this->viewHandler = $viewHandler;
56
        $this->validationErrorViewFactory = $validationErrorViewFactory;
57
        $this->loggedInShopUserProvider = $loggedInShopUserProvider;
58
        $this->genericSuccessViewFactory = $genericSuccessViewFactory;
59
        $this->orderHistoryViewFactory = $orderHistoryViewFactory;
60
    }
61
62
    public function __invoke(Request $request): Response
63
    {
64
        $validationResults = $this->getOrderHistoryRequestProcessor->validate($request);
65
66
        if (0 !== count($validationResults)) {
67
            return $this->viewHandler->handle(
68
                View::create(
69
                    $this->validationErrorViewFactory->create($validationResults),
70
                    Response::HTTP_BAD_REQUEST
71
                )
72
            );
73
        }
74
75
        $user = $this->loggedInShopUserProvider->provide()->getCustomer();
76
77
        /** @var GetOrderHistory $query */
78
        $query = $this->getOrderHistoryRequestProcessor->getQuery($request);
79
80
        return $this->viewHandler->handle(View::create(
81
            $this->genericSuccessViewFactory->create(
82
                $this->orderHistoryViewFactory->create($user, $query->paginationParameters())),
83
            Response::HTTP_OK
84
        ));
85
    }
86
}
87