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

createPaymentPayoneQueryByOrderId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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

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