Completed
Push — master ( 41948a...5bb6bb )
by Łukasz
17:08 queued 13:33
created

getPaymentMethods()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace Sylius\ShopApiPlugin\Controller\Checkout;
4
5
use FOS\RestBundle\View\View;
6
use FOS\RestBundle\View\ViewHandlerInterface;
7
use Sylius\Component\Core\Factory\PaymentMethodFactoryInterface;
8
use Sylius\Component\Core\Model\OrderInterface;
9
use Sylius\Component\Core\Model\PaymentInterface;
10
use Sylius\Component\Core\Model\PaymentMethodInterface;
11
use Sylius\Component\Core\Model\ShipmentInterface;
12
use Sylius\Component\Core\Model\ShippingMethodInterface;
13
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
14
use Sylius\Component\Payment\Resolver\PaymentMethodsResolverInterface;
15
use Sylius\Component\Registry\ServiceRegistryInterface;
16
use Sylius\Component\Shipping\Calculator\CalculatorInterface;
17
use Sylius\Component\Shipping\Resolver\ShippingMethodsResolverInterface;
18
use Sylius\ShopApiPlugin\Factory\PaymentMethodViewFactoryInterface;
19
use Sylius\ShopApiPlugin\View\PaymentMethodView;
20
use Sylius\ShopApiPlugin\View\ShipmentMethodView;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpFoundation\Response;
23
24 View Code Duplication
final class ShowAvailablePaymentMethodsAction
1 ignored issue
show
Duplication introduced by
This class 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...
25
{
26
    /**
27
     * @var OrderRepositoryInterface
28
     */
29
    private $cartRepository;
30
31
    /**
32
     * @var ViewHandlerInterface
33
     */
34
    private $viewHandler;
35
36
    /**
37
     * @var PaymentMethodsResolverInterface
38
     */
39
    private $paymentMethodsResolver;
40
41
    /**
42
     * @var PaymentMethodViewFactoryInterface
43
     */
44
    private $paymentMethodViewFactory;
45
46
    /**
47
     * @param OrderRepositoryInterface $cartRepository
48
     * @param ViewHandlerInterface $viewHandler
49
     * @param PaymentMethodsResolverInterface $paymentMethodResolver
50
     * @param PaymentMethodViewFactoryInterface $paymentMethodViewFactory
51
     */
52
    public function __construct(
53
        OrderRepositoryInterface $cartRepository,
54
        ViewHandlerInterface $viewHandler,
55
        PaymentMethodsResolverInterface $paymentMethodResolver,
56
        PaymentMethodViewFactoryInterface $paymentMethodViewFactory
57
    ) {
58
        $this->cartRepository = $cartRepository;
59
        $this->viewHandler = $viewHandler;
60
        $this->paymentMethodsResolver = $paymentMethodResolver;
61
        $this->paymentMethodViewFactory = $paymentMethodViewFactory;
62
    }
63
64
    /**
65
     * @param Request $request
66
     *
67
     * @return Response
68
     */
69
    public function __invoke(Request $request)
70
    {
71
        /** @var OrderInterface $cart */
72
        $cart = $this->cartRepository->findOneBy(['tokenValue' => $request->attributes->get('token')]);
73
74
        $payments = [];
75
76
        foreach ($cart->getPayments() as $payment) {
77
            $payments['payments'][] = $this->getPaymentMethods($payment, $cart->getLocaleCode());
78
        }
79
80
        return $this->viewHandler->handle(View::create($payments));
81
    }
82
83
    /**
84
     * @param PaymentInterface $payment
85
     * @param string $locale
86
     *
87
     * @return array
88
     */
89
    private function getPaymentMethods(PaymentInterface $payment, $locale)
90
    {
91
        $rawPaymentMethods = [];
92
93
        /** @var PaymentMethodInterface $paymentMethod */
94
        foreach ($this->paymentMethodsResolver->getSupportedMethods($payment) as $paymentMethod) {
95
            $rawPaymentMethods['methods'][$paymentMethod->getCode()] = $this->paymentMethodViewFactory->create($paymentMethod, $locale);
0 ignored issues
show
Compatibility introduced by
$paymentMethod of type object<Sylius\Component\...PaymentMethodInterface> is not a sub-type of object<Sylius\Component\...PaymentMethodInterface>. It seems like you assume a child interface of the interface Sylius\Component\Payment...\PaymentMethodInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
96
        }
97
98
        return $rawPaymentMethods;
99
    }
100
}
101