ConvertPaymentAction   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 0
loc 91
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A execute() 0 18 1
A supports() 0 8 3
A getPaymentData() 0 10 1
A getCustomerData() 0 21 3
A getShoppingList() 0 17 2
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusPrzelewy24Plugin\Action;
14
15
use Payum\Core\Action\ActionInterface;
16
use Payum\Core\Exception\RequestNotSupportedException;
17
use Payum\Core\GatewayAwareTrait;
18
use Payum\Core\Request\Convert;
19
use Sylius\Bundle\PayumBundle\Provider\PaymentDescriptionProviderInterface;
20
use Sylius\Component\Core\Model\OrderInterface;
21
use Sylius\Component\Core\Model\OrderItemInterface;
22
use Sylius\Component\Core\Model\PaymentInterface;
23
24
final class ConvertPaymentAction implements ActionInterface
25
{
26
    use GatewayAwareTrait;
27
28
    /** @var PaymentDescriptionProviderInterface */
29
    private $paymentDescriptionProvider;
30
31
    public function __construct(PaymentDescriptionProviderInterface $paymentDescriptionProvider)
32
    {
33
        $this->paymentDescriptionProvider = $paymentDescriptionProvider;
34
    }
35
36
    public function execute($request): void
37
    {
38
        RequestNotSupportedException::assertSupports($this, $request);
39
40
        /** @var PaymentInterface $payment */
41
        $payment = $request->getSource();
42
43
        /** @var OrderInterface $order */
44
        $order = $payment->getOrder();
45
46
        $paymentData = $this->getPaymentData($payment);
47
        $customerData = $this->getCustomerData($order);
48
        $shoppingList = $this->getShoppingList($order);
49
50
        $details = array_merge($paymentData, $customerData, $shoppingList);
51
52
        $request->setResult($details);
53
    }
54
55
    public function supports($request): bool
56
    {
57
        return
58
            $request instanceof Convert &&
59
            $request->getSource() instanceof PaymentInterface &&
60
            $request->getTo() === 'array'
61
        ;
62
    }
63
64
    private function getPaymentData(PaymentInterface $payment): array
65
    {
66
        $paymentData = [];
67
68
        $paymentData['p24_amount'] = $payment->getAmount();
69
        $paymentData['p24_currency'] = $payment->getCurrencyCode();
70
        $paymentData['p24_description'] = $this->paymentDescriptionProvider->getPaymentDescription($payment);
71
72
        return $paymentData;
73
    }
74
75
    private function getCustomerData(OrderInterface $order): array
76
    {
77
        $customerData = [];
78
79
        $customerData['p24_language'] = $order->getLocaleCode();
80
81
        if (null !== $customer = $order->getCustomer()) {
82
            $customerData['p24_email'] = $customer->getEmail();
83
        }
84
85
        if (null !== $address = $order->getShippingAddress()) {
86
            $customerData['p24_adress'] = $address->getStreet();
87
            $customerData['p24_zip'] = $address->getPostcode();
88
            $customerData['p24_country'] = $address->getCountryCode();
89
            $customerData['p24_phone'] = $address->getPhoneNumber();
90
            $customerData['p24_city'] = $address->getCity();
91
            $customerData['p24_client'] = $address->getFullName();
92
        }
93
94
        return $customerData;
95
    }
96
97
    private function getShoppingList(OrderInterface $order): array
98
    {
99
        $shoppingList = [];
100
101
        $index = 1;
102
103
        /** @var OrderItemInterface $item */
104
        foreach ($order->getItems() as $item) {
105
            $shoppingList['p24_name_' . $index] = $item->getProduct()->getName();
106
            $shoppingList['p24_quantity_' . $index] = $item->getQuantity();
107
            $shoppingList['p24_price_' . $index] = $item->getUnitPrice();
108
109
            ++$index;
110
        }
111
112
        return $shoppingList;
113
    }
114
}
115