GetAppliedCouponAction::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 5
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\GenericSuccessViewFactoryInterface;
16
use BitBag\SyliusVueStorefrontPlugin\Factory\ValidationErrorViewFactoryInterface;
17
use BitBag\SyliusVueStorefrontPlugin\Processor\RequestProcessorInterface;
18
use FOS\RestBundle\View\View;
19
use FOS\RestBundle\View\ViewHandlerInterface;
20
use Sylius\Component\Core\Model\OrderInterface;
21
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\HttpFoundation\Response;
24
25
final class GetAppliedCouponAction
26
{
27
    /** @var RequestProcessorInterface */
28
    private $getAppliedCouponProcessor;
29
30
    /** @var ViewHandlerInterface */
31
    private $viewHandler;
32
33
    /** @var ValidationErrorViewFactoryInterface */
34
    private $validationErrorViewFactory;
35
36
    /** @var OrderRepositoryInterface */
37
    private $orderRepository;
38
39
    /** @var GenericSuccessViewFactoryInterface */
40
    private $genericSuccessViewFactory;
41
42
    public function __construct(
43
        RequestProcessorInterface $getAppliedCouponProcessor,
44
        ViewHandlerInterface $viewHandler,
45
        ValidationErrorViewFactoryInterface $validationErrorViewFactory,
46
        OrderRepositoryInterface $orderRepository,
47
        GenericSuccessViewFactoryInterface $genericSuccessViewFactory
48
    ) {
49
        $this->getAppliedCouponProcessor = $getAppliedCouponProcessor;
50
        $this->viewHandler = $viewHandler;
51
        $this->validationErrorViewFactory = $validationErrorViewFactory;
52
        $this->orderRepository = $orderRepository;
53
        $this->genericSuccessViewFactory = $genericSuccessViewFactory;
54
    }
55
56
    public function __invoke(Request $request): Response
57
    {
58
        $validationResults = $this->getAppliedCouponProcessor->validate($request);
59
60
        if (0 !== count($validationResults)) {
61
            return $this->viewHandler->handle(View::create(
62
                $this->validationErrorViewFactory->create($validationResults),
63
                Response::HTTP_BAD_REQUEST
64
            ));
65
        }
66
67
        $code = $this->getAppliedCouponProcessor->getQuery($request);
68
69
        /** @var OrderInterface $cart */
70
        $cart = $this->orderRepository->findOneBy(['tokenValue' => $code->cartId(), 'state' => OrderInterface::STATE_CART]);
71
72
        $promotionCoupon = $cart->getPromotionCoupon() ?? false;
73
74
        if ($promotionCoupon) {
75
            $promotionCoupon = $promotionCoupon->getCode();
76
        }
77
78
        return $this->viewHandler->handle(View::create(
79
            $this->genericSuccessViewFactory->create($promotionCoupon)
80
        ));
81
    }
82
}
83