Passed
Pull Request — master (#88)
by Olha
05:04
created

PayoneRepository::getFormattedPaymentData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 15
rs 10
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
    protected const LOG_TYPE_API_LOG = 'SpyPaymentPayoneApiLog';
34
    protected const LOG_TYPE_TRANSACTION_STATUS_LOG = 'SpyPaymentPayoneTransactionStatusLog';
35
36
    /**
37
     * @var string
38
     */
39
    protected const RAW_RESPONSE_COLUMN_NAME = 'rawResponse';
40
41
    /**
42
     * @param \Generated\Shared\Transfer\PayoneOrderItemFilterTransfer $payoneOrderItemFilerTransfer
43
     *
44
     * @return \Generated\Shared\Transfer\PaymentPayoneOrderItemTransfer[]
45
     */
46
    public function findPaymentPayoneOrderItemByFilter(PayoneOrderItemFilterTransfer $payoneOrderItemFilerTransfer): array
47
    {
48
        $paymentPayoneOrderItemQuery = $this->getFactory()->createPaymentPayoneOrderItemQuery();
49
        $paymentPayoneOrderItemQuery = $this->setPayoneOrderItemFilters(
50
            $paymentPayoneOrderItemQuery,
51
            $payoneOrderItemFilerTransfer
52
        );
53
54
        $paymentPayoneOrderItemEntities = $paymentPayoneOrderItemQuery->find();
55
        $paymentPayoneOrderItemTransfers = [];
56
57
        foreach ($paymentPayoneOrderItemEntities as $paymentPayoneOrderItemEntity) {
58
            $paymentPayoneOrderItemTransfers[] = $this->getFactory()
59
                ->createPayonePersistenceMapper()
60
                ->mapEntityToPaymentPayoneOrderItemTransfer($paymentPayoneOrderItemEntity, new PaymentPayoneOrderItemTransfer());
61
        }
62
63
        return $paymentPayoneOrderItemTransfers;
64
    }
65
66
    /**
67
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
68
     *
69
     * @return \Generated\Shared\Transfer\PayonePaymentTransfer
70
     */
71
    public function getPayonePaymentByOrder(OrderTransfer $orderTransfer): PayonePaymentTransfer
72
    {
73
        $paymentPayoneEntity = $this->createPaymentPayoneQueryByOrderId($orderTransfer->getIdSalesOrder())->findOne();
74
75
        return $this
76
            ->getFactory()
77
            ->createPayonePersistenceMapper()
78
            ->mapPaymentPayoneEntityToPayonePaymentTransfer($paymentPayoneEntity, new PayonePaymentTransfer());
79
    }
80
81
    /**
82
     * @param int $idOrder
83
     *
84
     * @return \Generated\Shared\Transfer\PaymentDetailTransfer
85
     */
86
    public function getPaymentDetail(int $idOrder): PaymentDetailTransfer
87
    {
88
        $paymentPayoneEntity = $this->createPaymentPayoneQueryByOrderId($idOrder)->findOne();
89
        $paymentPayoneDetailEntity = $paymentPayoneEntity->getSpyPaymentPayoneDetail();
90
91
        return $this->getFactory()->createPayonePersistenceMapper()->mapPaymentPayoneDetailToPaymentDetailTransfer($paymentPayoneDetailEntity);
92
    }
93
94
    /**
95
     * Gets payment logs (both api and transaction status) for specific orders in chronological order.
96
     *
97
     * @param \ArrayObject|\Generated\Shared\Transfer\OrderTransfer[] $orderTransfers
98
     *
99
     * @return \Generated\Shared\Transfer\PayonePaymentLogCollectionTransfer
100
     */
101
    public function getPaymentLogs(ArrayObject $orderTransfers): PayonePaymentLogCollectionTransfer
102
    {
103
        $paymentPayoneApiLogEntityCollection = $this->createPaymentPayoneApiLogQueryByOrderIds($orderTransfers)->find()->getData();
104
105
        $paymentPayoneTransactionStatusLogEntityCollection = $this->createPaymentPayoneTransactionStatusLogQueryByOrderIds($orderTransfers)->find()->getData();
106
107
        $payonePaymentLogTransferList = [];
108
        /** @var \Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLog $paymentPayoneApiLogEntity */
109
        foreach ($paymentPayoneApiLogEntityCollection as $paymentPayoneApiLogEntity) {
110
            /** @var \DateTime $apiLogCreatedAtDateTime */
111
            $apiLogCreatedAtDateTime = $paymentPayoneApiLogEntity->getCreatedAt();
112
            $key = $apiLogCreatedAtDateTime->format('Y-m-d\TH:i:s\Z') . 'a' . $paymentPayoneApiLogEntity->getIdPaymentPayoneApiLog();
113
            $payonePaymentLogTransfer = new PayonePaymentLogTransfer();
114
            $payonePaymentLogTransfer->fromArray($paymentPayoneApiLogEntity->toArray(), true);
115
            $payonePaymentLogTransfer->setLogType(static::LOG_TYPE_API_LOG);
116
            $payonePaymentLogTransferList[$key] = $payonePaymentLogTransfer;
117
        }
118
        /** @var \Orm\Zed\Payone\Persistence\SpyPaymentPayoneTransactionStatusLog $paymentPayoneTransactionStatusLogEntity */
119
        foreach ($paymentPayoneTransactionStatusLogEntityCollection as $paymentPayoneTransactionStatusLogEntity) {
120
            /** @var \DateTime $transactionStatusLogDateTime */
121
            $transactionStatusLogDateTime = $paymentPayoneTransactionStatusLogEntity->getCreatedAt();
122
            $key = $transactionStatusLogDateTime->format('Y-m-d\TH:i:s\Z') . 't' . $paymentPayoneTransactionStatusLogEntity->getIdPaymentPayoneTransactionStatusLog();
123
            $payonePaymentLogTransfer = new PayonePaymentLogTransfer();
124
            $payonePaymentLogTransfer->fromArray($paymentPayoneTransactionStatusLogEntity->toArray(), true);
125
            $payonePaymentLogTransfer->setLogType(static::LOG_TYPE_TRANSACTION_STATUS_LOG);
126
            $payonePaymentLogTransferList[$key] = $payonePaymentLogTransfer;
127
        }
128
129
        ksort($payonePaymentLogTransferList);
130
131
        $payonePaymentLogCollectionTransfer = new PayonePaymentLogCollectionTransfer();
132
133
        foreach ($payonePaymentLogTransferList as $payonePaymentLogTransfer) {
134
            $payonePaymentLogCollectionTransfer->addPaymentLog($payonePaymentLogTransfer);
135
        }
136
137
        return $payonePaymentLogCollectionTransfer;
138
    }
139
140
    /**
141
     * @param int $idSalesOrder
142
     *
143
     * @return \Generated\Shared\Transfer\PayoneApiLogTransfer|null
144
     */
145
    public function findLastApiLogByOrderId(int $idSalesOrder): ?PayoneApiLogTransfer
146
    {
147
        $paymentPayoneApiLogEntity = $this->getFactory()->createPaymentPayoneApiLogQuery()
148
            ->useSpyPaymentPayoneQuery()
149
                ->filterByFkSalesOrder($idSalesOrder)
150
            ->endUse()
151
            ->orderByCreatedAt(Criteria::DESC)
152
            ->orderByIdPaymentPayoneApiLog(Criteria::DESC)
153
            ->findOne();
154
155
        if (!$paymentPayoneApiLogEntity) {
156
            return null;
157
        }
158
159
        $apiLogTransfer = new PayoneApiLogTransfer();
160
        $apiLogTransfer->fromArray($paymentPayoneApiLogEntity->toArray(), true);
161
162
        return $apiLogTransfer;
163
    }
164
165
    /**
166
     * @param int $idOrder
167
     *
168
     * @return \Orm\Zed\Payone\Persistence\SpyPaymentPayoneQuery
169
     */
170
    public function createPaymentPayoneQueryByOrderId(int $idOrder): SpyPaymentPayoneQuery
171
    {
172
        $query = $this->getFactory()->createPaymentPayoneQuery();
173
        $query->findByFkSalesOrder($idOrder);
174
175
        return $query;
176
    }
177
178
    /**
179
     * @param string $reference
180
     *
181
     * @return array<string, mixed>
182
     */
183
    public function getPreauthorizedPaymentByReference(string $reference): array
184
    {
185
        $paymentPayoneQuery = $this->getFactory()->createPaymentPayoneQuery();
186
        $paymentPayoneQuery->filterByReference($reference)
187
            ->joinSpyPaymentPayoneApiLog()
188
            ->useSpyPaymentPayoneApiLogQuery()
189
                ->filterByRequest(PayoneApiConstants::REQUEST_TYPE_PREAUTHORIZATION)
190
                ->filterByStatus(PayoneApiConstants::RESPONSE_TYPE_APPROVED)
191
            ->endUse()
192
            ->withColumn(SpyPaymentPayoneApiLogTableMap::COL_RAW_RESPONSE, static::RAW_RESPONSE_COLUMN_NAME);
193
        $paymentData = $paymentPayoneQuery->findOne();
194
195
        if ($paymentData === null) {
196
            return [];
197
        }
198
199
        return $this->getFormattedPaymentData($paymentData);
200
    }
201
202
    /**
203
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayoneOrderItemQuery $paymentPayoneOrderItemQuery
204
     * @param \Generated\Shared\Transfer\PayoneOrderItemFilterTransfer $payoneOrderItemFilerTransfer
205
     *
206
     * @return \Orm\Zed\Payone\Persistence\SpyPaymentPayoneOrderItemQuery
207
     */
208
    protected function setPayoneOrderItemFilters(
209
        SpyPaymentPayoneOrderItemQuery $paymentPayoneOrderItemQuery,
210
        PayoneOrderItemFilterTransfer $payoneOrderItemFilerTransfer
211
    ): SpyPaymentPayoneOrderItemQuery {
212
        if ($payoneOrderItemFilerTransfer->getIdSalesOrder()) {
213
            $paymentPayoneOrderItemQuery
214
                ->useSpyPaymentPayoneQuery()
215
                    ->filterByFkSalesOrder($payoneOrderItemFilerTransfer->getIdSalesOrder())
216
                ->endUse();
217
        }
218
219
        if (count($payoneOrderItemFilerTransfer->getSalesOrderItemIds())) {
220
            $paymentPayoneOrderItemQuery->filterByFkSalesOrderItem_In(
221
                $payoneOrderItemFilerTransfer->getSalesOrderItemIds()
222
            );
223
        }
224
225
        return $paymentPayoneOrderItemQuery;
226
    }
227
228
    /**
229
     * @param \ArrayObject|\Generated\Shared\Transfer\OrderTransfer[] $orderTransfers
230
     *
231
     * @return \Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLogQuery
232
     */
233
    protected function createPaymentPayoneApiLogQueryByOrderIds($orderTransfers): SpyPaymentPayoneApiLogQuery
234
    {
235
        $ids = [];
236
        foreach ($orderTransfers as $orderTransfer) {
237
            $ids[] = $orderTransfer->getIdSalesOrder();
238
        }
239
240
        $query = $this->getFactory()->createPaymentPayoneApiLogQuery()
241
            ->useSpyPaymentPayoneQuery()
242
                ->filterByFkSalesOrder($ids, Criteria::IN)
243
            ->endUse()
244
            ->orderByCreatedAt();
245
246
        return $query;
247
    }
248
249
    /**
250
     * @param \ArrayObject|\Generated\Shared\Transfer\OrderTransfer[] $orderTransfers
251
     *
252
     * @return \Orm\Zed\Payone\Persistence\SpyPaymentPayoneTransactionStatusLogQuery
253
     */
254
    protected function createPaymentPayoneTransactionStatusLogQueryByOrderIds(ArrayObject $orderTransfers): SpyPaymentPayoneTransactionStatusLogQuery
255
    {
256
        $ids = [];
257
        foreach ($orderTransfers as $orderTransfer) {
258
            $ids[] = $orderTransfer->getIdSalesOrder();
259
        }
260
261
        $query = $this->getFactory()->createPaymentPayoneTransactionStatusLogQuery()
262
            ->useSpyPaymentPayoneQuery()
263
                ->filterByFkSalesOrder($ids, Criteria::IN)
264
            ->endUse()
265
            ->orderByCreatedAt();
266
267
        return $query;
268
    }
269
270
    /**
271
     * @param array<string, mixed> $paymentData
272
     *
273
     * @return array<string, mixed>
274
     */
275
    protected function getFormattedPaymentData(array $paymentData): array
276
    {
277
        $paymentResponseData = $this->getFactory()->getServiceUtilEncoding()->decodeJson($paymentData[static::RAW_RESPONSE_COLUMN_NAME], true);
278
279
        if ($paymentResponseData === null) {
280
            return [];
281
        }
282
283
        $formattedPaymentData = array_merge(
284
            $paymentData,
285
            $paymentResponseData,
0 ignored issues
show
Bug introduced by
It seems like $paymentResponseData can also be of type 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

285
            /** @scrutinizer ignore-type */ $paymentResponseData,
Loading history...
286
        );
287
        unset($formattedPaymentData[static::RAW_RESPONSE_COLUMN_NAME]);
288
289
        return $formattedPaymentData;
290
    }
291
}
292