Passed
Push — feature/eco-3135/eco-3149-dire... ( e79f9f...f6c32e )
by Volodymyr
05:30
created

isQuoteExpired()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 2
nc 4
nop 1
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Yves\Heidelpay\Processor;
9
10
use Generated\Shared\Transfer\HeidelpayDirectDebitRegistrationTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfe...bitRegistrationTransfer 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\HeidelpayResponseErrorTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfe...ayResponseErrorTransfer 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\QuoteTransfer;
1 ignored issue
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...
13
use Spryker\Shared\Kernel\Transfer\Exception\RequiredTransferPropertyException;
14
use SprykerEco\Client\Heidelpay\HeidelpayClientInterface;
15
use SprykerEco\Shared\Heidelpay\HeidelpayConfig;
16
use SprykerEco\Shared\Heidelpay\QuoteUniqueIdGenerator;
17
use SprykerEco\Yves\Heidelpay\Dependency\Client\HeidelpayToQuoteClientInterface;
18
use Symfony\Component\HttpFoundation\Request;
19
20
class HeidelpayDirectDebitRegistrationProcessor implements HeidelpayDirectDebitRegistrationProcessorInterface
21
{
22
    protected const REQUEST_PARAM_REGISTRATION_ID = 'id_registration';
23
    protected const ERROR_CODE_REGISTRATION_NOT_FOUND = 'registration_not_found';
24
    protected const ERROR_CODE_REGISTRATION_NOT_SAVED = 'registration_not_saved';
25
    protected const ERROR_CODE_QUOTE_EXPIRED = 'quote_expired';
26
27
    /**
28
     * @var \SprykerEco\Client\Heidelpay\HeidelpayClientInterface
29
     */
30
    protected $heidelpayClient;
31
32
    /**
33
     * @var \SprykerEco\Yves\Heidelpay\Dependency\Client\HeidelpayToQuoteClientInterface
34
     */
35
    protected $quoteClient;
36
37
    /**
38
     * @param \SprykerEco\Client\Heidelpay\HeidelpayClientInterface $heidelpayClient
39
     * @param \SprykerEco\Yves\Heidelpay\Dependency\Client\HeidelpayToQuoteClientInterface $quoteClient
40
     */
41
    public function __construct(
42
        HeidelpayClientInterface $heidelpayClient,
43
        HeidelpayToQuoteClientInterface $quoteClient
44
    ) {
45
        $this->heidelpayClient = $heidelpayClient;
46
        $this->quoteClient = $quoteClient;
47
    }
48
49
    /**
50
     * @param \Symfony\Component\HttpFoundation\Request $request
51
     *
52
     * @return \Generated\Shared\Transfer\HeidelpayDirectDebitRegistrationTransfer
53
     */
54
    public function processNewRegistration(Request $request): HeidelpayDirectDebitRegistrationTransfer
55
    {
56
        $directDebitRegistrationTransfer = $this->createNewDirectDebitRegistrationTransferFromRequest($request);
57
        if ($directDebitRegistrationTransfer->getIsError()) {
58
            return $directDebitRegistrationTransfer;
59
        }
60
61
        $directDebitRegistrationTransfer = $this->heidelpayClient
62
            ->saveDirectDebitRegistration($directDebitRegistrationTransfer);
63
64
        if ($directDebitRegistrationTransfer->getIdDirectDebitRegistration() !== null) {
65
            return $directDebitRegistrationTransfer;
66
        }
67
68
        return $this->addError(
69
            static::ERROR_CODE_REGISTRATION_NOT_SAVED,
70
            $directDebitRegistrationTransfer
71
        );
72
    }
73
74
    /**
75
     * @param \Symfony\Component\HttpFoundation\Request $request
76
     *
77
     * @return \Generated\Shared\Transfer\HeidelpayDirectDebitRegistrationTransfer
78
     */
79
    public function processSuccessRegistration(Request $request): HeidelpayDirectDebitRegistrationTransfer
80
    {
81
        $quoteTransfer = $this->quoteClient->getQuote();
82
        $directDebitRegistrationTransfer = $this->retrieveExistingDirectDebitRegistrationByRequestAndQuote($request, $quoteTransfer);
83
        if ($this->isQuoteExpired($quoteTransfer)) {
84
            return $this->addError(
85
                static::ERROR_CODE_QUOTE_EXPIRED,
86
                $directDebitRegistrationTransfer
87
            );
88
        }
89
90
        $directDebitRegistrationTransfer = $this->heidelpayClient
91
            ->retrieveDirectDebitRegistration($directDebitRegistrationTransfer);
92
93
        if ($directDebitRegistrationTransfer->getIdDirectDebitRegistration() === null) {
94
            return $this->addError(
95
                static::ERROR_CODE_REGISTRATION_NOT_FOUND,
96
                $directDebitRegistrationTransfer
97
            );
98
        }
99
100
        $quoteTransfer = $this->addDirectDebitRegistrationToQuote($directDebitRegistrationTransfer, $quoteTransfer);
101
        $this->quoteClient->setQuote($quoteTransfer);
102
103
        return $directDebitRegistrationTransfer;
104
    }
105
106
    /**
107
     * @param \Symfony\Component\HttpFoundation\Request $request
108
     *
109
     * @return \Generated\Shared\Transfer\HeidelpayDirectDebitRegistrationTransfer
110
     */
111
    protected function createNewDirectDebitRegistrationTransferFromRequest(Request $request): HeidelpayDirectDebitRegistrationTransfer
112
    {
113
        $apiResponseAsArray = $this->getUrldecodedRequestBody($request);
114
        $apiResponseAsArray = $this->filterResponseParameters($apiResponseAsArray);
115
116
        return $this->heidelpayClient->parseDirectDebitRegistrationResponse($apiResponseAsArray);
117
    }
118
119
    /**
120
     * @param \Symfony\Component\HttpFoundation\Request $request
121
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
122
     *
123
     * @return \Generated\Shared\Transfer\HeidelpayDirectDebitRegistrationTransfer
124
     */
125
    protected function retrieveExistingDirectDebitRegistrationByRequestAndQuote(
126
        Request $request,
127
        QuoteTransfer $quoteTransfer
128
    ): HeidelpayDirectDebitRegistrationTransfer {
129
        $directDebitRegistrationTransfer = (new HeidelpayDirectDebitRegistrationTransfer())
130
            ->setRegistrationUniqueId($this->getRegistrationId($request))
131
            ->setIdCustomerAddress($quoteTransfer->getShippingAddress()->getIdCustomerAddress())
132
            ->setTransactionId($this->generateTransactionId($quoteTransfer));
133
134
        return $directDebitRegistrationTransfer;
135
    }
136
137
    /**
138
     * @param string $code
139
     * @param \Generated\Shared\Transfer\HeidelpayDirectDebitRegistrationTransfer $directDebitRegistrationTransfer
140
     *
141
     * @return \Generated\Shared\Transfer\HeidelpayDirectDebitRegistrationTransfer
142
     */
143
    protected function addError(string $code, HeidelpayDirectDebitRegistrationTransfer $directDebitRegistrationTransfer): HeidelpayDirectDebitRegistrationTransfer
144
    {
145
        $directDebitRegistrationTransfer
146
            ->setIsError(true)
147
            ->setError(
148
                (new HeidelpayResponseErrorTransfer())
149
                    ->setCode($code)
150
            );
151
152
        return $directDebitRegistrationTransfer;
153
    }
154
155
    /**
156
     * @param \Symfony\Component\HttpFoundation\Request $request
157
     *
158
     * @return int
159
     */
160
    protected function getRegistrationId(Request $request): int
161
    {
162
        return (int)$request->get(static::REQUEST_PARAM_REGISTRATION_ID);
163
    }
164
165
    /**
166
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
167
     *
168
     * @return bool
169
     */
170
    protected function isQuoteExpired(QuoteTransfer $quoteTransfer): bool
171
    {
172
        try {
173
            $quoteTransfer->requireCustomer();
174
            $quoteTransfer->getCustomer()->requireEmail();
175
            $quoteTransfer->requireTotals();
176
        } catch (RequiredTransferPropertyException $exception) {
177
            return true;
178
        }
179
180
        return false;
181
    }
182
183
    /**
184
     * @param \Generated\Shared\Transfer\HeidelpayDirectDebitRegistrationTransfer $directDebitRegistrationTransfer
185
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
186
     *
187
     * @return \Generated\Shared\Transfer\QuoteTransfer
188
     */
189
    protected function addDirectDebitRegistrationToQuote(
190
        HeidelpayDirectDebitRegistrationTransfer $directDebitRegistrationTransfer,
191
        QuoteTransfer $quoteTransfer
192
    ): QuoteTransfer {
193
        $paymentTransfer = $quoteTransfer->requirePayment()->getPayment();
194
        $paymentTransfer
195
            ->requireHeidelpayDirectDebit()
196
            ->getHeidelpayDirectDebit()
197
            ->setSelectedRegistration($directDebitRegistrationTransfer)
198
            ->setSelectedPaymentOption(HeidelpayConfig::PAYMENT_OPTION_NEW_REGISTRATION);
199
200
        $quoteTransfer->setPayment($paymentTransfer);
201
202
        return $quoteTransfer;
203
    }
204
205
    /**
206
     * @param \Symfony\Component\HttpFoundation\Request $request
207
     *
208
     * @return array
209
     */
210
    protected function getUrlDecodedRequestBody(Request $request): array
211
    {
212
        $allRequestParameters = $request->request->all();
213
214
        foreach ($allRequestParameters as $key => $value) {
215
            if (is_string($value)) {
216
                $allRequestParameters[$key] = urldecode($value);
217
            }
218
        }
219
220
        return $allRequestParameters;
221
    }
222
223
    /**
224
     * @param array $responseArray
225
     *
226
     * @return array
227
     */
228
    public function filterResponseParameters(array $responseArray): array
229
    {
230
        return array_filter($responseArray, function ($key) {
231
            return !preg_match('/^paymentForm+|^lang+/', $key);
232
        }, ARRAY_FILTER_USE_KEY);
233
    }
234
235
    /**
236
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
237
     *
238
     * @return string
239
     */
240
    protected function generateTransactionId(QuoteTransfer $quoteTransfer): string
241
    {
242
        return QuoteUniqueIdGenerator::getHashByCustomerEmailAndTotals($quoteTransfer);
243
    }
244
}
245