Completed
Push — feature/eco-574/eco-2266-check... ( b72c03...e5ed68 )
by Ruslan
11s queued 10s
created

Saver::getIdCheckout()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
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\Business\Order;
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\QuoteTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\QuoteTransfer 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 Generated\Shared\Transfer\SaveOrderTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\SaveOrderTransfer 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\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...
14
use Orm\Zed\AfterPay\Persistence\SpyPaymentAfterPayOrderItem;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\AfterPay\Persist...aymentAfterPayOrderItem 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\Persistence\EntityManager\TransactionTrait;
16
use SprykerEco\Shared\AfterPay\AfterPayConfig as SharedAfterPayConfig;
17
use SprykerEco\Zed\AfterPay\AfterPayConfig;
18
19
class Saver implements SaverInterface
20
{
21
    use TransactionTrait;
22
23
    /**
24
     * @var \SprykerEco\Zed\AfterPay\AfterPayConfig
25
     */
26
    protected $config;
27
28
    /**
29
     * @param \SprykerEco\Zed\AfterPay\AfterPayConfig $config
30
     */
31
    public function __construct(AfterPayConfig $config)
32
    {
33
        $this->config = $config;
34
    }
35
36
    /**
37
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
38
     * @param \Generated\Shared\Transfer\SaveOrderTransfer $saveOrderTransfer
39
     *
40
     * @return void
41
     */
42
    public function saveOrderPayment(QuoteTransfer $quoteTransfer, SaveOrderTransfer $saveOrderTransfer): void
43
    {
44
        $this->getTransactionHandler()->handleTransaction(function () use ($quoteTransfer, $saveOrderTransfer) {
45
            $this->executeSavePaymentForOrderAndItemsTransaction($quoteTransfer, $saveOrderTransfer);
46
        });
47
    }
48
49
    /**
50
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
51
     * @param \Generated\Shared\Transfer\SaveOrderTransfer $saveOrderTransfer
52
     *
53
     * @return void
54
     */
55
    protected function executeSavePaymentForOrderAndItemsTransaction(
56
        QuoteTransfer $quoteTransfer,
57
        SaveOrderTransfer $saveOrderTransfer
58
    ): void {
59
60
        $paymentEntity = $this->buildPaymentEntity($quoteTransfer, $saveOrderTransfer);
61
        $paymentEntity->save();
62
63
        $idPayment = $paymentEntity->getIdPaymentAfterPay();
64
65
        foreach ($saveOrderTransfer->getOrderItems() as $orderItem) {
66
            $this->savePaymentForOrderItem($orderItem, $idPayment);
67
        }
68
    }
69
70
    /**
71
     * @param \Generated\Shared\Transfer\ItemTransfer $orderItemTransfer
72
     * @param int $idPayment
73
     *
74
     * @return void
75
     */
76
    protected function savePaymentForOrderItem(ItemTransfer $orderItemTransfer, int $idPayment): void
77
    {
78
        $paymentOrderItemEntity = new SpyPaymentAfterPayOrderItem();
79
        $paymentOrderItemEntity
80
            ->setFkPaymentAfterPay($idPayment)
81
            ->setFkSalesOrderItem($orderItemTransfer->getIdSalesOrderItem());
82
83
        $paymentOrderItemEntity->save();
84
    }
85
86
    /**
87
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
88
     * @param \Generated\Shared\Transfer\SaveOrderTransfer $saveOrderTransfer
89
     *
90
     * @return \Orm\Zed\AfterPay\Persistence\SpyPaymentAfterPay
91
     */
92
    protected function buildPaymentEntity(
93
        QuoteTransfer $quoteTransfer,
94
        SaveOrderTransfer $saveOrderTransfer
95
    ): SpyPaymentAfterPay {
96
        $paymentEntity = new SpyPaymentAfterPay();
97
98
        $paymentTransfer = $quoteTransfer->getPayment();
99
100
        $paymentEntity
101
            ->setFkSalesPayment($paymentTransfer->getIdSalesPayment())
102
            ->setPaymentMethod($paymentTransfer->getPaymentMethod())
103
            ->setFkSalesOrder($saveOrderTransfer->getIdSalesOrder())
104
            ->setIdCheckout($this->getIdCheckout($quoteTransfer))
105
            ->setIdChannel($this->getIdChannel($paymentTransfer->getPaymentMethod()))
106
            ->setInfoscoreCustomerNumber($paymentTransfer->getAfterPayCustomerNumber())
107
            ->setExpenseTotal($quoteTransfer->getTotals()->getExpenseTotal())
108
            ->setGrandTotal($this->getPaymentPriceToPay($quoteTransfer));
109
110
        return $paymentEntity;
111
    }
112
113
    /**
114
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
115
     *
116
     * @return int
117
     */
118
    protected function getPaymentPriceToPay(QuoteTransfer $quoteTransfer): int
119
    {
120
        if ($quoteTransfer->getPayment() && $quoteTransfer->getPayment()->getAmount()) {
121
            return $quoteTransfer->getPayment()->getAmount();
122
        }
123
124
        foreach ($quoteTransfer->getPayments() as $paymentTransfer) {
125
            if ($paymentTransfer->getPaymentMethod() !== SharedAfterPayConfig::PROVIDER_NAME || !$paymentTransfer->getAmount()) {
126
                continue;
127
            }
128
            return $paymentTransfer->getAmount();
129
        }
130
131
        return $quoteTransfer->getTotals()->getGrandTotal();
132
    }
133
134
    /**
135
     * @param string $paymentMethod
136
     *
137
     * @return string
138
     */
139
    protected function getIdChannel(string $paymentMethod): string
140
    {
141
        return $this->config->getPaymentChannelId($paymentMethod);
142
    }
143
144
    /**
145
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
146
     *
147
     * @return string|null
148
     */
149
    protected function getIdCheckout(QuoteTransfer $quoteTransfer): ?string
150
    {
151
        if ($quoteTransfer->getAfterPayAvailablePaymentMethods()) {
152
            return $quoteTransfer->getAfterPayAvailablePaymentMethods()->getCheckoutId();
153
        }
154
155
        return null;
156
    }
157
}
158