Passed
Push — feature/eco-3623-payone-pay-u-... ( 0a3f73...4fb100 )
by Roman
03:15
created

getPayuCeeSingleInitResponseFromQuoteTransfer()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 8
rs 10
cc 3
nc 2
nop 1
1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\Computop\Business\Payment\Handler\Saver\Init;
9
10
use Generated\Shared\Transfer\ComputopApiResponseHeaderTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...iResponseHeaderTransfer 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\ComputopPaymentComputopOrderItemCollectionTransfer;
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...
12
use Generated\Shared\Transfer\ComputopPaymentComputopTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...PaymentComputopTransfer 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\ComputopPayuCeeSingleInitResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...gleInitResponseTransfer 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\ComputopSalesOrderItemTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...pSalesOrderItemTransfer 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\PaymentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PaymentTransfer 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\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...
17
use Spryker\Zed\Kernel\Persistence\EntityManager\TransactionTrait;
18
use SprykerEco\Shared\Computop\ComputopConfig as SharedComputopConfig;
19
use SprykerEco\Zed\Computop\ComputopConfig;
20
use SprykerEco\Zed\Computop\Persistence\ComputopEntityManagerInterface;
21
use SprykerEco\Zed\Computop\Persistence\ComputopRepositoryInterface;
22
23
class PayuCeeSingleResponseSaver implements InitResponseSaverInterface
24
{
25
    use TransactionTrait;
26
27
    /**
28
     * @var \SprykerEco\Zed\Computop\Persistence\ComputopEntityManagerInterface
29
     */
30
    protected $computopEntityManager;
31
32
    /**
33
     * @var \SprykerEco\Zed\Computop\ComputopConfig
34
     */
35
    protected $computopConfig;
36
37
    /**
38
     * @var \SprykerEco\Zed\Computop\Persistence\ComputopRepositoryInterface
39
     */
40
    protected $computopRepository;
41
42
    /**
43
     * @param \SprykerEco\Zed\Computop\ComputopConfig $computopConfig
44
     * @param \SprykerEco\Zed\Computop\Persistence\ComputopRepositoryInterface $computopRepository
45
     * @param \SprykerEco\Zed\Computop\Persistence\ComputopEntityManagerInterface $computopEntityManager
46
     */
47
    public function __construct(
48
        ComputopConfig $computopConfig,
49
        ComputopRepositoryInterface $computopRepository,
50
        ComputopEntityManagerInterface $computopEntityManager
51
    ) {
52
        $this->computopConfig = $computopConfig;
53
        $this->computopRepository = $computopRepository;
54
        $this->computopEntityManager = $computopEntityManager;
55
    }
56
57
    /**
58
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
59
     *
60
     * @return \Generated\Shared\Transfer\QuoteTransfer
61
     */
62
    public function save(QuoteTransfer $quoteTransfer): QuoteTransfer
63
    {
64
        $computopPayuCeeSingleInitResponseTransfer = $this->getPaymentTransferFromQuoteOrFail($quoteTransfer)
65
            ->getComputopPayuCeeSingleOrFail()
66
            ->getPayuCeeSingleInitResponseOrFail();
67
68
        $computopApiResponseHeaderTransfer = $computopPayuCeeSingleInitResponseTransfer->getHeaderOrFail()
69
            ->requireTransId()
70
            ->requirePayId()
71
            ->requireXid();
72
73
        if (!$computopApiResponseHeaderTransfer->getIsSuccess()) {
74
            return $quoteTransfer;
75
        }
76
77
        $computopPaymentComputopTransfer = $this->computopRepository->findComputopPaymentByComputopTransId(
78
            $computopApiResponseHeaderTransfer->getTransId()
79
        );
80
        if ($computopPaymentComputopTransfer === null) {
81
            return $quoteTransfer;
82
        }
83
84
        $this->getTransactionHandler()->handleTransaction(function () use ($computopPaymentComputopTransfer, $computopPayuCeeSingleInitResponseTransfer) {
85
            $this->executeSavePaymentResponseTransaction($computopPaymentComputopTransfer, $computopPayuCeeSingleInitResponseTransfer);
86
        });
87
88
        return $quoteTransfer;
89
    }
90
91
    /**
92
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopTransfer $computopPaymentComputopTransfer
93
     * @param \Generated\Shared\Transfer\ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer
94
     *
95
     * @return void
96
     */
97
    protected function executeSavePaymentResponseTransaction(
98
        ComputopPaymentComputopTransfer $computopPaymentComputopTransfer,
99
        ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer
100
    ): void {
101
        $this->savePaymentComputopEntity($computopPaymentComputopTransfer, $computopPayuCeeSingleInitResponseTransfer->getHeader());
102
        $this->savePaymentComputopDetailEntity($computopPaymentComputopTransfer, $computopPayuCeeSingleInitResponseTransfer);
103
        $paymentStatus = $this->getOrderItemPaymentStatusFromResponseHeader($computopPayuCeeSingleInitResponseTransfer->getHeader());
104
        if ($paymentStatus) {
105
            $this->savePaymentComputopOrderItemsEntities($computopPaymentComputopTransfer, $paymentStatus);
106
        }
107
    }
108
109
    /**
110
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
111
     *
112
     * @return \Generated\Shared\Transfer\PaymentTransfer
113
     */
114
    protected function getPaymentTransferFromQuoteOrFail(QuoteTransfer $quoteTransfer): PaymentTransfer
115
    {
116
        foreach ($quoteTransfer->getPayments() as $paymentTransfer) {
117
            if ($paymentTransfer !== null) {
118
                return $paymentTransfer;
119
            }
120
        }
121
122
        return $quoteTransfer->getPaymentOrFail();
123
    }
124
125
    /**
126
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopTransfer $computopPaymentComputopTransfer
127
     * @param \Generated\Shared\Transfer\ComputopApiResponseHeaderTransfer $computopApiResponseHeaderTransfer
128
     *
129
     * @return void
130
     */
131
    protected function savePaymentComputopEntity(
132
        ComputopPaymentComputopTransfer $computopPaymentComputopTransfer,
133
        ComputopApiResponseHeaderTransfer $computopApiResponseHeaderTransfer
134
    ): void {
135
        $computopPaymentComputopTransfer
136
            ->setPayId($computopApiResponseHeaderTransfer->getPayId())
137
            ->setXId($computopApiResponseHeaderTransfer->getXId());
138
139
        $this->computopEntityManager->saveComputopPayment($computopPaymentComputopTransfer);
140
    }
141
142
    /**
143
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopTransfer $computopPaymentComputopTransfer
144
     * @param \Generated\Shared\Transfer\ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer
145
     *
146
     * @return void
147
     */
148
    protected function savePaymentComputopDetailEntity(
149
        ComputopPaymentComputopTransfer $computopPaymentComputopTransfer,
150
        ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer
151
    ): void {
152
        $computopPaymentComputopDetailTransfer = $this->computopRepository->findComputopPaymentDetail($computopPaymentComputopTransfer);
153
        $computopPaymentComputopDetailTransfer->fromArray($computopPayuCeeSingleInitResponseTransfer->toArray(), true);
154
        $computopPaymentComputopDetailTransfer->setCustomerTransactionId(
155
            (int)$computopPaymentComputopDetailTransfer->getCustomerTransactionId()
156
        );
157
158
        $this->computopEntityManager->updateComputopPaymentDetail($computopPaymentComputopDetailTransfer);
0 ignored issues
show
Bug introduced by
It seems like $computopPaymentComputopDetailTransfer can also be of type null; however, parameter $computopPaymentComputopDetailTransfer of SprykerEco\Zed\Computop\...ComputopPaymentDetail() does only seem to accept Generated\Shared\Transfe...tComputopDetailTransfer, 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

158
        $this->computopEntityManager->updateComputopPaymentDetail(/** @scrutinizer ignore-type */ $computopPaymentComputopDetailTransfer);
Loading history...
159
    }
160
161
    /**
162
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopTransfer $computopPaymentComputopTransfer
163
     * @param string $paymentStatus
164
     *
165
     * @return void
166
     */
167
    protected function savePaymentComputopOrderItemsEntities(
168
        ComputopPaymentComputopTransfer $computopPaymentComputopTransfer,
169
        string $paymentStatus
170
    ): void {
171
        $salesOrderItemsCollectionTransfer = $this->computopRepository
172
            ->getComputopSalesOrderItemsCollection($computopPaymentComputopTransfer);
173
174
        $computopPaymentComputopOrderItemsCollection = $this->computopRepository
175
            ->getComputopPaymentComputopOrderItemsCollection($computopPaymentComputopTransfer);
176
177
        foreach ($salesOrderItemsCollectionTransfer->getComputopSalesOrderItems() as $computopSalesOrderItem) {
178
            $this->updateComputopSalesOrderItem(
179
                $computopPaymentComputopOrderItemsCollection,
180
                $computopSalesOrderItem,
181
                $paymentStatus
182
            );
183
        }
184
    }
185
186
    /**
187
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopOrderItemCollectionTransfer $computopPaymentComputopOrderItemsCollection
188
     * @param \Generated\Shared\Transfer\ComputopSalesOrderItemTransfer $computopSalesOrderItem
189
     * @param string|null $paymentStatus
190
     *
191
     * @return void
192
     */
193
    protected function updateComputopSalesOrderItem(
194
        ComputopPaymentComputopOrderItemCollectionTransfer $computopPaymentComputopOrderItemsCollection,
195
        ComputopSalesOrderItemTransfer $computopSalesOrderItem,
196
        ?string $paymentStatus
197
    ): void {
198
        foreach ($computopPaymentComputopOrderItemsCollection->getComputopPaymentComputopOrderItems() as $computopPaymentComputopOrderItem) {
199
            if ($computopSalesOrderItem->getIdSalesOrderItem() !== $computopPaymentComputopOrderItem->getFkSalesOrderItem()) {
200
                continue;
201
            }
202
203
            $computopPaymentComputopOrderItem->setStatus($paymentStatus);
204
            $this->computopEntityManager->updateComputopPaymentComputopOrderItem($computopPaymentComputopOrderItem);
205
206
            return;
207
        }
208
    }
209
210
    /**
211
     * @param \Generated\Shared\Transfer\ComputopApiResponseHeaderTransfer $computopApiResponseHeaderTransfer
212
     *
213
     * @return string
214
     */
215
    protected function getOrderItemPaymentStatusFromResponseHeader(
216
        ComputopApiResponseHeaderTransfer $computopApiResponseHeaderTransfer
217
    ): ?string {
218
        if ($computopApiResponseHeaderTransfer->getStatus() === SharedComputopConfig::AUTHORIZE_REQUEST_STATUS) {
219
            return $this->computopConfig->getAuthorizeRequestOmsStatus();
220
        }
221
222
        if ($computopApiResponseHeaderTransfer->getStatus() === SharedComputopConfig::SUCCESS_OK) {
223
            return $this->computopConfig->getOmsStatusAuthorized();
224
        }
225
226
        return null;
227
    }
228
}
229