ShipmentShipEventListener::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
 * You can find more information about us on https://bitbag.io and write us
7
 * an email on [email protected].
8
 */
9
10
declare(strict_types=1);
11
12
namespace BitBag\SyliusMolliePlugin\EventListener;
13
14
use BitBag\SyliusMolliePlugin\Client\MollieApiClient;
15
use BitBag\SyliusMolliePlugin\Factory\MollieGatewayFactory;
16
use BitBag\SyliusMolliePlugin\Form\Type\MollieGatewayConfigurationType;
17
use Mollie\Api\Exceptions\ApiException;
18
use Mollie\Api\Resources\Order;
19
use Sylius\Component\Core\Model\OrderInterface;
20
use Sylius\Component\Core\Model\PaymentMethodInterface;
21
use Sylius\Component\Core\Model\ShipmentInterface;
22
use Symfony\Component\EventDispatcher\GenericEvent;
23
use Symfony\Component\HttpFoundation\Session\Session;
24
use Webmozart\Assert\Assert;
25
26
final class ShipmentShipEventListener
27
{
28
    /** @var Session */
29
    private $session;
30
31
    /** @var MollieApiClient */
32
    private $apiClient;
33
34
    public function __construct(MollieApiClient $apiClient)
35
    {
36
        $this->apiClient = $apiClient;
37
    }
38
39
    public function shipAll(GenericEvent $event): void
40
    {
41
        /** @var ShipmentInterface $shipment */
42
        $shipment = $event->getSubject();
43
        Assert::isInstanceOf($shipment, ShipmentInterface::class);
44
45
        /** @var OrderInterface $order */
46
        $order = $shipment->getOrder();
47
        $payment = $order->getLastPayment();
48
49
        if (null === $payment) {
50
            return;
51
        }
52
53
        /** @var PaymentMethodInterface $paymentMethod */
54
        $paymentMethod = $payment->getMethod();
55
56
        $factoryName = $paymentMethod->getGatewayConfig()->getFactoryName() ?? null;
0 ignored issues
show
Deprecated Code introduced by
The function Payum\Core\Model\Gateway...rface::getFactoryName() has been deprecated: since 1.3.3 will be removed in 2.0. set factory option inside the config ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

56
        $factoryName = /** @scrutinizer ignore-deprecated */ $paymentMethod->getGatewayConfig()->getFactoryName() ?? null;

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
57
58
        if (!isset($payment->getDetails()['order_mollie_id']) || MollieGatewayFactory::FACTORY_NAME !== $factoryName) {
59
            return;
60
        }
61
62
        $modusKey = $this->getModus($paymentMethod->getGatewayConfig()->getConfig());
63
64
        try {
65
            /** @var Order $order */
66
            $this->apiClient->setApiKey($modusKey);
67
            $order = $this->apiClient->orders->get($payment->getDetails()['order_mollie_id']);
68
            $order->shipAll();
69
        } catch (ApiException $e) {
70
            $this->session->getFlashBag()->add('error', $e->getMessage());
71
        }
72
    }
73
74
    private function getModus(array $config): string
75
    {
76
        if ($config['environment']) {
77
            return $config[MollieGatewayConfigurationType::API_KEY_LIVE];
78
        }
79
80
        return $config[MollieGatewayConfigurationType::API_KEY_TEST];
81
    }
82
}
83