Passed
Push — bugfix/cc-14473-computop-notif... ( 82b7b5...28957a )
by Michael
05:57 queued 02:12
created

NotificationProcessor::updatePaymentComputop()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
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\Business\Processor;
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\ComputopPaymentComputopTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...PaymentComputopTransfer 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 Spryker\Zed\Kernel\Persistence\EntityManager\TransactionTrait;
13
use SprykerEco\Zed\Computop\ComputopConfig;
14
use SprykerEco\Zed\Computop\Persistence\ComputopEntityManagerInterface;
15
use SprykerEco\Zed\Computop\Persistence\ComputopRepositoryInterface;
16
17
class NotificationProcessor implements NotificationProcessorInterface
18
{
19
    use TransactionTrait;
20
21
    /**
22
     * @var \SprykerEco\Zed\Computop\Persistence\ComputopEntityManagerInterface
23
     */
24
    protected $computopEntityManager;
25
26
    /**
27
     * @var \SprykerEco\Zed\Computop\Persistence\ComputopRepositoryInterface
28
     */
29
    protected $computopRepository;
30
31
    /**
32
     * @var \SprykerEco\Zed\Computop\ComputopConfig
33
     */
34
    protected $computopConfig;
35
36
    /**
37
     * @param \SprykerEco\Zed\Computop\Persistence\ComputopEntityManagerInterface $computopEntityManager
38
     * @param \SprykerEco\Zed\Computop\Persistence\ComputopRepositoryInterface $computopRepository
39
     * @param \SprykerEco\Zed\Computop\ComputopConfig $computopConfig
40
     */
41
    public function __construct(
42
        ComputopEntityManagerInterface $computopEntityManager,
43
        ComputopRepositoryInterface $computopRepository,
44
        ComputopConfig $computopConfig
45
    ) {
46
        $this->computopEntityManager = $computopEntityManager;
47
        $this->computopRepository = $computopRepository;
48
        $this->computopConfig = $computopConfig;
49
    }
50
51
    /**
52
     * @param \Generated\Shared\Transfer\ComputopNotificationTransfer $computopNotificationTransfer
53
     *
54
     * @return \Generated\Shared\Transfer\ComputopNotificationTransfer
55
     */
56
    public function processNotification(
57
        ComputopNotificationTransfer $computopNotificationTransfer
58
    ): ComputopNotificationTransfer {
59
        return $this->getTransactionHandler()->handleTransaction(
60
            function () use ($computopNotificationTransfer): ComputopNotificationTransfer {
61
                return $this->executeSaveComputopNotificationTransaction($computopNotificationTransfer);
62
            },
63
        );
64
    }
65
66
    /**
67
     * @param \Generated\Shared\Transfer\ComputopNotificationTransfer $computopNotificationTransfer
68
     *
69
     * @return \Generated\Shared\Transfer\ComputopNotificationTransfer
70
     */
71
    protected function executeSaveComputopNotificationTransaction(
72
        ComputopNotificationTransfer $computopNotificationTransfer
73
    ): ComputopNotificationTransfer {
74
        $this->computopEntityManager->savePaymentComputopNotification($computopNotificationTransfer);
75
        $paymentComputopTransfer = $this->computopRepository->findComputopPaymentByComputopTransId(
76
            $computopNotificationTransfer->getTransIdOrFail(),
77
        );
78
79
        if (!$paymentComputopTransfer) {
80
            return $computopNotificationTransfer->setIsProcessed(false);
81
        }
82
83
        $this->updatePaymentComputop($paymentComputopTransfer, $computopNotificationTransfer);
84
85
        $isProcessed = $this->updatePaymentComputopOrderItems($paymentComputopTransfer, $computopNotificationTransfer);
86
87
        return $computopNotificationTransfer->setIsProcessed($isProcessed);
88
    }
89
90
    /**
91
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopTransfer $computopPaymentComputopTransfer
92
     * @param \Generated\Shared\Transfer\ComputopNotificationTransfer $computopNotificationTransfer
93
     *
94
     * @return void
95
     */
96
    protected function updatePaymentComputop(
97
        ComputopPaymentComputopTransfer $computopPaymentComputopTransfer,
98
        ComputopNotificationTransfer $computopNotificationTransfer
99
    ): void {
100
        $computopPaymentComputopTransfer
101
            ->setPayId($computopNotificationTransfer->getPayId())
102
            ->setXId($computopNotificationTransfer->getXId());
103
104
        $this->computopEntityManager->updateComputopPayment($computopPaymentComputopTransfer);
105
    }
106
107
    /**
108
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopTransfer $computopPaymentComputopTransfer
109
     * @param \Generated\Shared\Transfer\ComputopNotificationTransfer $computopNotificationTransfer
110
     *
111
     * @return bool
112
     */
113
    protected function updatePaymentComputopOrderItems(
114
        ComputopPaymentComputopTransfer $computopPaymentComputopTransfer,
115
        ComputopNotificationTransfer $computopNotificationTransfer
116
    ): bool {
117
        $orderItemsPaymentStatus = $this->getCurrentOrderItemEntityStatus($computopNotificationTransfer);
118
119
        $paymentComputopOrderItemsCollection = $this->computopRepository
120
            ->getComputopPaymentComputopOrderItemCollection($computopPaymentComputopTransfer);
121
122
        if ($paymentComputopOrderItemsCollection->getComputopPaymentComputopOrderItems()->count() === 0) {
123
            return false;
124
        }
125
126
        foreach ($paymentComputopOrderItemsCollection->getComputopPaymentComputopOrderItems() as $paymentComputopOrderItem) {
127
            $paymentComputopOrderItem
128
                ->setStatus($orderItemsPaymentStatus)
129
                ->setIsPaymentConfirmed((bool)$computopNotificationTransfer->getIsSuccess());
130
131
            $this->computopEntityManager->updateComputopPaymentComputopOrderItem($paymentComputopOrderItem);
132
        }
133
134
        return true;
135
    }
136
137
    /**
138
     * @param \Generated\Shared\Transfer\ComputopNotificationTransfer $computopNotificationTransfer
139
     *
140
     * @return string|null
141
     */
142
    protected function getCurrentOrderItemEntityStatus(ComputopNotificationTransfer $computopNotificationTransfer): ?string
143
    {
144
        if ((int)$computopNotificationTransfer->getAmountcap() > 0) {
145
            return $this->computopConfig->getOmsStatusCaptured();
146
        }
147
148
        if ((int)$computopNotificationTransfer->getAmountauth() > 0) {
149
            return $this->computopConfig->getOmsStatusAuthorized();
150
        }
151
152
        return null;
153
    }
154
}
155