RefundPlugin   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 26
dl 0
loc 96
rs 10
c 2
b 0
f 0
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A hydrateAfterPayPayment() 0 6 1
A storeRefund() 0 4 1
A getPaymentEntity() 0 3 1
A run() 0 11 1
A getOrderTransfer() 0 7 1
A getItemTransfers() 0 14 2
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Zed\AfterPay\Communication\Plugin\Oms\Command;
9
10
use Generated\Shared\Transfer\ItemTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\ItemTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Generated\Shared\Transfer\OrderTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\OrderTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Orm\Zed\AfterPay\Persistence\SpyPaymentAfterPay;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\AfterPay\Persistence\SpyPaymentAfterPay was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Orm\Zed\Sales\Persistence\SpySalesOrder;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Sales\Persistence\SpySalesOrder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Spryker\Zed\Kernel\Communication\AbstractPlugin;
15
use Spryker\Zed\Oms\Business\Util\ReadOnlyArrayObject;
16
use Spryker\Zed\Oms\Dependency\Plugin\Command\CommandByOrderInterface;
17
18
/**
19
 * @method \SprykerEco\Zed\AfterPay\Business\AfterPayFacadeInterface getFacade()
20
 * @method \SprykerEco\Zed\AfterPay\Communication\AfterPayCommunicationFactory getFactory()
21
 * @method \SprykerEco\Zed\AfterPay\Persistence\AfterPayQueryContainer getQueryContainer()
22
 * @method \SprykerEco\Zed\AfterPay\AfterPayConfig getConfig()
23
 */
24
class RefundPlugin extends AbstractPlugin implements CommandByOrderInterface
25
{
26
    /**
27
     * {@inheritDoc}
28
     * - Sends `refund` request to AfterPay gateway, to refund payment for a specific order.
29
     * - Command which is executed per order basis.
30
     *
31
     * @api
32
     *
33
     * @param array<\Orm\Zed\Sales\Persistence\SpySalesOrderItem> $orderItems
34
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrder $orderEntity
35
     * @param \Spryker\Zed\Oms\Business\Util\ReadOnlyArrayObject $data
36
     *
37
     * @return array
38
     */
39
    public function run(array $orderItems, SpySalesOrder $orderEntity, ReadOnlyArrayObject $data): array
40
    {
41
        $orderTransfer = $this->getOrderTransfer($orderEntity);
42
        $this->hydrateAfterPayPayment($orderTransfer);
43
44
        $items = $this->getItemTransfers($orderItems);
45
46
        $this->getFacade()->refundPayment($items, $orderTransfer);
47
        $this->storeRefund($orderItems, $orderEntity);
48
49
        return [];
50
    }
51
52
    /**
53
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrder $order
54
     *
55
     * @return \Generated\Shared\Transfer\OrderTransfer
56
     */
57
    protected function getOrderTransfer(SpySalesOrder $order): OrderTransfer
58
    {
59
        $orderTransfer = $this->getFactory()
60
            ->getSalesFacade()
61
            ->getOrderByIdSalesOrder($order->getIdSalesOrder());
62
63
        return $orderTransfer;
64
    }
65
66
    /**
67
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrder $orderEntity
68
     *
69
     * @return \Orm\Zed\AfterPay\Persistence\SpyPaymentAfterPay|null
70
     */
71
    protected function getPaymentEntity(SpySalesOrder $orderEntity): ?SpyPaymentAfterPay
72
    {
73
        return $orderEntity->getSpyPaymentAfterPays()->getFirst();
74
    }
75
76
    /**
77
     * @param array<\Orm\Zed\Sales\Persistence\SpySalesOrderItem> $orderItems
78
     *
79
     * @return array<\Generated\Shared\Transfer\ItemTransfer>
80
     */
81
    protected function getItemTransfers(array $orderItems): array
82
    {
83
        $items = [];
84
85
        foreach ($orderItems as $orderItem) {
86
            $items[] = (new ItemTransfer())
87
                ->fromArray($orderItem->toArray(), true)
88
                ->setUnitGrossPrice($orderItem->getGrossPrice())
89
                ->setUnitNetPrice($orderItem->getNetPrice())
90
                ->setUnitPriceToPayAggregation($orderItem->getPriceToPayAggregation())
91
                ->setUnitTaxAmountFullAggregation($orderItem->getTaxAmountFullAggregation());
92
        }
93
94
        return $items;
95
    }
96
97
    /**
98
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
99
     *
100
     * @return \Generated\Shared\Transfer\OrderTransfer
101
     */
102
    protected function hydrateAfterPayPayment(OrderTransfer $orderTransfer): OrderTransfer
103
    {
104
        $paymentTransfer = $this->getFacade()->getPaymentByIdSalesOrder($orderTransfer->getIdSalesOrder());
105
        $orderTransfer->setAfterPayPayment($paymentTransfer);
106
107
        return $orderTransfer;
108
    }
109
110
    /**
111
     * @param array<\Orm\Zed\Sales\Persistence\SpySalesOrderItem> $orderItems
112
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrder $orderEntity
113
     *
114
     * @return void
115
     */
116
    protected function storeRefund(array $orderItems, SpySalesOrder $orderEntity): void
117
    {
118
        $refundTransfer = $this->getFactory()->getRefundFacade()->calculateRefund($orderItems, $orderEntity);
119
        $this->getFactory()->getRefundFacade()->saveRefund($refundTransfer);
120
    }
121
}
122