OrderRefundCommandCreator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 60
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A fromOrder() 0 34 5
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\Creator;
13
14
use BitBag\SyliusMolliePlugin\DTO\PartialRefundItems;
15
use BitBag\SyliusMolliePlugin\Exceptions\OfflineRefundPaymentMethodNotFound;
16
use BitBag\SyliusMolliePlugin\Helper\ConvertOrderInterface;
17
use BitBag\SyliusMolliePlugin\Refund\Units\UnitsItemOrderRefundInterface;
18
use BitBag\SyliusMolliePlugin\Refund\Units\UnitsShipmentOrderRefundInterface;
19
use Mollie\Api\Resources\Order;
20
use Sylius\Component\Core\Model\OrderInterface;
21
use Sylius\Component\Resource\Repository\RepositoryInterface;
22
use Sylius\RefundPlugin\Command\RefundUnits;
23
use Sylius\RefundPlugin\Provider\RefundPaymentMethodsProviderInterface;
24
use Webmozart\Assert\Assert;
25
26
final class OrderRefundCommandCreator implements OrderRefundCommandCreatorInterface
27
{
28
    /** @var RepositoryInterface */
29
    private $orderRepository;
30
31
    /** @var UnitsItemOrderRefundInterface */
32
    private $unitsItemOrderRefund;
33
34
    /** @var UnitsShipmentOrderRefundInterface */
35
    private $shipmentOrderRefund;
36
37
    /** @var RefundPaymentMethodsProviderInterface */
38
    private $refundPaymentMethodProvider;
39
40
    public function __construct(
41
        RepositoryInterface $orderRepository,
42
        UnitsItemOrderRefundInterface $unitsItemOrderRefund,
43
        UnitsShipmentOrderRefundInterface $shipmentOrderRefund,
44
        RefundPaymentMethodsProviderInterface $refundPaymentMethodProvider
45
    ) {
46
        $this->orderRepository = $orderRepository;
47
        $this->unitsItemOrderRefund = $unitsItemOrderRefund;
48
        $this->shipmentOrderRefund = $shipmentOrderRefund;
49
        $this->refundPaymentMethodProvider = $refundPaymentMethodProvider;
50
    }
51
52
    public function fromOrder(Order $order): RefundUnits
53
    {
54
        $orderId = $order->metadata->order_id;
55
        /** @var OrderInterface $syliusOrder */
56
        $syliusOrder = $this->orderRepository->findOneBy(['id' => $orderId]);
57
        Assert::notNull($order, sprintf('Cannot find order id with id %s', $orderId));
58
59
        $partialRefundItems = new PartialRefundItems();
60
61
        foreach ($order->lines as $line) {
62
            if ($line->status === 'paid' && $line->type === ConvertOrderInterface::PHYSICAL_TYPE) {
63
                $getRefundedQuantity = $this->unitsItemOrderRefund->getActualRefundedQuantity($syliusOrder, $line->metadata->item_id);
64
                $partialRefundItems->addPartialRefundItemByQuantity(
65
                    $line->metadata->item_id,
66
                    $line->type,
67
                    $line->quantityRefunded - $getRefundedQuantity
68
                );
69
            }
70
        }
71
72
        $refundMethods = $this->refundPaymentMethodProvider->findForChannel($syliusOrder->getChannel());
0 ignored issues
show
Bug introduced by
It seems like $syliusOrder->getChannel() can also be of type null; however, parameter $channel of Sylius\RefundPlugin\Prov...rface::findForChannel() does only seem to accept Sylius\Component\Core\Model\ChannelInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

72
        $refundMethods = $this->refundPaymentMethodProvider->findForChannel(/** @scrutinizer ignore-type */ $syliusOrder->getChannel());
Loading history...
73
74
        if (empty($refundMethods)) {
75
            throw new OfflineRefundPaymentMethodNotFound(
76
                sprintf('Not found offline payment method on this channel with code :%s', $syliusOrder->getChannel()->getCode())
77
            );
78
        }
79
80
        $refundMethod = current($refundMethods);
81
82
        $unitsToRefund = $this->unitsItemOrderRefund->refund($syliusOrder, $partialRefundItems);
83
        $shipmentToRefund = $this->shipmentOrderRefund->refund($order, $syliusOrder);
84
85
        return new RefundUnits($syliusOrder->getNumber(), $unitsToRefund, $shipmentToRefund, $refundMethod->getId(), '');
0 ignored issues
show
Bug introduced by
It seems like $syliusOrder->getNumber() can also be of type null; however, parameter $orderNumber of Sylius\RefundPlugin\Comm...undUnits::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

85
        return new RefundUnits(/** @scrutinizer ignore-type */ $syliusOrder->getNumber(), $unitsToRefund, $shipmentToRefund, $refundMethod->getId(), '');
Loading history...
86
    }
87
}
88