Passed
Push — feature/eco-3623-payone-pay-u-... ( 9cfa99...246d21 )
by Roman
04:37
created

ComputopEntityManager::getPaymentStatus()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
c 0
b 0
f 0
dl 0
loc 26
rs 9.2222
cc 6
nc 5
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\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 Propel\Runtime\Collection\ObjectCollection;
14
use Spryker\Zed\Kernel\Persistence\AbstractEntityManager;
15
16
/**
17
 * @method \SprykerEco\Zed\Computop\Persistence\ComputopPersistenceFactory getFactory()
18
 */
19
class ComputopEntityManager extends AbstractEntityManager implements ComputopEntityManagerInterface
20
{
21
    /**
22
     * @param \Generated\Shared\Transfer\ComputopNotificationTransfer $computopNotificationTransfer
23
     *
24
     * @return void
25
     */
26
    public function savePaymentComputopNotification(ComputopNotificationTransfer $computopNotificationTransfer): void
27
    {
28
        $paymentComputopNotificationEntity = $this->getFactory()
29
            ->createPaymentComputopNotificationQuery()
30
            ->filterByPayId($computopNotificationTransfer->getPayId())
31
            ->filterByTransId($computopNotificationTransfer->getTransId())
32
            ->filterByXId($computopNotificationTransfer->getXId())
33
            ->findOneOrCreate();
34
35
        $paymentComputopNotificationEntity->fromArray(
36
            $computopNotificationTransfer->modifiedToArray()
37
        );
38
        $paymentComputopNotificationEntity->save();
39
    }
40
41
    /**
42
     * @param \Generated\Shared\Transfer\ComputopNotificationTransfer $computopNotificationTransfer
43
     *
44
     * @return bool
45
     */
46
    public function updatePaymentComputopOrderItemPaymentConfirmation(
47
        ComputopNotificationTransfer $computopNotificationTransfer
48
    ): bool {
49
        /** @var \Orm\Zed\Computop\Persistence\SpyPaymentComputopOrderItem[]|\Propel\Runtime\Collection\ObjectCollection $paymentComputopOrderItemEntities */
50
        $paymentComputopOrderItemEntities = $this->getFactory()
51
            ->createPaymentComputopOrderItemQuery()
52
            ->useSpyPaymentComputopQuery()
53
                ->filterByTransId($computopNotificationTransfer->getTransId())
54
                ->filterByPayId($computopNotificationTransfer->getPayId())
55
            ->endUse()
56
            ->find();
57
58
        if (!$paymentComputopOrderItemEntities->count()) {
59
            return false;
60
        }
61
62
        foreach ($paymentComputopOrderItemEntities as $paymentComputopOrderItemEntity) {
63
            $orderItemEntityStatus = $this->getCurrentOrderItemEntityStatus($computopNotificationTransfer);
64
            $paymentComputopOrderItemEntity
65
                ->setIsPaymentConfirmed((bool)$computopNotificationTransfer->getIsSuccess())
66
                ->setStatus($orderItemEntityStatus)
67
                ->save();
68
        }
69
70
        return true;
71
    }
72
73
    /**
74
     * @param \Generated\Shared\Transfer\ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer
75
     * @param string $orderItemsStatus
76
     *
77
     * @return void
78
     */
79
    public function saveComputopPayuCeeSingleInitResponse(
80
        ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer,
81
        string $orderItemsStatus
82
    ): void {
83
        $computopApiResponseHeaderTransfer = $computopPayuCeeSingleInitResponseTransfer->requireHeader()
84
            ->getHeader()
85
                ->requireTransId()
86
                ->requirePayId()
87
                ->requireXid();
88
89
        $paymentComputopEntity = $this->getFactory()
90
            ->createPaymentComputopQuery()
91
            ->filterByTransId($computopApiResponseHeaderTransfer->getTransId())
92
            ->findOne();
93
94
        if (!$paymentComputopEntity) {
95
            return;
96
        }
97
98
        $paymentComputopEntity
99
            ->setPayId($computopApiResponseHeaderTransfer->getPayId())
100
            ->setXId($computopApiResponseHeaderTransfer->getXId())
101
            ->save();
102
103
        $this->savePaymentComputopDetailEntity(
104
            $paymentComputopEntity->getSpyPaymentComputopDetail(),
105
            $computopPayuCeeSingleInitResponseTransfer
106
        );
107
108
        $this->savePaymentComputopOrderItems($paymentComputopEntity->getSpyPaymentComputopOrderItems(), $orderItemsStatus);
109
    }
110
111
    /**
112
     * @param \Orm\Zed\Computop\Persistence\SpyPaymentComputopDetail $paymentComputopDetailEntity
113
     * @param \Generated\Shared\Transfer\ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer
114
     *
115
     * @return void
116
     */
117
    protected function savePaymentComputopDetailEntity(
118
        SpyPaymentComputopDetail $paymentComputopDetailEntity,
119
        ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer
120
    ): void {
121
        $paymentComputopDetailEntity->fromArray($computopPayuCeeSingleInitResponseTransfer->toArray());
122
        $customerTransactionId = $computopPayuCeeSingleInitResponseTransfer->getCustomerTransactionId();
123
        if ($customerTransactionId) {
124
            $paymentComputopDetailEntity->setCustomerTransactionId((int)$customerTransactionId);
125
        }
126
127
        $paymentComputopDetailEntity->save();
128
    }
129
130
    /**
131
     * @param \Orm\Zed\Computop\Persistence\SpyPaymentComputopOrderItem[]|\Propel\Runtime\Collection\ObjectCollection $paymentComputopOrderItemEntities
132
     * @param string $paymentStatus
133
     *
134
     * @return void
135
     */
136
    public function savePaymentComputopOrderItems(ObjectCollection $paymentComputopOrderItemEntities, string $paymentStatus): void
137
    {
138
        foreach ($paymentComputopOrderItemEntities as $paymentComputopOrderItem) {
139
            $paymentComputopOrderItem->setStatus($paymentStatus);
140
            $paymentComputopOrderItem->save();
141
        }
142
    }
143
144
    /**
145
     * @param \Generated\Shared\Transfer\ComputopNotificationTransfer $computopNotificationTransfer
146
     *
147
     * @return string
148
     */
149
    protected function getCurrentOrderItemEntityStatus(ComputopNotificationTransfer $computopNotificationTransfer): string
150
    {
151
        if ((int)$computopNotificationTransfer->getAmountcap() > 0) {
152
            return $this->getFactory()->getConfig()->getOmsStatusCaptured();
153
        }
154
155
        if ((int)$computopNotificationTransfer->getAmountauth() > 0) {
156
            return $this->getFactory()->getConfig()->getOmsStatusAuthorized();
157
        }
158
159
        return $this->getFactory()->getConfig()->getOmsStatusNew();
160
    }
161
}
162