1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Sylius\SyliusShopApiPlugin\Controller\Checkout; |
6
|
|
|
|
7
|
|
|
use FOS\RestBundle\View\View; |
8
|
|
|
use FOS\RestBundle\View\ViewHandlerInterface; |
9
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
10
|
|
|
use Sylius\Component\Core\Model\PaymentInterface; |
11
|
|
|
use Sylius\Component\Core\Model\PaymentMethodInterface; |
12
|
|
|
use Sylius\Component\Core\Repository\OrderRepositoryInterface; |
13
|
|
|
use Sylius\Component\Payment\Resolver\PaymentMethodsResolverInterface; |
14
|
|
|
use Sylius\SyliusShopApiPlugin\Factory\PaymentMethodViewFactoryInterface; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
17
|
|
|
|
18
|
|
|
final class ShowAvailablePaymentMethodsAction |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var OrderRepositoryInterface |
22
|
|
|
*/ |
23
|
|
|
private $cartRepository; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ViewHandlerInterface |
27
|
|
|
*/ |
28
|
|
|
private $viewHandler; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var PaymentMethodsResolverInterface |
32
|
|
|
*/ |
33
|
|
|
private $paymentMethodsResolver; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var PaymentMethodViewFactoryInterface |
37
|
|
|
*/ |
38
|
|
|
private $paymentMethodViewFactory; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param OrderRepositoryInterface $cartRepository |
42
|
|
|
* @param ViewHandlerInterface $viewHandler |
43
|
|
|
* @param PaymentMethodsResolverInterface $paymentMethodResolver |
44
|
|
|
* @param PaymentMethodViewFactoryInterface $paymentMethodViewFactory |
45
|
|
|
*/ |
46
|
|
|
public function __construct( |
47
|
|
|
OrderRepositoryInterface $cartRepository, |
48
|
|
|
ViewHandlerInterface $viewHandler, |
49
|
|
|
PaymentMethodsResolverInterface $paymentMethodResolver, |
50
|
|
|
PaymentMethodViewFactoryInterface $paymentMethodViewFactory |
51
|
|
|
) { |
52
|
|
|
$this->cartRepository = $cartRepository; |
53
|
|
|
$this->viewHandler = $viewHandler; |
54
|
|
|
$this->paymentMethodsResolver = $paymentMethodResolver; |
55
|
|
|
$this->paymentMethodViewFactory = $paymentMethodViewFactory; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param Request $request |
60
|
|
|
* |
61
|
|
|
* @return Response |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public function __invoke(Request $request) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
/** @var OrderInterface $cart */ |
66
|
|
|
$cart = $this->cartRepository->findOneBy(['tokenValue' => $request->attributes->get('token')]); |
67
|
|
|
|
68
|
|
|
$payments = []; |
69
|
|
|
|
70
|
|
|
foreach ($cart->getPayments() as $payment) { |
71
|
|
|
$payments['payments'][] = $this->getPaymentMethods($payment, $cart->getLocaleCode()); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->viewHandler->handle(View::create($payments)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param PaymentInterface $payment |
79
|
|
|
* @param string $locale |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
private function getPaymentMethods(PaymentInterface $payment, $locale) |
84
|
|
|
{ |
85
|
|
|
$rawPaymentMethods = []; |
86
|
|
|
|
87
|
|
|
/** @var PaymentMethodInterface $paymentMethod */ |
88
|
|
|
foreach ($this->paymentMethodsResolver->getSupportedMethods($payment) as $paymentMethod) { |
89
|
|
|
$rawPaymentMethods['methods'][$paymentMethod->getCode()] = $this->paymentMethodViewFactory->create($paymentMethod, $locale); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $rawPaymentMethods; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.