Completed
Push — master ( 823e66...ef0fdb )
by mark
19s queued 12s
created

CrefoPayWriter::getTaxRate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 10
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\CrefoPay\Business\Writer;
9
10
use Generated\Shared\Transfer\CrefoPayNotificationTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...PayNotificationTransfer 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\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...
12
use Generated\Shared\Transfer\PaymentCrefoPayNotificationTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...PayNotificationTransfer 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 Generated\Shared\Transfer\PaymentCrefoPayOrderItemCollectionTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...rItemCollectionTransfer 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 Generated\Shared\Transfer\PaymentCrefoPayOrderItemToCrefoPayApiLogTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...oCrefoPayApiLogTransfer 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 Generated\Shared\Transfer\PaymentCrefoPayOrderItemToCrefoPayNotificationTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...PayNotificationTransfer 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...
16
use Generated\Shared\Transfer\PaymentCrefoPayOrderItemTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...efoPayOrderItemTransfer 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...
17
use Generated\Shared\Transfer\PaymentCrefoPayTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PaymentCrefoPayTransfer 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...
18
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...
19
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...
20
use Spryker\DecimalObject\Decimal;
21
use Spryker\Zed\Kernel\Persistence\EntityManager\TransactionTrait;
22
use SprykerEco\Zed\CrefoPay\CrefoPayConfig;
23
use SprykerEco\Zed\CrefoPay\Persistence\CrefoPayEntityManagerInterface;
24
25
class CrefoPayWriter implements CrefoPayWriterInterface
26
{
27
    use TransactionTrait;
28
29
    /**
30
     * @var \SprykerEco\Zed\CrefoPay\Persistence\CrefoPayEntityManagerInterface
31
     */
32
    protected $entityManager;
33
34
    /**
35
     * @var \SprykerEco\Zed\CrefoPay\CrefoPayConfig
36
     */
37
    protected $config;
38
39
    /**
40
     * @param \SprykerEco\Zed\CrefoPay\Persistence\CrefoPayEntityManagerInterface $entityManager
41
     * @param \SprykerEco\Zed\CrefoPay\CrefoPayConfig $config
42
     */
43
    public function __construct(
44
        CrefoPayEntityManagerInterface $entityManager,
45
        CrefoPayConfig $config
46
    ) {
47
        $this->entityManager = $entityManager;
48
        $this->config = $config;
49
    }
50
51
    /**
52
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
53
     * @param \Generated\Shared\Transfer\SaveOrderTransfer $saveOrderTransfer
54
     *
55
     * @return void
56
     */
57
    public function createPaymentEntities(
58
        QuoteTransfer $quoteTransfer,
59
        SaveOrderTransfer $saveOrderTransfer
60
    ): void {
61
        $paymentCrefoPayTransfer = $this->createPaymentCrefoPayEntity($quoteTransfer, $saveOrderTransfer);
62
63
        foreach ($saveOrderTransfer->getOrderItems() as $orderItem) {
64
            $this->createPaymentCrefoPayOrderItemEntity($paymentCrefoPayTransfer, $orderItem);
65
        }
66
    }
67
68
    /**
69
     * @param \Generated\Shared\Transfer\PaymentCrefoPayOrderItemCollectionTransfer $paymentCrefoPayOrderItemCollection
70
     * @param \Generated\Shared\Transfer\PaymentCrefoPayTransfer|null $paymentCrefoPayTransfer
71
     * @param int|null $crefoPayApiLogId
72
     *
73
     * @return void
74
     */
75
    public function updatePaymentEntities(
76
        PaymentCrefoPayOrderItemCollectionTransfer $paymentCrefoPayOrderItemCollection,
77
        ?PaymentCrefoPayTransfer $paymentCrefoPayTransfer = null,
78
        ?int $crefoPayApiLogId = null
79
    ): void {
80
        $this->getTransactionHandler()->handleTransaction(
81
            function () use ($paymentCrefoPayOrderItemCollection, $paymentCrefoPayTransfer, $crefoPayApiLogId): void {
82
                if ($paymentCrefoPayTransfer !== null) {
83
                    $this->entityManager->savePaymentCrefoPayEntity($paymentCrefoPayTransfer);
84
                }
85
86
                foreach ($paymentCrefoPayOrderItemCollection->getCrefoPayOrderItems() as $paymentCrefoPayOrderItemTransfer) {
87
                    $paymentCrefoPayOrderItemTransfer = $this->entityManager
88
                        ->savePaymentCrefoPayOrderItemEntity($paymentCrefoPayOrderItemTransfer);
89
                    if ($crefoPayApiLogId !== null) {
90
                        $this->createPaymentCrefoPayOrderItemToCrefoPayApiLogEntity(
91
                            $paymentCrefoPayOrderItemTransfer->getIdPaymentCrefoPayOrderItem(),
92
                            $crefoPayApiLogId,
93
                        );
94
                    }
95
                }
96
            },
97
        );
98
    }
99
100
    /**
101
     * @param \Generated\Shared\Transfer\CrefoPayNotificationTransfer $notificationTransfer
102
     * @param \Generated\Shared\Transfer\PaymentCrefoPayOrderItemCollectionTransfer $paymentCrefoPayOrderItemCollection
103
     *
104
     * @return void
105
     */
106
    public function createNotificationEntities(
107
        CrefoPayNotificationTransfer $notificationTransfer,
108
        PaymentCrefoPayOrderItemCollectionTransfer $paymentCrefoPayOrderItemCollection
109
    ): void {
110
        $this->getTransactionHandler()->handleTransaction(
111
            function () use ($notificationTransfer, $paymentCrefoPayOrderItemCollection): void {
112
                $paymentCrefoPayNotificationTransfer = $this->createPaymentCrefoPayNotificationEntity($notificationTransfer);
113
                foreach ($paymentCrefoPayOrderItemCollection->getCrefoPayOrderItems() as $paymentCrefoPayOrderItemTransfer) {
114
                    $this->entityManager->savePaymentCrefoPayOrderItemEntity($paymentCrefoPayOrderItemTransfer);
115
                    $this->createPaymentCrefoPayOrderItemToCrefoPayNotificationEntity(
116
                        $paymentCrefoPayOrderItemTransfer->getIdPaymentCrefoPayOrderItem(),
117
                        $paymentCrefoPayNotificationTransfer->getIdPaymentCrefoPayNotification(),
118
                    );
119
                }
120
            },
121
        );
122
    }
123
124
    /**
125
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
126
     * @param \Generated\Shared\Transfer\SaveOrderTransfer $saveOrderTransfer
127
     *
128
     * @return \Generated\Shared\Transfer\PaymentCrefoPayTransfer
129
     */
130
    protected function createPaymentCrefoPayEntity(
131
        QuoteTransfer $quoteTransfer,
132
        SaveOrderTransfer $saveOrderTransfer
133
    ): PaymentCrefoPayTransfer {
134
        $paymentCrefoPayTransfer = (new PaymentCrefoPayTransfer())
135
            ->setIdSalesOrder($saveOrderTransfer->getIdSalesOrder())
136
            ->setOrderReference($saveOrderTransfer->getOrderReference())
137
            ->setCrefoPayOrderId($quoteTransfer->getCrefoPayTransaction()->getCrefoPayOrderId())
138
            ->setPaymentMethod($quoteTransfer->getPayment()->getPaymentSelection())
139
            ->setClientIp($quoteTransfer->getCrefoPayTransaction()->getClientIp());
140
141
        return $this->entityManager->savePaymentCrefoPayEntity($paymentCrefoPayTransfer);
142
    }
143
144
    /**
145
     * @param \Generated\Shared\Transfer\PaymentCrefoPayTransfer $paymentCrefoPayTransfer
146
     * @param \Generated\Shared\Transfer\ItemTransfer $orderItem
147
     *
148
     * @return \Generated\Shared\Transfer\PaymentCrefoPayOrderItemTransfer
149
     */
150
    protected function createPaymentCrefoPayOrderItemEntity(
151
        PaymentCrefoPayTransfer $paymentCrefoPayTransfer,
152
        ItemTransfer $orderItem
153
    ): PaymentCrefoPayOrderItemTransfer {
154
        $paymentCrefoPayOrderItemTransfer = (new PaymentCrefoPayOrderItemTransfer())
155
            ->setIdSalesOrderItem($orderItem->getIdSalesOrderItem())
156
            ->setIdPaymentCrefoPay($paymentCrefoPayTransfer->getIdPaymentCrefoPay())
157
            ->setAmount($orderItem->getSumPriceToPayAggregation())
158
            ->setVatAmount($orderItem->getSumTaxAmountFullAggregation())
159
            ->setVatRate($this->getTaxRate($orderItem))
160
            ->setRefundableAmount($orderItem->getRefundableAmount())
161
            ->setQuantity($orderItem->getQuantity())
162
            ->setStatus($this->config->getOmsStatusNew());
163
164
        return $this->entityManager->savePaymentCrefoPayOrderItemEntity($paymentCrefoPayOrderItemTransfer);
165
    }
166
167
    /**
168
     * @param \Generated\Shared\Transfer\CrefoPayNotificationTransfer $notificationTransfer
169
     *
170
     * @return \Generated\Shared\Transfer\PaymentCrefoPayNotificationTransfer
171
     */
172
    protected function createPaymentCrefoPayNotificationEntity(CrefoPayNotificationTransfer $notificationTransfer): PaymentCrefoPayNotificationTransfer
173
    {
174
        $paymentCrefoPayNotificationTransfer = (new PaymentCrefoPayNotificationTransfer())
175
            ->setCrefoPayOrderId($notificationTransfer->getOrderID())
176
            ->setCaptureId($notificationTransfer->getCaptureID())
177
            ->setMerchantReference($notificationTransfer->getMerchantReference())
178
            ->setPaymentReference($notificationTransfer->getPaymentReference())
179
            ->setUserId($notificationTransfer->getUserID())
180
            ->setAmount($notificationTransfer->getAmount())
181
            ->setCurrency($notificationTransfer->getCurrency())
182
            ->setTransactionStatus($notificationTransfer->getTransactionStatus())
183
            ->setOrderStatus($notificationTransfer->getOrderStatus())
184
            ->setTimestamp($notificationTransfer->getTimestamp())
185
            ->setVersion($notificationTransfer->getVersion());
186
187
        return $this->entityManager->savePaymentCrefoPayNotificationEntity($paymentCrefoPayNotificationTransfer);
188
    }
189
190
    /**
191
     * @param int $idPaymentCrefoPayOrderItem
192
     * @param int $idPaymentCrefoPayApiLog
193
     *
194
     * @return \Generated\Shared\Transfer\PaymentCrefoPayOrderItemToCrefoPayApiLogTransfer
195
     */
196
    protected function createPaymentCrefoPayOrderItemToCrefoPayApiLogEntity(
197
        int $idPaymentCrefoPayOrderItem,
198
        int $idPaymentCrefoPayApiLog
199
    ): PaymentCrefoPayOrderItemToCrefoPayApiLogTransfer {
200
        $paymentCrefoPayOrderItemToCrefoPayApiLogTransfer = (new PaymentCrefoPayOrderItemToCrefoPayApiLogTransfer())
201
            ->setIdPaymentCrefoPayOrderItem($idPaymentCrefoPayOrderItem)
202
            ->setIdPaymentCrefoPayApiLog($idPaymentCrefoPayApiLog);
203
204
        return $this->entityManager
205
            ->savePaymentCrefoPayOrderItemToCrefoPayApiLogEntity($paymentCrefoPayOrderItemToCrefoPayApiLogTransfer);
206
    }
207
208
    /**
209
     * @param int $idPaymentCrefoPayOrderItem
210
     * @param int $idPaymentCrefoPayNotification
211
     *
212
     * @return \Generated\Shared\Transfer\PaymentCrefoPayOrderItemToCrefoPayNotificationTransfer
213
     */
214
    protected function createPaymentCrefoPayOrderItemToCrefoPayNotificationEntity(
215
        int $idPaymentCrefoPayOrderItem,
216
        int $idPaymentCrefoPayNotification
217
    ): PaymentCrefoPayOrderItemToCrefoPayNotificationTransfer {
218
        $paymentCrefoPayOrderItemToCrefoPayNotificationTransfer = (new PaymentCrefoPayOrderItemToCrefoPayNotificationTransfer())
219
            ->setIdPaymentCrefoPayOrderItem($idPaymentCrefoPayOrderItem)
220
            ->setIdPaymentCrefoPayNotification($idPaymentCrefoPayNotification);
221
222
        return $this->entityManager
223
            ->savePaymentCrefoPayOrderItemToCrefoPayNotificationEntity($paymentCrefoPayOrderItemToCrefoPayNotificationTransfer);
224
    }
225
226
    /**
227
     * @param \Generated\Shared\Transfer\ItemTransfer $orderItem
228
     *
229
     * @return float|null
230
     */
231
    protected function getTaxRate(ItemTransfer $orderItem): ?float
232
    {
233
        $taxRate = $orderItem->getTaxRate();
234
        if (!is_a($taxRate, Decimal::class)) {
235
            return $taxRate;
236
        }
237
238
        return $taxRate->toFloat();
239
    }
240
}
241