ChoosePaymentMethodHandler   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 2
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 58
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A handle() 0 22 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\ShopApiPlugin\Handler;
6
7
use SM\Factory\FactoryInterface;
8
use Sylius\Component\Core\Model\OrderInterface;
9
use Sylius\Component\Core\Model\PaymentMethodInterface;
10
use Sylius\Component\Core\OrderCheckoutTransitions;
11
use Sylius\Component\Core\Repository\OrderRepositoryInterface;
12
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface;
13
use Sylius\ShopApiPlugin\Command\ChoosePaymentMethod;
14
use Webmozart\Assert\Assert;
15
16
final class ChoosePaymentMethodHandler
17
{
18
    /**
19
     * @var OrderRepositoryInterface
20
     */
21
    private $orderRepository;
22
23
    /**
24
     * @var PaymentMethodRepositoryInterface
25
     */
26
    private $paymentMethodRepository;
27
28
    /**
29
     * @var FactoryInterface
30
     */
31
    private $stateMachineFactory;
32
33
    /**
34
     * @param OrderRepositoryInterface $orderRepository
35
     * @param PaymentMethodRepositoryInterface $paymentMethodRepository
36
     * @param FactoryInterface $stateMachineFactory
37
     */
38
    public function __construct(
39
        OrderRepositoryInterface $orderRepository,
40
        PaymentMethodRepositoryInterface $paymentMethodRepository,
41
        FactoryInterface $stateMachineFactory
42
    ) {
43
        $this->orderRepository = $orderRepository;
44
        $this->paymentMethodRepository = $paymentMethodRepository;
45
        $this->stateMachineFactory = $stateMachineFactory;
46
    }
47
48
    /**
49
     * @param ChoosePaymentMethod $choosePaymentMethod
50
     */
51
    public function handle(ChoosePaymentMethod $choosePaymentMethod)
52
    {
53
        /** @var OrderInterface $cart */
54
        $cart = $this->orderRepository->findOneBy(['tokenValue' => $choosePaymentMethod->orderToken()]);
55
56
        Assert::notNull($cart, 'Cart has not been found.');
57
58
        $stateMachine = $this->stateMachineFactory->get($cart, OrderCheckoutTransitions::GRAPH);
59
60
        Assert::true($stateMachine->can(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT), 'Order cannot have payment method assigned.');
61
62
        /** @var PaymentMethodInterface $paymentMethod */
63
        $paymentMethod = $this->paymentMethodRepository->findOneBy(['code' => $choosePaymentMethod->paymentMethod()]);
64
65
        Assert::notNull($paymentMethod, 'Payment method has not been found');
66
        Assert::true(isset($cart->getPayments()[$choosePaymentMethod->paymentIdentifier()]), 'Can not find payment with given identifier.');
67
68
        $payment = $cart->getPayments()[$choosePaymentMethod->paymentIdentifier()];
69
70
        $payment->setMethod($paymentMethod);
71
        $stateMachine->apply(OrderCheckoutTransitions::TRANSITION_SELECT_PAYMENT);
72
    }
73
}
74