Completed
Push — master ( 99b9b3...0bcb3b )
by Kamil
18:37
created

OrderPaymentProvider::getDefaultPaymentMethod()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 3
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Component\Core\Payment\Provider;
13
14
use SM\Factory\FactoryInterface as StateMachineFactoryInterface;
15
use Sylius\Component\Core\Model\OrderInterface;
16
use Sylius\Component\Core\Model\PaymentInterface;
17
use Sylius\Component\Core\Model\PaymentMethodInterface;
18
use Sylius\Component\Core\Payment\Exception\NotProvidedOrderPaymentException;
19
use Sylius\Component\Payment\Exception\UnresolvedDefaultPaymentMethodException;
20
use Sylius\Component\Payment\Factory\PaymentFactoryInterface;
21
use Sylius\Component\Payment\PaymentTransitions;
22
use Sylius\Component\Payment\Resolver\DefaultPaymentMethodResolverInterface;
23
use Sylius\Component\Resource\StateMachine\StateMachineInterface;
24
25
/**
26
 * @author Mateusz Zalewski <[email protected]>
27
 */
28
final class OrderPaymentProvider implements OrderPaymentProviderInterface
29
{
30
    /**
31
     * @var DefaultPaymentMethodResolverInterface
32
     */
33
    private $defaultPaymentMethodResolver;
34
35
    /**
36
     * @var PaymentFactoryInterface
37
     */
38
    private $paymentFactory;
39
40
    /**
41
     * @var StateMachineFactoryInterface
42
     */
43
    private $stateMachineFactory;
44
45
    /**
46
     * @param DefaultPaymentMethodResolverInterface $defaultPaymentMethodResolver
47
     * @param PaymentFactoryInterface $paymentFactory
48
     * @param StateMachineFactoryInterface $stateMachineFactory
49
     */
50
    public function __construct(
51
        DefaultPaymentMethodResolverInterface $defaultPaymentMethodResolver,
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $defaultPaymentMethodResolver exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
52
        PaymentFactoryInterface $paymentFactory,
53
        StateMachineFactoryInterface $stateMachineFactory
54
    ) {
55
        $this->defaultPaymentMethodResolver = $defaultPaymentMethodResolver;
56
        $this->paymentFactory = $paymentFactory;
57
        $this->stateMachineFactory = $stateMachineFactory;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function provideOrderPayment(OrderInterface $order, $targetState)
64
    {
65
        /** @var PaymentInterface $payment */
66
        $payment = $this->paymentFactory->createWithAmountAndCurrencyCode($order->getTotal(), $order->getCurrencyCode());
67
68
        $paymentMethod = $this->getDefaultPaymentMethod($payment, $order);
69
        $lastPayment = $this->getLastPayment($order);
70
71
        if (null !== $lastPayment) {
72
            $paymentMethod = $lastPayment->getMethod();
73
        }
74
75
        if (null === $paymentMethod) {
76
            throw new NotProvidedOrderPaymentException();
77
        }
78
79
        $payment->setMethod($paymentMethod);
80
        $this->applyRequiredTransition($payment, $targetState);
81
82
        return $payment;
83
    }
84
85
    /**
86
     * @param OrderInterface $order
87
     *
88
     * @return PaymentInterface|null
89
     */
90
    private function getLastPayment(OrderInterface $order)
91
    {
92
        $lastCancelledPayment = $order->getLastPayment(PaymentInterface::STATE_CANCELLED);
93
        if (null !== $lastCancelledPayment) {
94
            return $lastCancelledPayment;
95
        }
96
97
        $lastFailedPayment = $order->getLastPayment(PaymentInterface::STATE_FAILED);
98
        if (null !== $lastFailedPayment) {
99
            return $lastFailedPayment;
100
        }
101
102
        return null;
103
    }
104
105
    /**
106
     * @param PaymentInterface $payment
107
     * @param OrderInterface $order
108
     *
109
     * @return PaymentMethodInterface|null
0 ignored issues
show
Documentation introduced by
Should the return type not be \Sylius\Component\Paymen...entMethodInterface|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
110
     */
111
    private function getDefaultPaymentMethod(PaymentInterface $payment, OrderInterface $order)
112
    {
113
        try {
114
            $payment->setOrder($order);
115
            $paymentMethod = $this->defaultPaymentMethodResolver->getDefaultPaymentMethod($payment);
116
117
            return $paymentMethod;
118
        } catch (UnresolvedDefaultPaymentMethodException $exception) {
119
            return null;
120
        }
121
    }
122
123
    /**
124
     * @param PaymentInterface $payment
125
     * @param string $targetState
126
     */
127
    private function applyRequiredTransition(PaymentInterface $payment, $targetState)
128
    {
129
        if ($targetState === $payment->getState()) {
130
            return;
131
        }
132
133
        /** @var StateMachineInterface $stateMachine */
134
        $stateMachine = $this->stateMachineFactory->get($payment, PaymentTransitions::GRAPH);
135
136
        $targetTransition = $stateMachine->getTransitionToState($targetState);
137
        if (null !== $targetTransition) {
138
            $stateMachine->apply($targetTransition);
139
        }
140
    }
141
}
142