Completed
Push — master ( 24e2fb...716e2b )
by Oleksandr
16s queued 14s
created

findOrderAuthorizeOnRegistrationTransactionLogByIdSalesOrder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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\Heidelpay\Business\Payment\Transaction;
9
10
use Generated\Shared\Transfer\HeidelpayTransactionLogTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfe...yTransactionLogTransfer 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 Orm\Zed\Heidelpay\Persistence\SpyPaymentHeidelpayTransactionLog;
1 ignored issue
show
Bug introduced by
The type Orm\Zed\Heidelpay\Persis...HeidelpayTransactionLog 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 SprykerEco\Shared\Heidelpay\HeidelpayConfig;
13
use SprykerEco\Zed\Heidelpay\Business\Adapter\TransactionParserInterface;
14
use SprykerEco\Zed\Heidelpay\Business\Encrypter\EncrypterInterface;
15
use SprykerEco\Zed\Heidelpay\Business\Order\OrderReaderInterface;
16
use SprykerEco\Zed\Heidelpay\Persistence\HeidelpayQueryContainerInterface;
17
18
class TransactionLogReader implements TransactionLogReaderInterface
19
{
20
    /**
21
     * @var \SprykerEco\Zed\Heidelpay\Persistence\HeidelpayQueryContainerInterface
22
     */
23
    protected $queryContainer;
24
25
    /**
26
     * @var \SprykerEco\Zed\Heidelpay\Business\Adapter\TransactionParserInterface
27
     */
28
    protected $transactionAdapter;
29
30
    /**
31
     * @var \SprykerEco\Zed\Heidelpay\Business\Order\OrderReaderInterface
32
     */
33
    protected $orderReader;
34
35
    /**
36
     * @var \SprykerEco\Zed\Heidelpay\Business\Encrypter\EncrypterInterface
37
     */
38
    protected $encrypter;
39
40
    /**
41
     * @param \SprykerEco\Zed\Heidelpay\Persistence\HeidelpayQueryContainerInterface $queryContainer
42
     * @param \SprykerEco\Zed\Heidelpay\Business\Adapter\TransactionParserInterface $transactionAdapter
43
     * @param \SprykerEco\Zed\Heidelpay\Business\Order\OrderReaderInterface $orderReader
44
     * @param \SprykerEco\Zed\Heidelpay\Business\Encrypter\EncrypterInterface $encrypter
45
     */
46
    public function __construct(
47
        HeidelpayQueryContainerInterface $queryContainer,
48
        TransactionParserInterface $transactionAdapter,
49
        OrderReaderInterface $orderReader,
50
        EncrypterInterface $encrypter
51
    ) {
52
        $this->queryContainer = $queryContainer;
53
        $this->transactionAdapter = $transactionAdapter;
54
        $this->orderReader = $orderReader;
55
        $this->encrypter = $encrypter;
56
    }
57
58
    /**
59
     * @param int $idSalesOrder
60
     *
61
     * @return \Generated\Shared\Transfer\HeidelpayTransactionLogTransfer|null
62
     */
63
    public function findOrderDebitTransactionLog(int $idSalesOrder): ?HeidelpayTransactionLogTransfer
64
    {
65
        $spyTransactionLog = $this->findOrderDebitTransactionEntity($idSalesOrder);
66
67
        if ($spyTransactionLog === null) {
68
            return null;
69
        }
70
71
        return $this->buildTransactionTransfer($spyTransactionLog);
72
    }
73
74
    /**
75
     * @param int $idSalesOrder
76
     *
77
     * @return \Generated\Shared\Transfer\HeidelpayTransactionLogTransfer|null
78
     */
79
    public function findOrderAuthorizeTransactionLogByIdSalesOrder(int $idSalesOrder): ?HeidelpayTransactionLogTransfer
80
    {
81
        $spyTransactionLog = $this->findOrderAuthorizeTransactionEntity($idSalesOrder);
82
83
        if ($spyTransactionLog === null) {
84
            return null;
85
        }
86
87
        return $this->buildTransactionTransfer($spyTransactionLog);
88
    }
89
90
    /**
91
     * @param int $idSalesOrder
92
     *
93
     * @return \Generated\Shared\Transfer\HeidelpayTransactionLogTransfer|null
94
     */
95
    public function findOrderAuthorizeOnRegistrationTransactionLogByIdSalesOrder($idSalesOrder)
96
    {
97
        $spyTransactionLog = $this->findOrderAuthorizeOnRegistrationTransactionEntity($idSalesOrder);
98
99
        if ($spyTransactionLog === null) {
100
            return null;
101
        }
102
103
        return $this->buildTransactionTransfer($spyTransactionLog);
104
    }
105
106
    /**
107
     * @param int $idSalesOrder
108
     *
109
     * @return \Generated\Shared\Transfer\HeidelpayTransactionLogTransfer|null
110
     */
111
    public function findQuoteInitializeTransactionLogByIdSalesOrder($idSalesOrder)
112
    {
113
        $spyTransactionLog = $this->findQuoteInitializeTransactionEntity($idSalesOrder);
114
115
        if ($spyTransactionLog === null) {
116
            return null;
117
        }
118
119
        return $this->buildTransactionTransfer($spyTransactionLog);
120
    }
121
122
    /**
123
     * @param int $idSalesOrder
124
     *
125
     * @return \Generated\Shared\Transfer\HeidelpayTransactionLogTransfer|null
126
     */
127
    public function findQuoteReservationTransactionLogByIdSalesOrder($idSalesOrder)
128
    {
129
        $spyTransactionLog = $this->findQuoteReservationTransactionEntity($idSalesOrder);
130
131
        if ($spyTransactionLog === null) {
132
            return null;
133
        }
134
135
        return $this->buildTransactionTransfer($spyTransactionLog);
136
    }
137
138
    /**
139
     * @param int $idSalesOrder
140
     *
141
     * @return \Generated\Shared\Transfer\HeidelpayTransactionLogTransfer|null
142
     */
143
    public function findQuoteFinalizeTransactionLogByIdSalesOrder($idSalesOrder)
144
    {
145
        $spyTransactionLog = $this->findQuoteFinalizeTransactionEntity($idSalesOrder);
146
147
        if ($spyTransactionLog === null) {
148
            return null;
149
        }
150
151
        return $this->buildTransactionTransfer($spyTransactionLog);
152
    }
153
154
    /**
155
     * @param string $orderReference
156
     *
157
     * @return \Generated\Shared\Transfer\HeidelpayTransactionLogTransfer
158
     */
159
    public function findOrderAuthorizeTransactionLogByOrderReference(string $orderReference): HeidelpayTransactionLogTransfer
160
    {
161
        $idSalesOrder = $this->orderReader->getOrderIdByReference($orderReference);
162
163
        return $this->findOrderAuthorizeTransactionLogByIdSalesOrder($idSalesOrder);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->findOrderA...lesOrder($idSalesOrder) could return the type null which is incompatible with the type-hinted return Generated\Shared\Transfe...yTransactionLogTransfer. Consider adding an additional type-check to rule them out.
Loading history...
164
    }
165
166
    /**
167
     * @param string $orderReference
168
     *
169
     * @return \Generated\Shared\Transfer\HeidelpayTransactionLogTransfer|null
170
     */
171
    public function findOrderAuthorizeOnRegistrationTransactionLogByOrderReference($orderReference)
172
    {
173
        $idSalesOrder = $this->orderReader->getOrderIdByReference($orderReference);
174
175
        return $this->findOrderAuthorizeOnRegistrationTransactionLogByIdSalesOrder($idSalesOrder);
176
    }
177
178
    /**
179
     * @param string $orderReference
180
     *
181
     * @return \Generated\Shared\Transfer\HeidelpayTransactionLogTransfer|null
182
     */
183
    public function findQuoteInitializeTransactionLogByOrderReference($orderReference)
184
    {
185
        $idSalesOrder = $this->orderReader->getOrderIdByReference($orderReference);
186
187
        return $this->findQuoteInitializeTransactionLogByIdSalesOrder($idSalesOrder);
188
    }
189
190
    /**
191
     * @param int $idSalesOrder
192
     *
193
     * @return \Orm\Zed\Heidelpay\Persistence\SpyPaymentHeidelpayTransactionLog|null
194
     */
195
    protected function findOrderAuthorizeTransactionEntity(int $idSalesOrder): ?SpyPaymentHeidelpayTransactionLog
196
    {
197
        $transactionLogEntity = $this
198
            ->queryContainer
199
            ->queryTransactionByIdSalesOrderAndType(
200
                $idSalesOrder,
201
                HeidelpayConfig::TRANSACTION_TYPE_AUTHORIZE
202
            )
203
            ->findOne();
204
205
        return $transactionLogEntity;
206
    }
207
208
    /**
209
     * @param int $idSalesOrder
210
     *
211
     * @return \Orm\Zed\Heidelpay\Persistence\SpyPaymentHeidelpayTransactionLog|null
212
     */
213
    protected function findOrderAuthorizeOnRegistrationTransactionEntity($idSalesOrder)
214
    {
215
        $transactionLogEntity = $this
216
            ->queryContainer
217
            ->queryTransactionByIdSalesOrderAndType(
218
                $idSalesOrder,
219
                HeidelpayConfig::TRANSACTION_TYPE_AUTHORIZE_ON_REGISTRATION
220
            )
221
            ->findOne();
222
223
        return $transactionLogEntity;
224
    }
225
226
    /**
227
     * @param int $idSalesOrder
228
     *
229
     * @return \Orm\Zed\Heidelpay\Persistence\SpyPaymentHeidelpayTransactionLog|null
230
     */
231
    protected function findQuoteInitializeTransactionEntity($idSalesOrder)
232
    {
233
        $transactionLogEntity = $this
234
            ->queryContainer
235
            ->queryTransactionByIdSalesOrderAndType(
236
                $idSalesOrder,
237
                HeidelpayConfig::TRANSACTION_TYPE_INITIALIZE
238
            )
239
            ->findOne();
240
241
        return $transactionLogEntity;
242
    }
243
244
    /**
245
     * @param int $idSalesOrder
246
     *
247
     * @return \Orm\Zed\Heidelpay\Persistence\SpyPaymentHeidelpayTransactionLog|null
248
     */
249
    protected function findQuoteReservationTransactionEntity($idSalesOrder)
250
    {
251
        $transactionLogEntity = $this
252
            ->queryContainer
253
            ->queryTransactionByIdSalesOrderAndType(
254
                $idSalesOrder,
255
                HeidelpayConfig::TRANSACTION_TYPE_RESERVATION
256
            )
257
            ->findOne();
258
259
        return $transactionLogEntity;
260
    }
261
262
    /**
263
     * @param int $idSalesOrder
264
     *
265
     * @return \Orm\Zed\Heidelpay\Persistence\SpyPaymentHeidelpayTransactionLog|null
266
     */
267
    protected function findQuoteFinalizeTransactionEntity($idSalesOrder)
268
    {
269
        $transactionLogEntity = $this
270
            ->queryContainer
271
            ->queryTransactionByIdSalesOrderAndType(
272
                $idSalesOrder,
273
                HeidelpayConfig::TRANSACTION_TYPE_FINALIZE
274
            )
275
            ->findOne();
276
277
        return $transactionLogEntity;
278
    }
279
280
    /**
281
     * @param int $idSalesOrder
282
     *
283
     * @return \Orm\Zed\Heidelpay\Persistence\SpyPaymentHeidelpayTransactionLog|null
284
     */
285
    protected function findOrderDebitTransactionEntity(int $idSalesOrder): ?SpyPaymentHeidelpayTransactionLog
286
    {
287
        $transactionLogEntity = $this
288
            ->queryContainer
289
            ->queryTransactionByIdSalesOrderAndType(
290
                $idSalesOrder,
291
                HeidelpayConfig::TRANSACTION_TYPE_DEBIT
292
            )
293
            ->findOne();
294
295
        return $transactionLogEntity;
296
    }
297
298
    /**
299
     * @param \Orm\Zed\Heidelpay\Persistence\SpyPaymentHeidelpayTransactionLog $transactionLogEntry
300
     *
301
     * @return \Generated\Shared\Transfer\HeidelpayTransactionLogTransfer
302
     */
303
    protected function buildTransactionTransfer(SpyPaymentHeidelpayTransactionLog $transactionLogEntry): HeidelpayTransactionLogTransfer
304
    {
305
        $responsePayload = $this->prepareResponsePayload($transactionLogEntry);
306
307
        $transactionLogTransfer = (new HeidelpayTransactionLogTransfer())
308
            ->fromArray($transactionLogEntry->toArray(), true)
309
            ->setResponsePayload($responsePayload);
310
311
        return $this->hydrateHeidelpayPayloadTransfer($transactionLogTransfer);
312
    }
313
314
    /**
315
     * @param \Generated\Shared\Transfer\HeidelpayTransactionLogTransfer $transactionLogTransfer
316
     *
317
     * @return \Generated\Shared\Transfer\HeidelpayTransactionLogTransfer
318
     */
319
    protected function hydrateHeidelpayPayloadTransfer(HeidelpayTransactionLogTransfer $transactionLogTransfer): HeidelpayTransactionLogTransfer
320
    {
321
        $payloadTransfer = $this->transactionAdapter->getHeidelpayResponseTransfer($transactionLogTransfer);
322
        $transactionLogTransfer->setHeidelpayResponse($payloadTransfer);
323
324
        return $transactionLogTransfer;
325
    }
326
327
    /**
328
     * @param \Orm\Zed\Heidelpay\Persistence\SpyPaymentHeidelpayTransactionLog $transactionLogEntry
329
     *
330
     * @return string
331
     */
332
    protected function prepareResponsePayload(SpyPaymentHeidelpayTransactionLog $transactionLogEntry): string
333
    {
334
        $responsePayload = $transactionLogEntry->getResponsePayload();
335
        if ($responsePayload !== null) {
336
            $responsePayload = $this->encrypter
337
                ->decryptData(base64_decode($responsePayload));
338
        }
339
340
        return $responsePayload;
341
    }
342
}
343