Passed
Pull Request — master (#41)
by Roman
08:34
created

ComputopEntityManager   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Importance

Changes 10
Bugs 2 Features 1
Metric Value
wmc 24
eloc 88
c 10
b 2
f 1
dl 0
loc 201
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A savePaymentComputopNotification() 0 13 1
A savePaymentComputopDetailEntity() 0 11 2
A updatePaymentComputopOrderItemPaymentConfirmation() 0 40 5
A savePaymentComputopOrderItems() 0 5 2
A saveComputopPayuCeeSingleInitResponse() 0 30 2
A getCurrentOrderItemEntityStatus() 0 22 6
A getPaymentStatus() 0 26 6
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\Computop\Persistence;
9
10
use Generated\Shared\Transfer\ComputopNotificationTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...topNotificationTransfer 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 Generated\Shared\Transfer\ComputopPayuCeeSingleInitResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...gleInitResponseTransfer 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 Orm\Zed\Computop\Persistence\SpyPaymentComputopDetail;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Computop\Persist...pyPaymentComputopDetail 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 Orm\Zed\Computop\Persistence\SpyPaymentComputopOrderItem;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Computop\Persist...aymentComputopOrderItem 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 Propel\Runtime\Collection\ObjectCollection;
15
use Spryker\Zed\Kernel\Persistence\AbstractEntityManager;
16
use SprykerEco\Shared\Computop\ComputopConfig as SharedComputopConfig;
17
18
/**
19
 * @method \SprykerEco\Zed\Computop\Persistence\ComputopPersistenceFactory getFactory()
20
 */
21
class ComputopEntityManager extends AbstractEntityManager implements ComputopEntityManagerInterface
22
{
23
    /**
24
     * @param \Generated\Shared\Transfer\ComputopNotificationTransfer $computopNotificationTransfer
25
     *
26
     * @return void
27
     */
28
    public function savePaymentComputopNotification(ComputopNotificationTransfer $computopNotificationTransfer): void
29
    {
30
        $paymentComputopNotificationEntity = $this->getFactory()
31
            ->createPaymentComputopNotificationQuery()
32
            ->filterByPayId($computopNotificationTransfer->getPayId())
33
            ->filterByTransId($computopNotificationTransfer->getTransId())
34
            ->filterByXId($computopNotificationTransfer->getXId())
35
            ->findOneOrCreate();
36
37
        $paymentComputopNotificationEntity->fromArray(
38
            $computopNotificationTransfer->modifiedToArray()
39
        );
40
        $paymentComputopNotificationEntity->save();
41
    }
42
43
    /**
44
     * @param \Generated\Shared\Transfer\ComputopNotificationTransfer $computopNotificationTransfer
45
     *
46
     * @return bool
47
     */
48
    public function updatePaymentComputopOrderItemPaymentConfirmation(
49
        ComputopNotificationTransfer $computopNotificationTransfer
50
    ): bool {
51
        /** @var \Orm\Zed\Computop\Persistence\SpyPaymentComputop|null $paymentComputopEntity */
52
        $paymentComputopEntity = $this->getFactory()
53
            ->createPaymentComputopQuery()
54
            ->filterByTransId($computopNotificationTransfer->getTransId())
55
            ->findOne();
56
57
        if ($paymentComputopEntity === null) {
58
            return false;
59
        }
60
61
        $paymentComputopEntity
62
            ->setPayId($computopNotificationTransfer->getPayId())
63
            ->setXId($computopNotificationTransfer->getXId());
64
65
        if ($paymentComputopEntity->isModified()) {
66
            $paymentComputopEntity->save();
67
        }
68
69
        /** @var \Orm\Zed\Computop\Persistence\SpyPaymentComputopOrderItem[]|\Propel\Runtime\Collection\ObjectCollection $paymentComputopOrderItemEntities */
70
        $paymentComputopOrderItemEntities = $this->getFactory()
71
            ->createPaymentComputopOrderItemQuery()
72
            ->filterByFkPaymentComputop($paymentComputopEntity->getIdPaymentComputop())
73
            ->find();
74
75
        if (!$paymentComputopOrderItemEntities->count()) {
76
            return false;
77
        }
78
79
        foreach ($paymentComputopOrderItemEntities as $paymentComputopOrderItemEntity) {
80
            $orderItemEntityStatus = $this->getCurrentOrderItemEntityStatus($computopNotificationTransfer, $paymentComputopOrderItemEntity);
81
            $paymentComputopOrderItemEntity
82
                ->setIsPaymentConfirmed((bool)$computopNotificationTransfer->getIsSuccess())
83
                ->setStatus($orderItemEntityStatus)
84
                ->save();
85
        }
86
87
        return true;
88
    }
89
90
    /**
91
     * @param \Generated\Shared\Transfer\ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer
92
     *
93
     * @return void
94
     */
95
    public function saveComputopPayuCeeSingleInitResponse(ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer): void
96
    {
97
        $computopApiResponseHeaderTransfer = $computopPayuCeeSingleInitResponseTransfer->requireHeader()
98
            ->getHeader()
99
                ->requireTransId()
100
                ->requirePayId()
101
                ->requireXid();
102
103
        $paymentComputopEntity = $this->getFactory()
104
            ->createPaymentComputopQuery()
105
            ->filterByTransId($computopApiResponseHeaderTransfer->getTransId())
106
            ->findOne();
107
108
        if (!$paymentComputopEntity) {
109
            return;
110
        }
111
112
        $paymentComputopEntity
113
            ->setPayId($computopApiResponseHeaderTransfer->getPayId())
114
            ->setXId($computopApiResponseHeaderTransfer->getXId())
115
            ->save();
116
117
        $this->savePaymentComputopDetailEntity(
118
            $paymentComputopEntity->getSpyPaymentComputopDetail(),
119
            $computopPayuCeeSingleInitResponseTransfer
120
        );
121
122
        $this->savePaymentComputopOrderItems(
123
            $paymentComputopEntity->getSpyPaymentComputopOrderItems(),
124
            $this->getPaymentStatus($computopPayuCeeSingleInitResponseTransfer)
125
        );
126
    }
127
128
    /**
129
     * @param \Orm\Zed\Computop\Persistence\SpyPaymentComputopDetail $paymentComputopDetailEntity
130
     * @param \Generated\Shared\Transfer\ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer
131
     *
132
     * @return void
133
     */
134
    protected function savePaymentComputopDetailEntity(
135
        SpyPaymentComputopDetail $paymentComputopDetailEntity,
136
        ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer
137
    ): void {
138
        $paymentComputopDetailEntity->fromArray($computopPayuCeeSingleInitResponseTransfer->toArray());
139
        $customerTransactionId = $computopPayuCeeSingleInitResponseTransfer->getCustomerTransactionId();
140
        if ($customerTransactionId) {
141
            $paymentComputopDetailEntity->setCustomerTransactionId((int)$customerTransactionId);
142
        }
143
144
        $paymentComputopDetailEntity->save();
145
    }
146
147
    /**
148
     * @param \Orm\Zed\Computop\Persistence\SpyPaymentComputopOrderItem[]|\Propel\Runtime\Collection\ObjectCollection $paymentComputopOrderItemEntities
149
     * @param string $paymentStatus
150
     *
151
     * @return void
152
     */
153
    protected function savePaymentComputopOrderItems(ObjectCollection $paymentComputopOrderItemEntities, string $paymentStatus): void
154
    {
155
        foreach ($paymentComputopOrderItemEntities as $paymentComputopOrderItem) {
156
            $paymentComputopOrderItem->setStatus($paymentStatus);
157
            $paymentComputopOrderItem->save();
158
        }
159
    }
160
161
    /**
162
     * @param \Generated\Shared\Transfer\ComputopNotificationTransfer $computopNotificationTransfer
163
     * @param \Orm\Zed\Computop\Persistence\SpyPaymentComputopOrderItem $paymentComputopOrderItemEntity
164
     *
165
     * @return string
166
     */
167
    protected function getCurrentOrderItemEntityStatus(
168
        ComputopNotificationTransfer $computopNotificationTransfer,
169
        SpyPaymentComputopOrderItem $paymentComputopOrderItemEntity
170
    ): string {
171
        $computopConfig = $this->getFactory()->getConfig();
172
173
        if (
174
            (int)$computopNotificationTransfer->getAmountauth() > 0 &&
175
            in_array($paymentComputopOrderItemEntity->getStatus(), $computopConfig->getBeforeAuthorizeStatuses())
176
        ) {
177
            return $computopConfig->getOmsStatusAuthorized();
178
        }
179
180
        if (
181
            (int)$computopNotificationTransfer->getAmountcap() > 0 &&
182
            (int)$computopNotificationTransfer->getAmountcap() === (int)$computopNotificationTransfer->getAmountauth() &&
183
            in_array($paymentComputopOrderItemEntity->getStatus(), $computopConfig->getBeforeCaptureStatuses())
184
        ) {
185
            return $computopConfig->getOmsStatusCaptured();
186
        }
187
188
        return $paymentComputopOrderItemEntity->getStatus();
189
    }
190
191
    /**
192
     * @param \Generated\Shared\Transfer\ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer
193
     *
194
     * @return string
195
     */
196
    protected function getPaymentStatus(ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer): string
197
    {
198
        $computopConfig = $this->getFactory()->getConfig();
199
200
        $computopApiResponseHeaderTransfer = $computopPayuCeeSingleInitResponseTransfer->getHeader();
201
        if ($computopApiResponseHeaderTransfer === null) {
202
            return $computopConfig->getOmsStatusNew();
203
        }
204
205
        $responseStatus = $computopApiResponseHeaderTransfer->getStatus();
206
        if ($responseStatus === null) {
207
            return $computopConfig->getOmsStatusNew();
208
        }
209
210
        if ($responseStatus === SharedComputopConfig::AUTHORIZE_REQUEST_STATUS) {
211
            return $computopConfig->getAuthorizeRequestOmsStatus();
212
        }
213
214
        if (
215
            $responseStatus === SharedComputopConfig::SUCCESS_OK &&
216
            $computopApiResponseHeaderTransfer->getDescription() === SharedComputopConfig::SUCCESS_STATUS
217
        ) {
218
            return $computopConfig->getOmsStatusAuthorized();
219
        }
220
221
        return $computopConfig->getOmsStatusNew();
222
    }
223
}
224