Completed
Pull Request — master (#290)
by
unknown
36:49 queued 34:03
created

ShowAvailablePaymentMethodsAction   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 77
Duplicated Lines 16.88 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 13
loc 77
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A __invoke() 13 13 2
A getPaymentMethods() 0 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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