GetPaymentMethodsAction   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

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

2 Methods

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