CapturePaymentAction::execute()   B
last analyzed

Complexity

Conditions 5
Paths 20

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 8.6707
c 0
b 0
f 0
cc 5
nc 20
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace PH\Bundle\PayumBundle\Action;
6
7
use Payum\Core\Action\GatewayAwareAction;
8
use Payum\Core\Bridge\Spl\ArrayObject;
9
use Payum\Core\Exception\RequestNotSupportedException;
10
use Payum\Core\Model\Payment as PayumPayment;
11
use Payum\Core\Request\Capture;
12
use Payum\Core\Request\Convert;
13
use PH\Bundle\PayumBundle\Provider\PaymentDescriptionProviderInterface;
14
use PH\Component\Core\Model\SubscriptionInterface;
15
use PH\Component\Core\Model\PaymentInterface;
16
use Sylius\Bundle\PayumBundle\Request\GetStatus;
17
18
final class CapturePaymentAction extends GatewayAwareAction
0 ignored issues
show
Deprecated Code introduced by
The class Payum\Core\Action\GatewayAwareAction has been deprecated with message: since 1.3 will be removed in 2.0. Use trait+interface in your classes.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
19
{
20
    /**
21
     * @var PaymentDescriptionProviderInterface
22
     */
23
    private $descriptionProvider;
24
25
    /**
26
     * CapturePaymentAction constructor.
27
     *
28
     * @param PaymentDescriptionProviderInterface $descriptionProvider
29
     */
30
    public function __construct(PaymentDescriptionProviderInterface $descriptionProvider)
31
    {
32
        $this->descriptionProvider = $descriptionProvider;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function execute($request)
39
    {
40
        RequestNotSupportedException::assertSupports($this, $request);
41
42
        /** @var PaymentInterface $payment */
43
        $payment = $request->getModel();
44
        $paymentMethodConfig = $payment->getMethod()->getGatewayConfig()->getConfig();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Component\Payment...\PaymentMethodInterface as the method getGatewayConfig() does only exist in the following implementations of said interface: PH\Component\Core\Model\PaymentMethod, Sylius\Component\Core\Model\PaymentMethod.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
45
46
        /** @var SubscriptionInterface $subscription */
47
        $subscription = $payment->getSubscription();
48
49
        $this->gateway->execute($status = new GetStatus($payment));
50
51
        if ($status->isNew()) {
52
            try {
53
                $this->gateway->execute($convert = new Convert($payment, 'array', $request->getToken()));
54
                $payment->setDetails($convert->getResult());
55
            } catch (RequestNotSupportedException $e) {
56
                $totalAmount = $subscription->getTotal();
57
                $payumPayment = new PayumPayment();
58
                $payumPayment->setTotalAmount($totalAmount);
59
                $payumPayment->setCurrencyCode($subscription->getCurrencyCode());
60
                $payumPayment->setDescription($this->descriptionProvider->getPaymentDescription($payment));
61
62
                $startDate = $subscription->getStartDate();
63
                if (null === $startDate) {
64
                    $startDate = new \DateTime();
65
                }
66
67
                $details = [
68
                    'interval' => $subscription->getInterval(),
69
                    'startDate' => $startDate->format('Y-m-d'),
70
                ];
71
72
                if (isset($paymentMethodConfig['method'])) {
73
                    $details['method'] = $paymentMethodConfig['method'];
74
                }
75
76
                $details['type'] = $subscription->getType();
77
78
                $payumPayment->setDetails(array_merge($payment->getDetails(), $details));
79
                $this->gateway->execute($convert = new Convert($payumPayment, 'array', $request->getToken()));
80
                $payment->setDetails($convert->getResult());
81
            }
82
        }
83
84
        $details = ArrayObject::ensureArrayObject($payment->getDetails());
85
86
        try {
87
            $request->setModel($details);
88
            $this->gateway->execute($request);
89
        } finally {
90
            $payment->setDetails((array) $details);
91
        }
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function supports($request)
98
    {
99
        return
100
            $request instanceof Capture &&
101
            $request->getModel() instanceof PaymentInterface
102
        ;
103
    }
104
}
105