ConvertPaymentAction   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 5
dl 0
loc 71
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A execute() 0 26 2
A supports() 0 8 3
A formatPrice() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PH\Bundle\PayumBundle\Action\Paypal\ExpressCheckout;
6
7
use Payum\Core\Action\ActionInterface;
8
use Payum\Core\Exception\RequestNotSupportedException;
9
use Payum\Core\Request\Convert;
10
use PH\Bundle\PayumBundle\Provider\PaymentDescriptionProviderInterface;
11
use PH\Component\Core\Model\SubscriptionInterface;
12
use PH\Component\Core\Model\PaymentInterface;
13
14
final class ConvertPaymentAction implements ActionInterface
15
{
16
    /**
17
     * @var PaymentDescriptionProviderInterface
18
     */
19
    private $descriptionProvider;
20
21
    /**
22
     * CapturePaymentAction constructor.
23
     *
24
     * @param PaymentDescriptionProviderInterface $descriptionProvider
25
     */
26
    public function __construct(PaymentDescriptionProviderInterface $descriptionProvider)
27
    {
28
        $this->descriptionProvider = $descriptionProvider;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     *
34
     * @param Convert $request
35
     */
36
    public function execute($request)
37
    {
38
        RequestNotSupportedException::assertSupports($this, $request);
39
40
        /** @var PaymentInterface $payment */
41
        $payment = $request->getSource();
42
        /** @var SubscriptionInterface $subscription */
43
        $subscription = $payment->getSubscription();
44
45
        $details = [];
46
        $details['PAYMENTREQUEST_0_INVNUM'] = $subscription->getId().'-'.$payment->getId();
47
        $details['PAYMENTREQUEST_0_CURRENCYCODE'] = $subscription->getCurrencyCode();
48
        $details['PAYMENTREQUEST_0_AMT'] = $this->formatPrice($subscription->getTotal());
49
        $details['PAYMENTREQUEST_0_ITEMAMT'] = $this->formatPrice($subscription->getTotal());
50
51
        $m = 0;
52
        foreach ($subscription->getItems() as $item) {
53
            $details['L_PAYMENTREQUEST_0_NAME'.$m] = $this->descriptionProvider->getPaymentDescription($payment);
54
            $details['L_PAYMENTREQUEST_0_AMT'.$m] = $this->formatPrice($item->getUnitPrice());
55
            $details['L_PAYMENTREQUEST_0_QTY'.$m] = $item->getQuantity();
56
57
            ++$m;
58
        }
59
60
        $request->setResult($details);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function supports($request)
67
    {
68
        return
69
            $request instanceof Convert &&
70
            $request->getSource() instanceof PaymentInterface &&
71
            'array' === $request->getTo()
72
        ;
73
    }
74
75
    /**
76
     * @param int $price
77
     *
78
     * @return float
79
     */
80
    private function formatPrice(int $price)
81
    {
82
        return round($price / 100, 2);
83
    }
84
}
85