Passed
Pull Request — master (#45)
by
unknown
03:36
created

PayuCeeSingleResponseSaver::save()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 27
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\Business\Payment\Handler\Saver\Init;
9
10
use Generated\Shared\Transfer\ComputopApiResponseHeaderTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...iResponseHeaderTransfer 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 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...
13
use Generated\Shared\Transfer\PaymentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PaymentTransfer 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 Generated\Shared\Transfer\QuoteTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\QuoteTransfer 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...
15
use Spryker\Zed\Kernel\Persistence\EntityManager\TransactionTrait;
16
use SprykerEco\Shared\Computop\ComputopConfig as SharedComputopConfig;
17
use SprykerEco\Zed\Computop\ComputopConfig;
18
use SprykerEco\Zed\Computop\Persistence\ComputopEntityManagerInterface;
19
use SprykerEco\Zed\Computop\Persistence\ComputopRepositoryInterface;
20
21
class PayuCeeSingleResponseSaver implements InitResponseSaverInterface
22
{
23
    use TransactionTrait;
24
25
    /**
26
     * @var \SprykerEco\Zed\Computop\Persistence\ComputopEntityManagerInterface
27
     */
28
    protected $computopEntityManager;
29
30
    /**
31
     * @var \SprykerEco\Zed\Computop\ComputopConfig
32
     */
33
    protected $computopConfig;
34
35
    /**
36
     * @var \SprykerEco\Zed\Computop\Persistence\ComputopRepositoryInterface
37
     */
38
    protected $computopRepository;
39
40
    /**
41
     * @param \SprykerEco\Zed\Computop\ComputopConfig $computopConfig
42
     * @param \SprykerEco\Zed\Computop\Persistence\ComputopRepositoryInterface $computopRepository
43
     * @param \SprykerEco\Zed\Computop\Persistence\ComputopEntityManagerInterface $computopEntityManager
44
     */
45
    public function __construct(
46
        ComputopConfig $computopConfig,
47
        ComputopRepositoryInterface $computopRepository,
48
        ComputopEntityManagerInterface $computopEntityManager
49
    ) {
50
        $this->computopConfig = $computopConfig;
51
        $this->computopRepository = $computopRepository;
52
        $this->computopEntityManager = $computopEntityManager;
53
    }
54
55
    /**
56
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
57
     *
58
     * @return \Generated\Shared\Transfer\QuoteTransfer
59
     */
60
    public function save(QuoteTransfer $quoteTransfer): QuoteTransfer
61
    {
62
        $computopPayuCeeSingleInitResponseTransfer = $this->getPaymentTransferFromQuote($quoteTransfer)
63
            ->getComputopPayuCeeSingleOrFail()
64
            ->getPayuCeeSingleInitResponseOrFail();
65
66
        $computopApiResponseHeaderTransfer = $computopPayuCeeSingleInitResponseTransfer->getHeaderOrFail()
67
            ->requireTransId()
68
            ->requirePayId()
69
            ->requireXid();
70
71
        if (!$computopApiResponseHeaderTransfer->getIsSuccess()) {
72
            return $quoteTransfer;
73
        }
74
75
        $computopPaymentComputopTransfer = $this->computopRepository->findComputopPaymentByComputopTransId(
76
            $computopApiResponseHeaderTransfer->getTransId(),
77
        );
78
        if ($computopPaymentComputopTransfer === null) {
79
            return $quoteTransfer;
80
        }
81
82
        $this->getTransactionHandler()->handleTransaction(function () use ($computopPaymentComputopTransfer, $computopPayuCeeSingleInitResponseTransfer) {
83
            $this->executeSavePaymentResponseTransaction($computopPaymentComputopTransfer, $computopPayuCeeSingleInitResponseTransfer);
84
        });
85
86
        return $quoteTransfer;
87
    }
88
89
    /**
90
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopTransfer $computopPaymentComputopTransfer
91
     * @param \Generated\Shared\Transfer\ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer
92
     *
93
     * @return void
94
     */
95
    protected function executeSavePaymentResponseTransaction(
96
        ComputopPaymentComputopTransfer $computopPaymentComputopTransfer,
97
        ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer
98
    ): void {
99
        $this->savePaymentComputopEntity($computopPaymentComputopTransfer, $computopPayuCeeSingleInitResponseTransfer->getHeader());
100
        $this->savePaymentComputopDetailEntity($computopPaymentComputopTransfer, $computopPayuCeeSingleInitResponseTransfer);
101
        $paymentStatus = $this->getOrderItemPaymentStatusFromResponseHeader($computopPayuCeeSingleInitResponseTransfer->getHeader());
102
        if ($paymentStatus) {
103
            $this->savePaymentComputopOrderItemsEntities($computopPaymentComputopTransfer, $paymentStatus);
104
        }
105
    }
106
107
    /**
108
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
109
     *
110
     * @return \Generated\Shared\Transfer\PaymentTransfer
111
     */
112
    protected function getPaymentTransferFromQuote(QuoteTransfer $quoteTransfer): PaymentTransfer
113
    {
114
        foreach ($quoteTransfer->getPayments() as $paymentTransfer) {
115
            if ($paymentTransfer !== null) {
116
                return $paymentTransfer;
117
            }
118
        }
119
120
        return $quoteTransfer->getPaymentOrFail();
121
    }
122
123
    /**
124
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopTransfer $computopPaymentComputopTransfer
125
     * @param \Generated\Shared\Transfer\ComputopApiResponseHeaderTransfer $computopApiResponseHeaderTransfer
126
     *
127
     * @return void
128
     */
129
    protected function savePaymentComputopEntity(
130
        ComputopPaymentComputopTransfer $computopPaymentComputopTransfer,
131
        ComputopApiResponseHeaderTransfer $computopApiResponseHeaderTransfer
132
    ): void {
133
        $computopPaymentComputopTransfer
134
            ->setPayId($computopApiResponseHeaderTransfer->getPayId())
135
            ->setXId($computopApiResponseHeaderTransfer->getXId());
136
137
        $this->computopEntityManager->updateComputopPayment($computopPaymentComputopTransfer);
138
    }
139
140
    /**
141
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopTransfer $computopPaymentComputopTransfer
142
     * @param \Generated\Shared\Transfer\ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer
143
     *
144
     * @return void
145
     */
146
    protected function savePaymentComputopDetailEntity(
147
        ComputopPaymentComputopTransfer $computopPaymentComputopTransfer,
148
        ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer
149
    ): void {
150
        $computopPaymentComputopDetailTransfer = $this->computopRepository->findComputopPaymentDetail($computopPaymentComputopTransfer);
151
        if (!$computopPaymentComputopDetailTransfer) {
152
            return;
153
        }
154
155
        $computopPaymentComputopDetailTransfer->fromArray($computopPayuCeeSingleInitResponseTransfer->toArray(), true);
156
        $computopPaymentComputopDetailTransfer->setCustomerTransactionId(
157
            (int)$computopPaymentComputopDetailTransfer->getCustomerTransactionId(),
158
        );
159
160
        $this->computopEntityManager->updateComputopPaymentDetail($computopPaymentComputopDetailTransfer);
161
    }
162
163
    /**
164
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopTransfer $computopPaymentComputopTransfer
165
     * @param string $paymentStatus
166
     *
167
     * @return void
168
     */
169
    protected function savePaymentComputopOrderItemsEntities(
170
        ComputopPaymentComputopTransfer $computopPaymentComputopTransfer,
171
        string $paymentStatus
172
    ): void {
173
        $computopPaymentComputopOrderItemCollection = $this->computopRepository
174
            ->getComputopPaymentComputopOrderItemCollection($computopPaymentComputopTransfer);
175
176
        foreach ($computopPaymentComputopOrderItemCollection->getComputopPaymentComputopOrderItems() as $computopPaymentComputopOrderItem) {
177
            $computopPaymentComputopOrderItem->setStatus($paymentStatus);
178
            $this->computopEntityManager->updateComputopPaymentComputopOrderItem($computopPaymentComputopOrderItem);
179
        }
180
    }
181
182
    /**
183
     * @param \Generated\Shared\Transfer\ComputopApiResponseHeaderTransfer $computopApiResponseHeaderTransfer
184
     *
185
     * @return string|null
186
     */
187
    protected function getOrderItemPaymentStatusFromResponseHeader(
188
        ComputopApiResponseHeaderTransfer $computopApiResponseHeaderTransfer
189
    ): ?string {
190
        if ($computopApiResponseHeaderTransfer->getStatus() === SharedComputopConfig::AUTHORIZE_REQUEST_STATUS) {
191
            return $this->computopConfig->getAuthorizeRequestOmsStatus();
192
        }
193
194
        if ($computopApiResponseHeaderTransfer->getStatus() === SharedComputopConfig::SUCCESS_OK) {
195
            return $this->computopConfig->getOmsStatusAuthorized();
196
        }
197
198
        return null;
199
    }
200
}
201