ConvertPaymentAction::supports()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 6
rs 10
1
<?php
2
3
/*
4
 * This file was created by developers working at BitBag
5
 * Do you need more information about us and what we do? Visit our https://bitbag.io website!
6
 * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
7
*/
8
9
declare(strict_types=1);
10
11
namespace BitBag\SyliusMultiSafepayPlugin\Action;
12
13
use BitBag\SyliusMultiSafepayPlugin\Action\Api\ApiAwareTrait;
14
use Payum\Core\Action\ActionInterface;
15
use Payum\Core\ApiAwareInterface;
16
use Payum\Core\Bridge\Spl\ArrayObject;
17
use Payum\Core\Exception\RequestNotSupportedException;
18
use Payum\Core\GatewayAwareInterface;
19
use Payum\Core\GatewayAwareTrait;
20
use Payum\Core\Request\Convert;
21
use Sylius\Bundle\PayumBundle\Provider\PaymentDescriptionProviderInterface;
22
use Sylius\Component\Channel\Context\ChannelContextInterface;
23
use Sylius\Component\Core\Model\AddressInterface;
24
use Sylius\Component\Core\Model\Channel;
25
use Sylius\Component\Core\Model\CustomerInterface;
26
use Sylius\Component\Core\Model\OrderInterface;
27
use Sylius\Component\Core\Model\PaymentInterface;
28
use Sylius\Component\Currency\Model\CurrencyInterface;
29
30
final class ConvertPaymentAction implements ActionInterface, GatewayAwareInterface, ApiAwareInterface
31
{
32
    use GatewayAwareTrait, ApiAwareTrait;
33
34
    /** @var PaymentDescriptionProviderInterface */
35
    private $paymentDescriptionProvider;
36
37
    /** @var ChannelContextInterface */
38
    private $channelContext;
39
40
    public function __construct(PaymentDescriptionProviderInterface $paymentDescriptionProvider, ChannelContextInterface $channelContext)
41
    {
42
        $this->paymentDescriptionProvider = $paymentDescriptionProvider;
43
        $this->channelContext = $channelContext;
44
    }
45
46
    public function execute($request): void
47
    {
48
        RequestNotSupportedException::assertSupports($this, $request);
49
50
        /** @var PaymentInterface $payment */
51
        $payment = $request->getSource();
52
53
        /** @var OrderInterface $order */
54
        $order = $payment->getOrder();
55
56
        /** @var CustomerInterface $customer */
57
        $customer = $order->getCustomer();
58
59
        /** @var AddressInterface $shippingAddress */
60
        $shippingAddress = $order->getShippingAddress();
61
62
        /** @var AddressInterface $billingAddress */
63
        $billingAddress = $order->getBillingAddress();
64
65
        /** @var Channel $currentChannel */
66
        $currentChannel = $this->channelContext->getChannel();
67
68
        /** @var CurrencyInterface $baseCurrency */
69
        $baseCurrency = $currentChannel->getBaseCurrency();
70
        $currency = ($this->multiSafepayApiClient->getAllowMultiCurrency()) ? $order->getCurrencyCode() : $baseCurrency->getCode();
71
72
        $details = ArrayObject::ensureArrayObject($payment->getDetails());
73
        $details['paymentData'] = [
74
            'type' => $this->multiSafepayApiClient->getType(),
75
            'order_id' => sprintf('%d-%d-%s', $order->getId(), $payment->getId(), $billingAddress->getCountryCode()),
76
            'currency' => $currency,
77
            'amount' => $payment->getAmount(),
78
            'description' => $this->paymentDescriptionProvider->getPaymentDescription($payment),
79
            'customer' => [
80
                'locale' => $order->getLocaleCode(),
81
                'ip_address' => $order->getCustomerIp(),
82
                'first_name' => $shippingAddress->getFirstName(),
83
                'last_name' => $shippingAddress->getLastName(),
84
                'address1' => $shippingAddress->getStreet(),
85
                'zip_code' => $shippingAddress->getPostcode(),
86
                'city' => $shippingAddress->getCity(),
87
                'country' => $shippingAddress->getCountryCode(),
88
                'phone' => $shippingAddress->getPhoneNumber(),
89
                'email' => $customer->getEmail(),
90
            ],
91
        ];
92
93
        $request->setResult((array) $details);
94
    }
95
96
    public function supports($request): bool
97
    {
98
        return
99
            $request instanceof Convert &&
100
            $request->getSource() instanceof PaymentInterface &&
101
            'array' == $request->getTo()
102
        ;
103
    }
104
}
105