Passed
Pull Request — master (#41)
by Roman
05:10
created

savePaymentComputopOrderItems()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 2
nc 2
nop 2
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
     * @param string|null $orderItemsStatus
44
     *
45
     * @return bool
46
     */
47
    public function updatePaymentComputopOrderItemPaymentConfirmation(
48
        ComputopNotificationTransfer $computopNotificationTransfer,
49
        ?string $orderItemsStatus
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
            $paymentComputopOrderItemEntity
81
                ->setIsPaymentConfirmed((bool)$computopNotificationTransfer->getIsSuccess())
82
                ->setStatus($orderItemsStatus ?? $paymentComputopOrderItemEntity->getStatus())
83
                ->save();
84
        }
85
86
        return true;
87
    }
88
89
    /**
90
     * @param \Generated\Shared\Transfer\ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer
91
     * @param string $orderItemsStatus
92
     *
93
     * @return void
94
     */
95
    public function saveComputopPayuCeeSingleInitResponse(
96
        ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer,
97
        string $orderItemsStatus
98
    ): void {
99
        $computopApiResponseHeaderTransfer = $computopPayuCeeSingleInitResponseTransfer->requireHeader()
100
            ->getHeader()
101
                ->requireTransId()
102
                ->requirePayId()
103
                ->requireXid();
104
105
        $paymentComputopEntity = $this->getFactory()
106
            ->createPaymentComputopQuery()
107
            ->filterByTransId($computopApiResponseHeaderTransfer->getTransId())
108
            ->findOne();
109
110
        if (!$paymentComputopEntity) {
111
            return;
112
        }
113
114
        $paymentComputopEntity
115
            ->setPayId($computopApiResponseHeaderTransfer->getPayId())
116
            ->setXId($computopApiResponseHeaderTransfer->getXId())
117
            ->save();
118
119
        $this->savePaymentComputopDetailEntity(
120
            $paymentComputopEntity->getSpyPaymentComputopDetail(),
121
            $computopPayuCeeSingleInitResponseTransfer
122
        );
123
124
        $this->savePaymentComputopOrderItems($paymentComputopEntity->getSpyPaymentComputopOrderItems(), $orderItemsStatus);
125
    }
126
127
    /**
128
     * @param \Orm\Zed\Computop\Persistence\SpyPaymentComputopDetail $paymentComputopDetailEntity
129
     * @param \Generated\Shared\Transfer\ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer
130
     *
131
     * @return void
132
     */
133
    protected function savePaymentComputopDetailEntity(
134
        SpyPaymentComputopDetail $paymentComputopDetailEntity,
135
        ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer
136
    ): void {
137
        $paymentComputopDetailEntity->fromArray($computopPayuCeeSingleInitResponseTransfer->toArray());
138
        $customerTransactionId = $computopPayuCeeSingleInitResponseTransfer->getCustomerTransactionId();
139
        if ($customerTransactionId) {
140
            $paymentComputopDetailEntity->setCustomerTransactionId((int)$customerTransactionId);
141
        }
142
143
        $paymentComputopDetailEntity->save();
144
    }
145
146
    /**
147
     * @param \Orm\Zed\Computop\Persistence\SpyPaymentComputopOrderItem[]|\Propel\Runtime\Collection\ObjectCollection $paymentComputopOrderItemEntities
148
     * @param string $paymentStatus
149
     *
150
     * @return void
151
     */
152
    public function savePaymentComputopOrderItems(ObjectCollection $paymentComputopOrderItemEntities, string $paymentStatus): void
153
    {
154
        foreach ($paymentComputopOrderItemEntities as $paymentComputopOrderItem) {
155
            $paymentComputopOrderItem->setStatus($paymentStatus);
156
            $paymentComputopOrderItem->save();
157
        }
158
    }
159
}
160