Completed
Push — pull-request/8055 ( ad3cfa )
by Kamil
26:24 queued 01:40
created

CapturePaymentAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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\Bundle\PayumBundle\Action;
13
14
use Payum\Core\Action\GatewayAwareAction;
15
use Payum\Core\Bridge\Spl\ArrayObject;
16
use Payum\Core\Exception\RequestNotSupportedException;
17
use Payum\Core\Model\Payment as PayumPayment;
18
use Payum\Core\Request\Capture;
19
use Payum\Core\Request\Convert;
20
use Sylius\Bundle\PayumBundle\Provider\PaymentDescriptionProviderInterface;
21
use Sylius\Bundle\PayumBundle\Request\GetStatus;
22
use Sylius\Component\Core\Model\OrderInterface;
23
use Sylius\Component\Core\Model\PaymentInterface as SyliusPaymentInterface;
24
25
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...
26
{
27
    /**
28
     * @var PaymentDescriptionProviderInterface
29
     */
30
    private $paymentDescriptionProvider;
31
32
    /**
33
     * @param PaymentDescriptionProviderInterface $paymentDescriptionProvider
34
     */
35
    public function __construct(PaymentDescriptionProviderInterface $paymentDescriptionProvider)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $paymentDescriptionProvider 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...
36
    {
37
        $this->paymentDescriptionProvider = $paymentDescriptionProvider;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     *
43
     * @param $request Capture
44
     */
45
    public function execute($request)
46
    {
47
        RequestNotSupportedException::assertSupports($this, $request);
48
49
        /** @var $payment SyliusPaymentInterface */
50
        $payment = $request->getModel();
51
52
        /** @var OrderInterface $order */
53
        $order = $payment->getOrder();
54
55
        $this->gateway->execute($status = new GetStatus($payment));
56
        if ($status->isNew()) {
57
            try {
58
                $this->gateway->execute($convert = new Convert($payment, 'array', $request->getToken()));
59
                $payment->setDetails($convert->getResult());
60
            } catch (RequestNotSupportedException $e) {
61
                $totalAmount = $order->getTotal();
62
                $payumPayment = new PayumPayment();
63
                $payumPayment->setNumber($order->getNumber());
64
                $payumPayment->setTotalAmount($totalAmount);
65
                $payumPayment->setCurrencyCode($order->getCurrencyCode());
66
                $payumPayment->setClientEmail($order->getCustomer()->getEmail());
67
                $payumPayment->setClientId($order->getCustomer()->getId());
68
                $payumPayment->setDescription($this->paymentDescriptionProvider->getPaymentDescription($payment));
69
                $payumPayment->setDetails($payment->getDetails());
70
71
                $this->gateway->execute($convert = new Convert($payumPayment, 'array', $request->getToken()));
72
                $payment->setDetails($convert->getResult());
73
            }
74
        }
75
76
        $details = ArrayObject::ensureArrayObject($payment->getDetails());
77
78
        try {
79
            $request->setModel($details);
80
            $this->gateway->execute($request);
81
        } finally {
82
            $payment->setDetails((array) $details);
83
        }
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function supports($request)
90
    {
91
        return
92
            $request instanceof Capture &&
93
            $request->getModel() instanceof SyliusPaymentInterface
94
            ;
95
    }
96
}
97