Passed
Push — feature/eco-574/eco-2266-check... ( 7e1004...8cf5ab )
by Aleksey
04:15
created

RefundPlugin::storeRefund()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 Orm\Zed\Sales\Persistence\SpySalesOrderItem;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Sales\Persistence\SpySalesOrderItem 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...
15
use Spryker\Zed\Kernel\Communication\AbstractPlugin;
16
use Spryker\Zed\Oms\Business\Util\ReadOnlyArrayObject;
17
use Spryker\Zed\Oms\Dependency\Plugin\Command\CommandByOrderInterface;
18
19
/**
20
 * @method \SprykerEco\Zed\AfterPay\Business\AfterPayFacadeInterface getFacade()
21
 * @method \SprykerEco\Zed\AfterPay\Communication\AfterPayCommunicationFactory getFactory()
22
 * @method \SprykerEco\Zed\AfterPay\Persistence\AfterPayQueryContainer getQueryContainer()
23
 * @method \SprykerEco\Zed\AfterPay\AfterPayConfig getConfig()
24
 */
25
class RefundPlugin extends AbstractPlugin implements CommandByOrderInterface
26
{
27
    /**
28
     * Command which is executed per order basis
29
     *
30
     * @api
31
     *
32
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrderItem[] $orderItems
33
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrder $orderEntity
34
     * @param \Spryker\Zed\Oms\Business\Util\ReadOnlyArrayObject $data
35
     *
36
     * @return array
37
     */
38
    public function run(array $orderItems, SpySalesOrder $orderEntity, ReadOnlyArrayObject $data): array
39
    {
40
        $orderTransfer = $this->getOrderTransfer($orderEntity);
41
        $this->hydrateAfterPayPayment($orderTransfer);
42
43
        foreach ($orderItems as $orderItem) {
44
            $itemTransfer = $this->getOrderItemTransfer($orderItem);
45
            $this->getFacade()->refundPayment($itemTransfer, $orderTransfer);
46
        }
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
60
            ->getFactory()
61
            ->getSalesFacade()
62
            ->getOrderByIdSalesOrder(
63
                $order->getIdSalesOrder()
64
            );
65
66
        return $orderTransfer;
67
    }
68
69
    /**
70
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrder $orderEntity
71
     *
72
     * @return \Orm\Zed\AfterPay\Persistence\SpyPaymentAfterPay|null
73
     */
74
    protected function getPaymentEntity(SpySalesOrder $orderEntity): ?SpyPaymentAfterPay
75
    {
76
        return $orderEntity->getSpyPaymentAfterPays()->getFirst();
77
    }
78
79
    /**
80
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrderItem $orderItem
81
     *
82
     * @return \Generated\Shared\Transfer\ItemTransfer
83
     */
84
    protected function getOrderItemTransfer(SpySalesOrderItem $orderItem): ItemTransfer
85
    {
86
        $itemTransfer = new ItemTransfer();
87
        $itemTransfer->fromArray($orderItem->toArray(), true);
88
89
        $itemTransfer->setUnitGrossPrice($orderItem->getGrossPrice());
90
        $itemTransfer->setUnitNetPrice($orderItem->getNetPrice());
91
92
        $itemTransfer->setUnitPriceToPayAggregation($orderItem->getPriceToPayAggregation());
93
        $itemTransfer->setUnitTaxAmountFullAggregation($orderItem->getTaxAmountFullAggregation());
94
95
        return $itemTransfer;
96
    }
97
98
    /**
99
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
100
     *
101
     * @return \Generated\Shared\Transfer\OrderTransfer
102
     */
103
    protected function hydrateAfterPayPayment(OrderTransfer $orderTransfer): OrderTransfer
104
    {
105
        $paymentTransfer = $this->getFacade()->getPaymentByIdSalesOrder($orderTransfer->getIdSalesOrder());
106
        $orderTransfer->setAfterPayPayment($paymentTransfer);
107
108
        return $orderTransfer;
109
    }
110
111
    /**
112
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrderItem[] $orderItems
113
     * @param \Orm\Zed\Sales\Persistence\SpySalesOrder $orderEntity
114
     *
115
     * @return void
116
     */
117
    protected function storeRefund(array $orderItems, SpySalesOrder $orderEntity): void
118
    {
119
        $refundTransfer = $this->getFactory()->getRefundFacade()->calculateRefund($orderItems, $orderEntity);
120
        $this->getFactory()->getRefundFacade()->saveRefund($refundTransfer);
121
    }
122
}
123