Completed
Push — master ( bcb54c...8151ca )
by Kamil
05:51 queued 19s
created

ConvertPaymentAction::prepareAddressData()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 3
nc 4
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
declare(strict_types=1);
13
14
namespace Sylius\Bundle\PayumBundle\Action\Paypal\ExpressCheckout;
15
16
use Payum\Core\Action\ActionInterface;
17
use Payum\Core\Exception\RequestNotSupportedException;
18
use Payum\Core\Request\Convert;
19
use Sylius\Component\Core\Model\AdjustmentInterface;
20
use Sylius\Component\Core\Model\OrderInterface;
21
use Sylius\Component\Core\Model\PaymentInterface;
22
use Sylius\Component\Core\Payment\InvoiceNumberGeneratorInterface;
23
24
final class ConvertPaymentAction implements ActionInterface
25
{
26
    /** @var InvoiceNumberGeneratorInterface */
27
    private $invoiceNumberGenerator;
28
29
    public function __construct(InvoiceNumberGeneratorInterface $invoiceNumberGenerator)
30
    {
31
        $this->invoiceNumberGenerator = $invoiceNumberGenerator;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     *
37
     * @param Convert $request
38
     */
39
    public function execute($request): void
40
    {
41
        RequestNotSupportedException::assertSupports($this, $request);
42
43
        /** @var PaymentInterface $payment */
44
        $payment = $request->getSource();
45
        /** @var OrderInterface $order */
46
        $order = $payment->getOrder();
47
48
        $details = [];
49
        $details['PAYMENTREQUEST_0_INVNUM'] = $this->invoiceNumberGenerator->generate($order, $payment);
50
        $details['PAYMENTREQUEST_0_CURRENCYCODE'] = $order->getCurrencyCode();
51
        $details['PAYMENTREQUEST_0_AMT'] = $this->formatPrice($order->getTotal());
52
        $details['PAYMENTREQUEST_0_ITEMAMT'] = $this->formatPrice($order->getTotal());
53
54
        $details = $this->prepareAddressData($order, $details);
55
56
        $m = 0;
57
        foreach ($order->getItems() as $item) {
58
            $details['L_PAYMENTREQUEST_0_NAME' . $m] = $item->getVariant()->getProduct()->getName();
59
            $details['L_PAYMENTREQUEST_0_AMT' . $m] = $this->formatPrice($item->getDiscountedUnitPrice());
60
            $details['L_PAYMENTREQUEST_0_QTY' . $m] = $item->getQuantity();
61
62
            ++$m;
63
        }
64
65
        if (0 !== $taxTotal = $order->getAdjustmentsTotalRecursively(AdjustmentInterface::TAX_ADJUSTMENT)) {
66
            $details['L_PAYMENTREQUEST_0_NAME' . $m] = 'Tax Total';
67
            $details['L_PAYMENTREQUEST_0_AMT' . $m] = $this->formatPrice($taxTotal);
68
            $details['L_PAYMENTREQUEST_0_QTY' . $m] = 1;
69
70
            ++$m;
71
        }
72
73
        if (0 !== $promotionTotal = $order->getOrderPromotionTotal()) {
74
            $details['L_PAYMENTREQUEST_0_NAME' . $m] = 'Discount';
75
            $details['L_PAYMENTREQUEST_0_AMT' . $m] = $this->formatPrice($promotionTotal);
76
            $details['L_PAYMENTREQUEST_0_QTY' . $m] = 1;
77
78
            ++$m;
79
        }
80
81
        if (0 !== $shippingTotal = $order->getShippingTotal()) {
82
            $details['L_PAYMENTREQUEST_0_NAME' . $m] = 'Shipping Total';
83
            $details['L_PAYMENTREQUEST_0_AMT' . $m] = $this->formatPrice($shippingTotal);
84
            $details['L_PAYMENTREQUEST_0_QTY' . $m] = 1;
85
        }
86
87
        $request->setResult($details);
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function supports($request): bool
94
    {
95
        return
96
            $request instanceof Convert &&
0 ignored issues
show
Bug introduced by
The class Payum\Core\Request\Convert does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
97
            $request->getSource() instanceof PaymentInterface &&
98
            $request->getTo() === 'array'
99
        ;
100
    }
101
102
    private function formatPrice(int $price): float
103
    {
104
        return round($price / 100, 2);
105
    }
106
107
    private function prepareAddressData(OrderInterface $order, array $details): array
108
    {
109
        $details['EMAIL'] = $order->getCustomer()->getEmail();
110
        $billingAddress = $order->getBillingAddress();
111
        $details['LOCALECODE'] = $billingAddress->getCountryCode();
112
        $details['PAYMENTREQUEST_0_SHIPTONAME'] = $billingAddress->getFullName();
113
        $details['PAYMENTREQUEST_0_SHIPTOSTREET'] = $billingAddress->getStreet();
114
        $details['PAYMENTREQUEST_0_SHIPTOCITY'] = $billingAddress->getCity();
115
        $details['PAYMENTREQUEST_0_SHIPTOZIP'] = $billingAddress->getPostcode();
116
        $details['PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE'] = $billingAddress->getCountryCode();
117
118
        if ($billingAddress->getPhoneNumber() !== null) {
119
            $details['PAYMENTREQUEST_0_SHIPTOPHONENUM'] = $billingAddress->getPhoneNumber();
120
        }
121
122
        $province = $billingAddress->getProvinceCode() ?? $billingAddress->getProvinceName();
123
        if ($province !== null) {
124
            $details['PAYMENTREQUEST_0_SHIPTOSTATE'] = $province;
125
        }
126
127
        return $details;
128
    }
129
}
130