Code Duplication    Length = 77-78 lines in 2 locations

src/Controller/Checkout/ShowAvailablePaymentMethodsAction.php 1 location

@@ 24-100 (lines=77) @@
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpFoundation\Response;
23
24
final class ShowAvailablePaymentMethodsAction
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);
96
        }
97
98
        return $rawPaymentMethods;
99
    }
100
}
101

src/Controller/Checkout/ShowAvailableShippingMethodsAction.php 1 location

@@ 16-93 (lines=78) @@
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
16
final class ShowAvailableShippingMethodsAction
17
{
18
    /**
19
     * @var OrderRepositoryInterface
20
     */
21
    private $cartRepository;
22
23
    /**
24
     * @var ViewHandlerInterface
25
     */
26
    private $viewHandler;
27
28
    /**
29
     * @var ShippingMethodsResolverInterface
30
     */
31
    private $shippingMethodsResolver;
32
33
    /**
34
     * @var ShippingMethodViewFactory
35
     */
36
    private $shippingMethodViewFactory;
37
38
    /**
39
     * @param OrderRepositoryInterface $cartRepository
40
     * @param ViewHandlerInterface $viewHandler
41
     * @param ShippingMethodsResolverInterface $shippingMethodsResolver
42
     * @param ShippingMethodViewFactory $shippingMethodViewFactory
43
     */
44
    public function __construct(
45
        OrderRepositoryInterface $cartRepository,
46
        ViewHandlerInterface $viewHandler,
47
        ShippingMethodsResolverInterface $shippingMethodsResolver,
48
        ShippingMethodViewFactory $shippingMethodViewFactory
49
    ) {
50
        $this->cartRepository = $cartRepository;
51
        $this->viewHandler = $viewHandler;
52
        $this->shippingMethodsResolver = $shippingMethodsResolver;
53
        $this->shippingMethodViewFactory = $shippingMethodViewFactory;
54
    }
55
56
    /**
57
     * @param Request $request
58
     *
59
     * @return Response
60
     */
61
    public function __invoke(Request $request)
62
    {
63
        /** @var OrderInterface $cart */
64
        $cart = $this->cartRepository->findOneBy(['tokenValue' => $request->attributes->get('token')]);
65
66
        $shipments = [];
67
68
        foreach ($cart->getShipments() as $shipment) {
69
            $shipments['shipments'][] = $this->getCalculatedShippingMethods($shipment, $cart->getLocaleCode());
70
        }
71
72
        return $this->viewHandler->handle(View::create($shipments));
73
    }
74
75
    /**
76
     * @param ShipmentInterface $shipment
77
     * @param string $locale
78
     *
79
     * @return array
80
     */
81
    private function getCalculatedShippingMethods(ShipmentInterface $shipment, $locale)
82
    {
83
        $rawShippingMethods = [];
84
85
        /** @var ShippingMethodInterface $shippingMethod */
86
        foreach ($this->shippingMethodsResolver->getSupportedMethods($shipment) as $shippingMethod) {
87
88
            $rawShippingMethods['methods'][$shippingMethod->getCode()] = $this->shippingMethodViewFactory->createWithShippingMethod($shipment, $shippingMethod, $locale);
89
        }
90
91
        return $rawShippingMethods;
92
    }
93
}
94