Passed
Pull Request — dev (#4)
by Mykyta
11:52 queued 07:39
created

TransactionLogReader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 4
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\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($idSalesOrder)
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($idSalesOrder)
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 string $orderReference
92
     *
93
     * @return \Generated\Shared\Transfer\HeidelpayTransactionLogTransfer|null
94
     */
95
    public function findOrderAuthorizeTransactionLogByOrderReference($orderReference)
96
    {
97
        $idSalesOrder = $this->orderReader->getOrderIdByReference($orderReference);
98
99
        return $this->findOrderAuthorizeTransactionLogByIdSalesOrder($idSalesOrder);
100
    }
101
102
    /**
103
     * @param int $idSalesOrder
104
     *
105
     * @return \Orm\Zed\Heidelpay\Persistence\SpyPaymentHeidelpayTransactionLog|null
106
     */
107
    protected function findOrderAuthorizeTransactionEntity($idSalesOrder)
108
    {
109
        $transactionLogEntity = $this
110
            ->queryContainer
111
            ->queryTransactionByIdSalesOrderAndType(
112
                $idSalesOrder,
113
                HeidelpayConfig::TRANSACTION_TYPE_AUTHORIZE
114
            )
115
            ->findOne();
116
117
        return $transactionLogEntity;
118
    }
119
120
    /**
121
     * @param int $idSalesOrder
122
     *
123
     * @return \Orm\Zed\Heidelpay\Persistence\SpyPaymentHeidelpayTransactionLog|null
124
     */
125
    protected function findOrderDebitTransactionEntity($idSalesOrder)
126
    {
127
        $transactionLogEntity = $this
128
            ->queryContainer
129
            ->queryTransactionByIdSalesOrderAndType(
130
                $idSalesOrder,
131
                HeidelpayConfig::TRANSACTION_TYPE_DEBIT
132
            )
133
            ->findOne();
134
135
        return $transactionLogEntity;
136
    }
137
138
    /**
139
     * @param \Orm\Zed\Heidelpay\Persistence\SpyPaymentHeidelpayTransactionLog $transactionLogEntry
140
     *
141
     * @return \Generated\Shared\Transfer\HeidelpayTransactionLogTransfer
142
     */
143
    protected function buildTransactionTransfer(SpyPaymentHeidelpayTransactionLog $transactionLogEntry)
144
    {
145
        $responsePayload = $this->prepareResponsePayload($transactionLogEntry);
146
147
        $transactionLogTransfer = (new HeidelpayTransactionLogTransfer())
148
            ->fromArray($transactionLogEntry->toArray(), true)
149
            ->setResponsePayload($responsePayload);
150
151
        return $this->hydrateHeidelpayPayloadTransfer($transactionLogTransfer);
152
    }
153
154
    /**
155
     * @param \Generated\Shared\Transfer\HeidelpayTransactionLogTransfer $transactionLogTransfer
156
     *
157
     * @return \Generated\Shared\Transfer\HeidelpayTransactionLogTransfer
158
     */
159
    protected function hydrateHeidelpayPayloadTransfer($transactionLogTransfer)
160
    {
161
        $payloadTransfer = $this->transactionAdapter->getHeidelpayResponseTransfer($transactionLogTransfer);
162
        $transactionLogTransfer->setHeidelpayResponse($payloadTransfer);
163
164
        return $transactionLogTransfer;
165
    }
166
167
    /**
168
     * @param \Orm\Zed\Heidelpay\Persistence\SpyPaymentHeidelpayTransactionLog $transactionLogEntry
169
     *
170
     * @return string
171
     */
172
    protected function prepareResponsePayload(SpyPaymentHeidelpayTransactionLog $transactionLogEntry)
173
    {
174
        $responsePayload = $transactionLogEntry->getResponsePayload();
175
        if ($responsePayload !== null) {
176
            $responsePayload = $this->encrypter
177
                ->decryptData(base64_decode($responsePayload));
0 ignored issues
show
Bug introduced by
It seems like base64_decode($responsePayload) can also be of type false; however, parameter $data of SprykerEco\Zed\Heidelpay...nterface::decryptData() does only seem to accept string, 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

177
                ->decryptData(/** @scrutinizer ignore-type */ base64_decode($responsePayload));
Loading history...
178
        }
179
180
        return $responsePayload;
181
    }
182
}
183