Passed
Push — feature/eco-3623-payone-pay-u-... ( acd9a9...8799f8 )
by Roman
06:13
created

updatePaymentComputopOrderItemPaymentConfirmation()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
eloc 16
c 3
b 1
f 1
dl 0
loc 25
rs 9.7333
cc 3
nc 3
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 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...
12
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...
13
use Propel\Runtime\Collection\ObjectCollection;
14
use Spryker\Shared\Kernel\Transfer\TransferInterface;
15
use Spryker\Zed\Kernel\Persistence\AbstractEntityManager;
16
use SprykerEco\Zed\Computop\ComputopConfig;
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\SpyPaymentComputopOrderItem[]|\Propel\Runtime\Collection\ObjectCollection $paymentComputopOrderItemEntities */
52
        $paymentComputopOrderItemEntities = $this->getFactory()
53
            ->createPaymentComputopOrderItemQuery()
54
            ->useSpyPaymentComputopQuery()
55
                ->filterByTransId($computopNotificationTransfer->getTransId())
56
                ->filterByPayId($computopNotificationTransfer->getPayId())
57
            ->endUse()
58
            ->find();
59
60
        if (!$paymentComputopOrderItemEntities->count()) {
61
            return false;
62
        }
63
64
        foreach ($paymentComputopOrderItemEntities as $paymentComputopOrderItemEntity) {
65
            $orderItemEntityStatus = $this->getCurrentOrderItemEntityStatus($computopNotificationTransfer, $paymentComputopOrderItemEntity);
66
            $paymentComputopOrderItemEntity
67
                ->setIsPaymentConfirmed((bool)$computopNotificationTransfer->getIsSuccess())
68
                ->setStatus($orderItemEntityStatus)
69
                ->save();
70
        }
71
72
        return true;
73
    }
74
75
    /**
76
     * @param \Spryker\Shared\Kernel\Transfer\TransferInterface $responseTransfer
77
     * @param string|null $paymentStatus
78
     *
79
     * @return void
80
     */
81
    public function savePaymentResponse(TransferInterface $responseTransfer, ?string $paymentStatus = null): void
82
    {
83
        $header = $responseTransfer->requireHeader()
0 ignored issues
show
Bug introduced by
The method requireHeader() does not exist on Spryker\Shared\Kernel\Transfer\TransferInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
        $header = $responseTransfer->/** @scrutinizer ignore-call */ requireHeader()

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
84
            ->getHeader()
85
                ->requireTransId()
86
                ->requirePayId()
87
                ->requireXid();
88
89
        $paymentEntity = $this->getFactory()
90
            ->createPaymentComputopQuery()
91
            ->filterByTransId($header->getTransId())
92
            ->findOne();
93
94
        if (!$paymentEntity) {
95
            return;
96
        }
97
98
        $paymentEntity
99
            ->setPayId($header->getPayId())
100
            ->setXId($header->getXId())
101
            ->save();
102
103
        $this->savePaymentDetailEntity($paymentEntity->getSpyPaymentComputopDetail(), $responseTransfer);
104
105
        $this->saveAuthorizedPaymentOrderItems($paymentEntity->getSpyPaymentComputopOrderItems(), $paymentStatus);
106
    }
107
108
    /**
109
     * @param \Orm\Zed\Computop\Persistence\SpyPaymentComputopDetail $paymentEntityDetails
110
     * @param \Generated\Shared\Transfer\ComputopPayuCeeSingleInitResponseTransfer $responseTransfer
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...
111
     *
112
     * @return void
113
     */
114
    public function savePaymentDetailEntity(
115
        SpyPaymentComputopDetail $paymentEntityDetails,
116
        TransferInterface $responseTransfer
117
    ): void {
118
        $paymentEntityDetails->fromArray($responseTransfer->toArray());
119
        $customerTransactionId = $responseTransfer->getCustomerTransactionId();
0 ignored issues
show
Bug introduced by
The method getCustomerTransactionId() does not exist on Spryker\Shared\Kernel\Transfer\TransferInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

119
        /** @scrutinizer ignore-call */ 
120
        $customerTransactionId = $responseTransfer->getCustomerTransactionId();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
120
        if ($customerTransactionId) {
121
            $paymentEntityDetails->setCustomerTransactionId((int)$customerTransactionId);
122
        }
123
124
        $paymentEntityDetails->save();
125
    }
126
127
    /**
128
     * @param \Orm\Zed\Computop\Persistence\SpyPaymentComputopOrderItem[]|\Propel\Runtime\Collection\ObjectCollection $paymentComputopOrderItems
129
     * @param string|null $paymentStatus
130
     *
131
     * @return void
132
     */
133
    public function saveAuthorizedPaymentOrderItems(ObjectCollection $paymentComputopOrderItems, ?string $paymentStatus = null): void
134
    {
135
        $paymentStatus = $paymentStatus ?? $this->getFactory()->getConfig()->getOmsStatusAuthorized();
136
        foreach ($paymentComputopOrderItems as $paymentComputopOrderItem) {
137
            $paymentComputopOrderItem->setStatus($paymentStatus);
138
            $paymentComputopOrderItem->save();
139
        }
140
    }
141
142
    /**
143
     * @param \Generated\Shared\Transfer\ComputopNotificationTransfer $computopNotificationTransfer
144
     * @param \Orm\Zed\Computop\Persistence\SpyPaymentComputopOrderItem $paymentComputopOrderItemEntity
145
     *
146
     * @return string
147
     */
148
    protected function getCurrentOrderItemEntityStatus(
149
        ComputopNotificationTransfer $computopNotificationTransfer,
150
        SpyPaymentComputopOrderItem $paymentComputopOrderItemEntity
151
    ): string {
152
        if (
153
            $paymentComputopOrderItemEntity->getStatus() === ComputopConfig::OMS_STATUS_NEW &&
154
            (int)$computopNotificationTransfer->getAmountauth() > 0
155
        ) {
156
            return $this->getFactory()->getConfig()->getOmsStatusAuthorized();
157
        }
158
159
        if (
160
            $paymentComputopOrderItemEntity->getStatus() === ComputopConfig::OMS_STATUS_AUTHORIZED &&
161
            (int)$computopNotificationTransfer->getAmountcap() === (int)$computopNotificationTransfer->getAmountauth()
162
        ) {
163
            return $this->getFactory()->getConfig()->getOmsStatusCaptured();
164
        }
165
166
        return $paymentComputopOrderItemEntity->getStatus();
167
    }
168
}
169