Completed
Push — master ( b7e254...bcf326 )
by
unknown
09:07
created

OrderRefundCommandCreator::fromOrder()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 34
rs 9.3222
cc 5
nc 6
nop 1
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\SyliusMolliePlugin\Creator;
14
15
use BitBag\SyliusMolliePlugin\DTO\PartialRefundItems;
16
use BitBag\SyliusMolliePlugin\Exceptions\OfflineRefundPaymentMethodNotFound;
17
use BitBag\SyliusMolliePlugin\Helper\ConvertOrderInterface;
18
use BitBag\SyliusMolliePlugin\Refund\Units\UnitsItemOrderRefundInterface;
19
use BitBag\SyliusMolliePlugin\Refund\Units\UnitsShipmentOrderRefundInterface;
20
use Mollie\Api\Resources\Order;
21
use Sylius\Component\Core\Model\OrderInterface;
22
use Sylius\Component\Resource\Repository\RepositoryInterface;
23
use Sylius\RefundPlugin\Command\RefundUnits;
24
use Sylius\RefundPlugin\Provider\RefundPaymentMethodsProviderInterface;
25
use Webmozart\Assert\Assert;
26
27
final class OrderRefundCommandCreator implements OrderRefundCommandCreatorInterface
28
{
29
    /** @var RepositoryInterface */
30
    private $orderRepository;
31
32
    /** @var UnitsItemOrderRefundInterface */
33
    private $unitsItemOrderRefund;
34
35
    /** @var UnitsShipmentOrderRefundInterface */
36
    private $shipmentOrderRefund;
37
38
    /** @var RefundPaymentMethodsProviderInterface */
39
    private $refundPaymentMethodProvider;
40
41
    public function __construct
42
    (
43
        RepositoryInterface $orderRepository,
44
        UnitsItemOrderRefundInterface $unitsItemOrderRefund,
45
        UnitsShipmentOrderRefundInterface $shipmentOrderRefund,
46
        RefundPaymentMethodsProviderInterface $refundPaymentMethodProvider
47
    ) {
48
        $this->orderRepository = $orderRepository;
49
        $this->unitsItemOrderRefund = $unitsItemOrderRefund;
50
        $this->shipmentOrderRefund = $shipmentOrderRefund;
51
        $this->refundPaymentMethodProvider = $refundPaymentMethodProvider;
52
    }
53
54
    public function fromOrder(Order $order): RefundUnits
55
    {
56
        $orderId = $order->metadata->order_id;
57
        /** @var OrderInterface $syliusOrder */
58
        $syliusOrder = $this->orderRepository->findOneBy(['id' => $orderId]);
59
        Assert::notNull($order, sprintf('Cannot find order id with id %s', $orderId));
60
61
        $partialRefundItems = new PartialRefundItems();
62
63
        foreach ($order->lines as $line) {
64
            if ($line->status === 'paid' && $line->type === ConvertOrderInterface::PHYSICAL_TYPE) {
65
                $getRefundedQuantity = $this->unitsItemOrderRefund->getActualRefundedQuantity($syliusOrder, $line->metadata->item_id);
66
                $partialRefundItems->addPartialRefundItemByQuantity(
67
                    $line->metadata->item_id,
68
                    $line->type,
69
                    $line->quantityRefunded - $getRefundedQuantity
70
                );
71
            }
72
        }
73
74
        $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

74
        $refundMethods = $this->refundPaymentMethodProvider->findForChannel(/** @scrutinizer ignore-type */ $syliusOrder->getChannel());
Loading history...
75
76
        if (empty($refundMethods)) {
77
            throw new OfflineRefundPaymentMethodNotFound(
78
                sprintf('Not found offline payment method on this channel with code :%s', $syliusOrder->getChannel()->getCode())
79
            );
80
        }
81
82
        $refundMethod = current($refundMethods);
83
84
        $unitsToRefund = $this->unitsItemOrderRefund->refund($syliusOrder, $partialRefundItems);
85
        $shipmentToRefund = $this->shipmentOrderRefund->refund($order, $syliusOrder);
86
87
        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

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