OrderMolliePartialShip   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A partialShip() 0 33 5
A __construct() 0 8 1
A getModus() 0 7 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
 * 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\Purifier\PartialShip;
13
14
use BitBag\SyliusMolliePlugin\Client\MollieApiClient;
15
use BitBag\SyliusMolliePlugin\Factory\MollieGatewayFactory;
16
use BitBag\SyliusMolliePlugin\Form\Type\MollieGatewayConfigurationType;
17
use BitBag\SyliusMolliePlugin\Logger\MollieLoggerActionInterface;
18
use BitBag\SyliusMolliePlugin\Resolver\PartialShip\FromSyliusToMollieLinesResolverInterface;
19
use Mollie\Api\Exceptions\ApiException;
20
use Mollie\Api\Resources\Order;
21
use Sylius\Component\Core\Model\OrderInterface;
22
use Sylius\Component\Core\Model\PaymentMethodInterface;
23
24
final class OrderMolliePartialShip implements OrderMolliePartialShipInterface
25
{
26
    /** @var MollieApiClient */
27
    private $apiClient;
28
29
    /** @var MollieLoggerActionInterface */
30
    private $loggerAction;
31
32
    /** @var FromSyliusToMollieLinesResolverInterface */
33
    private $mollieUnitsResolver;
34
35
    public function __construct(
36
        MollieApiClient $apiClient,
37
        MollieLoggerActionInterface $loggerAction,
38
        FromSyliusToMollieLinesResolverInterface $mollieUnitsResolver
39
    ) {
40
        $this->apiClient = $apiClient;
41
        $this->loggerAction = $loggerAction;
42
        $this->mollieUnitsResolver = $mollieUnitsResolver;
43
    }
44
45
    public function partialShip(OrderInterface $order): void
46
    {
47
        $units = $order->getShipments()->last()->getUnits();
48
49
        if ($units->isEmpty()) {
50
            return;
51
        }
52
53
        $payment = $order->getLastPayment();
54
55
        /** @var PaymentMethodInterface $paymentMethod */
56
        $paymentMethod = $payment->getMethod();
57
58
        $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

58
        $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...
59
60
        if (!isset($payment->getDetails()['order_mollie_id']) || MollieGatewayFactory::FACTORY_NAME !== $factoryName) {
61
            return;
62
        }
63
64
        $modusKey = $this->getModus($paymentMethod->getGatewayConfig()->getConfig());
65
66
        try {
67
            /** @var Order $mollieOrder */
68
            $this->apiClient->setApiKey($modusKey);
69
            $mollieOrder = $this->apiClient->orders->get($payment->getDetails()['order_mollie_id']);
70
71
            $lines = $this->mollieUnitsResolver->resolve($units, $mollieOrder);
72
73
            $mollieOrder->createShipment(['lines' => $lines->getArrayFromObject()]);
74
75
            $this->loggerAction->addLog(sprintf('Partial ship with order id %s: ', $mollieOrder->id));
76
        } catch (ApiException $e) {
77
            $this->loggerAction->addNegativeLog(sprintf('Error partial ship with message %s: ', $e->getMessage()));
78
        }
79
    }
80
81
    private function getModus(array $config): string
82
    {
83
        if ($config['environment']) {
84
            return $config[MollieGatewayConfigurationType::API_KEY_LIVE];
85
        }
86
87
        return $config[MollieGatewayConfigurationType::API_KEY_TEST];
88
    }
89
}
90