Passed
Pull Request — master (#88)
by Olha
14:57
created

getPreauthorizedPaymentByReference()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
c 0
b 0
f 0
dl 0
loc 23
rs 9.7333
cc 2
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\Payone\Persistence;
9
10
use ArrayObject;
11
use Generated\Shared\Transfer\OrderTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\OrderTransfer 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\PaymentDetailTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PaymentDetailTransfer 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\PaymentPayoneOrderItemTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...PayoneOrderItemTransfer 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\PayoneApiLogTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PayoneApiLogTransfer 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\PayoneOrderItemFilterTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...OrderItemFilterTransfer 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\PayonePaymentLogCollectionTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ntLogCollectionTransfer 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\PayonePaymentLogTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PayonePaymentLogTransfer 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\PayonePaymentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PayonePaymentTransfer 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 Orm\Zed\Payone\Persistence\Map\SpyPaymentPayoneApiLogTableMap;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Payone\Persisten...entPayoneApiLogTableMap 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 Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLogQuery;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Payone\Persisten...aymentPayoneApiLogQuery 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...
21
use Orm\Zed\Payone\Persistence\SpyPaymentPayoneOrderItemQuery;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Payone\Persisten...entPayoneOrderItemQuery 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...
22
use Orm\Zed\Payone\Persistence\SpyPaymentPayoneQuery;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Payone\Persistence\SpyPaymentPayoneQuery 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...
23
use Orm\Zed\Payone\Persistence\SpyPaymentPayoneTransactionStatusLogQuery;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Payone\Persisten...ansactionStatusLogQuery 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...
24
use Propel\Runtime\ActiveQuery\Criteria;
25
use Spryker\Zed\Kernel\Persistence\AbstractRepository;
26
use SprykerEco\Shared\Payone\PayoneApiConstants;
27
28
/**
29
 * @method \SprykerEco\Zed\Payone\Persistence\PayonePersistenceFactory getFactory()
30
 */
31
class PayoneRepository extends AbstractRepository implements PayoneRepositoryInterface
32
{
33
    /**
34
     * @var string
35
     */
36
    protected const LOG_TYPE_API_LOG = 'SpyPaymentPayoneApiLog';
37
38
    /**
39
     * @var string
40
     */
41
    protected const LOG_TYPE_TRANSACTION_STATUS_LOG = 'SpyPaymentPayoneTransactionStatusLog';
42
43
    /**
44
     * @var string
45
     */
46
    protected const RAW_RESPONSE_COLUMN_NAME = 'rawResponse';
47
48
    /**
49
     * @param \Generated\Shared\Transfer\PayoneOrderItemFilterTransfer $payoneOrderItemFilerTransfer
50
     *
51
     * @return array<\Generated\Shared\Transfer\PaymentPayoneOrderItemTransfer>
52
     */
53
    public function findPaymentPayoneOrderItemByFilter(PayoneOrderItemFilterTransfer $payoneOrderItemFilerTransfer): array
54
    {
55
        $paymentPayoneOrderItemQuery = $this->getFactory()->createPaymentPayoneOrderItemQuery();
56
        $paymentPayoneOrderItemQuery = $this->setPayoneOrderItemFilters(
57
            $paymentPayoneOrderItemQuery,
58
            $payoneOrderItemFilerTransfer,
59
        );
60
61
        $paymentPayoneOrderItemEntities = $paymentPayoneOrderItemQuery->find();
62
        $paymentPayoneOrderItemTransfers = [];
63
64
        foreach ($paymentPayoneOrderItemEntities as $paymentPayoneOrderItemEntity) {
65
            $paymentPayoneOrderItemTransfers[] = $this->getFactory()
66
                ->createPayonePersistenceMapper()
67
                ->mapEntityToPaymentPayoneOrderItemTransfer($paymentPayoneOrderItemEntity, new PaymentPayoneOrderItemTransfer());
68
        }
69
70
        return $paymentPayoneOrderItemTransfers;
71
    }
72
73
    /**
74
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
75
     *
76
     * @return \Generated\Shared\Transfer\PayonePaymentTransfer
77
     */
78
    public function getPayonePaymentByOrder(OrderTransfer $orderTransfer): PayonePaymentTransfer
79
    {
80
        $paymentPayoneEntity = $this->createPaymentPayoneQueryByOrderId($orderTransfer->getIdSalesOrder())->findOne();
81
82
        return $this
83
            ->getFactory()
84
            ->createPayonePersistenceMapper()
85
            ->mapPaymentPayoneEntityToPayonePaymentTransfer($paymentPayoneEntity, new PayonePaymentTransfer());
86
    }
87
88
    /**
89
     * @param int $idOrder
90
     *
91
     * @return \Generated\Shared\Transfer\PaymentDetailTransfer
92
     */
93
    public function getPaymentDetail(int $idOrder): PaymentDetailTransfer
94
    {
95
        $paymentPayoneEntity = $this->createPaymentPayoneQueryByOrderId($idOrder)->findOne();
96
        $paymentPayoneDetailEntity = $paymentPayoneEntity->getSpyPaymentPayoneDetail();
97
98
        return $this->getFactory()->createPayonePersistenceMapper()->mapPaymentPayoneDetailToPaymentDetailTransfer($paymentPayoneDetailEntity);
99
    }
100
101
    /**
102
     * Gets payment logs (both api and transaction status) for specific orders in chronological order.
103
     *
104
     * @param \ArrayObject<\Generated\Shared\Transfer\OrderTransfer> $orderTransfers
105
     *
106
     * @return \Generated\Shared\Transfer\PayonePaymentLogCollectionTransfer
107
     */
108
    public function getPaymentLogs(ArrayObject $orderTransfers): PayonePaymentLogCollectionTransfer
109
    {
110
        $paymentPayoneApiLogEntityCollection = $this->createPaymentPayoneApiLogQueryByOrderIds($orderTransfers)->find()->getData();
111
112
        $paymentPayoneTransactionStatusLogEntityCollection = $this->createPaymentPayoneTransactionStatusLogQueryByOrderIds($orderTransfers)->find()->getData();
113
114
        $payonePaymentLogTransferList = [];
115
        /** @var \Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLog $paymentPayoneApiLogEntity */
116
        foreach ($paymentPayoneApiLogEntityCollection as $paymentPayoneApiLogEntity) {
117
            /** @var \DateTime $apiLogCreatedAtDateTime */
118
            $apiLogCreatedAtDateTime = $paymentPayoneApiLogEntity->getCreatedAt();
119
            $key = $apiLogCreatedAtDateTime->format('Y-m-d\TH:i:s\Z') . 'a' . $paymentPayoneApiLogEntity->getIdPaymentPayoneApiLog();
120
            $payonePaymentLogTransfer = new PayonePaymentLogTransfer();
121
            $payonePaymentLogTransfer->fromArray($paymentPayoneApiLogEntity->toArray(), true);
122
            $payonePaymentLogTransfer->setLogType(static::LOG_TYPE_API_LOG);
123
            $payonePaymentLogTransferList[$key] = $payonePaymentLogTransfer;
124
        }
125
        /** @var \Orm\Zed\Payone\Persistence\SpyPaymentPayoneTransactionStatusLog $paymentPayoneTransactionStatusLogEntity */
126
        foreach ($paymentPayoneTransactionStatusLogEntityCollection as $paymentPayoneTransactionStatusLogEntity) {
127
            /** @var \DateTime $transactionStatusLogDateTime */
128
            $transactionStatusLogDateTime = $paymentPayoneTransactionStatusLogEntity->getCreatedAt();
129
            $key = $transactionStatusLogDateTime->format('Y-m-d\TH:i:s\Z') . 't' . $paymentPayoneTransactionStatusLogEntity->getIdPaymentPayoneTransactionStatusLog();
130
            $payonePaymentLogTransfer = new PayonePaymentLogTransfer();
131
            $payonePaymentLogTransfer->fromArray($paymentPayoneTransactionStatusLogEntity->toArray(), true);
132
            $payonePaymentLogTransfer->setLogType(static::LOG_TYPE_TRANSACTION_STATUS_LOG);
133
            $payonePaymentLogTransferList[$key] = $payonePaymentLogTransfer;
134
        }
135
136
        ksort($payonePaymentLogTransferList);
137
138
        $payonePaymentLogCollectionTransfer = new PayonePaymentLogCollectionTransfer();
139
140
        foreach ($payonePaymentLogTransferList as $payonePaymentLogTransfer) {
141
            $payonePaymentLogCollectionTransfer->addPaymentLog($payonePaymentLogTransfer);
142
        }
143
144
        return $payonePaymentLogCollectionTransfer;
145
    }
146
147
    /**
148
     * @param int $idSalesOrder
149
     *
150
     * @return \Generated\Shared\Transfer\PayoneApiLogTransfer|null
151
     */
152
    public function findLastApiLogByOrderId(int $idSalesOrder): ?PayoneApiLogTransfer
153
    {
154
        $paymentPayoneApiLogEntity = $this->getFactory()->createPaymentPayoneApiLogQuery()
155
            ->useSpyPaymentPayoneQuery()
156
                ->filterByFkSalesOrder($idSalesOrder)
157
            ->endUse()
158
            ->orderByCreatedAt(Criteria::DESC)
159
            ->orderByIdPaymentPayoneApiLog(Criteria::DESC)
160
            ->findOne();
161
162
        if (!$paymentPayoneApiLogEntity) {
163
            return null;
164
        }
165
166
        $apiLogTransfer = new PayoneApiLogTransfer();
167
        $apiLogTransfer->fromArray($paymentPayoneApiLogEntity->toArray(), true);
168
169
        return $apiLogTransfer;
170
    }
171
172
    /**
173
     * @param int $idOrder
174
     *
175
     * @return \Orm\Zed\Payone\Persistence\SpyPaymentPayoneQuery
176
     */
177
    public function createPaymentPayoneQueryByOrderId(int $idOrder): SpyPaymentPayoneQuery
178
    {
179
        $query = $this->getFactory()->createPaymentPayoneQuery();
180
        $query->findByFkSalesOrder($idOrder);
181
182
        return $query;
183
    }
184
185
    /**
186
     * @param string $reference
187
     *
188
     * @return array<string, mixed>
189
     */
190
    public function getPreauthorizedPaymentByReference(string $reference): array
191
    {
192
        $paymentPayoneQuery = $this->getFactory()->createPaymentPayoneQuery();
193
        $paymentPayoneQuery->filterByReference($reference)
194
            ->joinSpyPaymentPayoneApiLog()
195
            ->useSpyPaymentPayoneApiLogQuery()
196
                ->filterByRequest(PayoneApiConstants::REQUEST_TYPE_PREAUTHORIZATION)
197
                ->filterByStatus(PayoneApiConstants::RESPONSE_TYPE_APPROVED)
198
            ->endUse()
199
            ->withColumn(SpyPaymentPayoneApiLogTableMap::COL_RAW_RESPONSE, static::RAW_RESPONSE_COLUMN_NAME);
200
        $paymentData = $paymentPayoneQuery->findOne();
201
202
        if ($paymentData === null) {
203
            return [];
204
        }
205
206
        $formattedPaymentData = array_merge(
207
            $paymentData->toArray(),
208
            $this->getFactory()->getServiceUtilEncoding()->decodeJson($paymentData->getVirtualColumn(static::RAW_RESPONSE_COLUMN_NAME), true),
0 ignored issues
show
Bug introduced by
It seems like $this->getFactory()->get...NSE_COLUMN_NAME), true) can also be of type null and object; however, parameter $arrays of array_merge() does only seem to accept array, 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

208
            /** @scrutinizer ignore-type */ $this->getFactory()->getServiceUtilEncoding()->decodeJson($paymentData->getVirtualColumn(static::RAW_RESPONSE_COLUMN_NAME), true),
Loading history...
209
        );
210
        unset($formattedPaymentData[static::RAW_RESPONSE_COLUMN_NAME]);
211
212
        return $formattedPaymentData;
213
    }
214
215
    /**
216
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayoneOrderItemQuery $paymentPayoneOrderItemQuery
217
     * @param \Generated\Shared\Transfer\PayoneOrderItemFilterTransfer $payoneOrderItemFilerTransfer
218
     *
219
     * @return \Orm\Zed\Payone\Persistence\SpyPaymentPayoneOrderItemQuery
220
     */
221
    protected function setPayoneOrderItemFilters(
222
        SpyPaymentPayoneOrderItemQuery $paymentPayoneOrderItemQuery,
223
        PayoneOrderItemFilterTransfer $payoneOrderItemFilerTransfer
224
    ): SpyPaymentPayoneOrderItemQuery {
225
        if ($payoneOrderItemFilerTransfer->getIdSalesOrder()) {
226
            $paymentPayoneOrderItemQuery
227
                ->useSpyPaymentPayoneQuery()
228
                    ->filterByFkSalesOrder($payoneOrderItemFilerTransfer->getIdSalesOrder())
229
                ->endUse();
230
        }
231
232
        if (count($payoneOrderItemFilerTransfer->getSalesOrderItemIds())) {
233
            $paymentPayoneOrderItemQuery->filterByFkSalesOrderItem_In(
234
                $payoneOrderItemFilerTransfer->getSalesOrderItemIds(),
235
            );
236
        }
237
238
        return $paymentPayoneOrderItemQuery;
239
    }
240
241
    /**
242
     * @param \ArrayObject<\Generated\Shared\Transfer\OrderTransfer> $orderTransfers
243
     *
244
     * @return \Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLogQuery
245
     */
246
    protected function createPaymentPayoneApiLogQueryByOrderIds($orderTransfers): SpyPaymentPayoneApiLogQuery
247
    {
248
        $ids = [];
249
        foreach ($orderTransfers as $orderTransfer) {
250
            $ids[] = $orderTransfer->getIdSalesOrder();
251
        }
252
253
        $query = $this->getFactory()->createPaymentPayoneApiLogQuery()
254
            ->useSpyPaymentPayoneQuery()
255
                ->filterByFkSalesOrder($ids, Criteria::IN)
256
            ->endUse()
257
            ->orderByCreatedAt();
258
259
        return $query;
260
    }
261
262
    /**
263
     * @param \ArrayObject<\Generated\Shared\Transfer\OrderTransfer> $orderTransfers
264
     *
265
     * @return \Orm\Zed\Payone\Persistence\SpyPaymentPayoneTransactionStatusLogQuery
266
     */
267
    protected function createPaymentPayoneTransactionStatusLogQueryByOrderIds(ArrayObject $orderTransfers): SpyPaymentPayoneTransactionStatusLogQuery
268
    {
269
        $ids = [];
270
        foreach ($orderTransfers as $orderTransfer) {
271
            $ids[] = $orderTransfer->getIdSalesOrder();
272
        }
273
274
        $query = $this->getFactory()->createPaymentPayoneTransactionStatusLogQuery()
275
            ->useSpyPaymentPayoneQuery()
276
                ->filterByFkSalesOrder($ids, Criteria::IN)
277
            ->endUse()
278
            ->orderByCreatedAt();
279
280
        return $query;
281
    }
282
}
283