Completed
Push — master ( 56a686...736967 )
by Oleksandr
15s queued 11s
created

redirectToRegistrationFailedAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
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\Controller;
9
10
use Generated\Shared\Transfer\HeidelpayCreditCardRegistrationTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfe...ardRegistrationTransfer 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\HeidelpayRegistrationRequestTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfe...strationRequestTransfer 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\HeidelpayRegistrationSaveResponseTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfe...ionSaveResponseTransfer 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\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...
14
use Spryker\Shared\Kernel\Transfer\Exception\RequiredTransferPropertyException;
15
use Symfony\Component\HttpFoundation\RedirectResponse;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
19
/**
20
 * @method \SprykerEco\Yves\Heidelpay\HeidelpayFactory getFactory()
21
 * @method \SprykerEco\Client\Heidelpay\HeidelpayClientInterface getClient()
22
 */
23
class CreditCardController extends BaseHeidelpayController
24
{
25
    public const REQUEST_PARAM_REGISTRATION_ID = 'id_registration';
26
    public const ERROR_CODE_REGISTRATION_NOT_FOUND = 'registration_not_found';
27
    public const ERROR_CODE_QUOTE_EXPIRED = 'quote_expired';
28
29
    /**
30
     * @param \Symfony\Component\HttpFoundation\Request $request
31
     *
32
     * @return \Symfony\Component\HttpFoundation\Response
33
     */
34
    public function registrationRequestAction(Request $request): Response
35
    {
36
        $apiResponseAsArray = $this->getUrldecodedRequestBody($request);
37
38
        $registrationRequestTransfer = $this->getValidatedRegistrationRequest(
39
            $this->getClient()->filterResponseParameters($apiResponseAsArray)
40
        );
41
42
        if ($registrationRequestTransfer->getIsError()) {
43
            return $this->getInvalidApiRequestActionUrl($registrationRequestTransfer);
44
        }
45
46
        $savingResultTransfer = $this->saveCreditCardRegistration($registrationRequestTransfer);
47
48
        return $this->getRegistrationSuccessActionUrl($savingResultTransfer);
49
    }
50
51
    /**
52
     * @param \Symfony\Component\HttpFoundation\Request $request
53
     *
54
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|string
55
     */
56
    public function registrationSuccessAction(Request $request)
57
    {
58
        $idRegistration = $request->get(static::REQUEST_PARAM_REGISTRATION_ID);
59
        $quoteTransfer = $this->getClient()->getQuoteFromSession();
1 ignored issue
show
Deprecated Code introduced by
The function SprykerEco\Client\Heidel...::getQuoteFromSession() has been deprecated: Use `getQuote()` instead. ( Ignorable by Annotation )

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

59
        $quoteTransfer = /** @scrutinizer ignore-deprecated */ $this->getClient()->getQuoteFromSession();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
60
61
        if ($this->isQuoteExpired($quoteTransfer)) {
62
            return $this->redirectToPaymentStepWithError(static::ERROR_CODE_QUOTE_EXPIRED);
63
        }
64
65
        $creditCardRegistration = $this->findRegistrationByIdAndQuote($idRegistration, $quoteTransfer);
0 ignored issues
show
Bug introduced by
It seems like $idRegistration can also be of type null; however, parameter $idRegistration of SprykerEco\Yves\Heidelpa...istrationByIdAndQuote() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

65
        $creditCardRegistration = $this->findRegistrationByIdAndQuote(/** @scrutinizer ignore-type */ $idRegistration, $quoteTransfer);
Loading history...
66
67
        if ($creditCardRegistration->getIdCreditCardRegistration() !== null) {
68
            $this->hydrateCreditCardRegistrationToQuote($creditCardRegistration, $quoteTransfer);
0 ignored issues
show
Bug introduced by
It seems like $creditCardRegistration can also be of type null; however, parameter $registrationTransfer of SprykerEco\Yves\Heidelpa...rdRegistrationToQuote() does only seem to accept Generated\Shared\Transfe...ardRegistrationTransfer, maybe add an additional type check? ( Ignorable by Annotation )

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

68
            $this->hydrateCreditCardRegistrationToQuote(/** @scrutinizer ignore-type */ $creditCardRegistration, $quoteTransfer);
Loading history...
69
70
            return $this->redirectToSummaryStep();
71
        }
72
73
        return $this->redirectToPaymentStepWithError(static::ERROR_CODE_REGISTRATION_NOT_FOUND);
74
    }
75
76
    /**
77
     * @param array $requestAsArray
78
     *
79
     * @return \Generated\Shared\Transfer\HeidelpayRegistrationRequestTransfer
80
     */
81
    protected function getValidatedRegistrationRequest(array $requestAsArray): HeidelpayRegistrationRequestTransfer
82
    {
83
        return $this->getClient()->parseExternalResponse($requestAsArray);
84
    }
85
86
    /**
87
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
88
     *
89
     * @return bool
90
     */
91
    protected function isQuoteExpired(QuoteTransfer $quoteTransfer): bool
92
    {
93
        try {
94
            $quoteTransfer->requireCustomer();
95
            $quoteTransfer->getCustomer()->requireEmail();
96
            $quoteTransfer->requireTotals();
97
        } catch (RequiredTransferPropertyException $exception) {
98
            return true;
99
        }
100
101
        return false;
102
    }
103
104
    /**
105
     * @param \Generated\Shared\Transfer\HeidelpayRegistrationRequestTransfer $registrationRequestTransfer
106
     *
107
     * @return \Generated\Shared\Transfer\HeidelpayRegistrationSaveResponseTransfer
108
     */
109
    protected function saveCreditCardRegistration(HeidelpayRegistrationRequestTransfer $registrationRequestTransfer): HeidelpayRegistrationSaveResponseTransfer
110
    {
111
        return $this
112
            ->getClient()
113
            ->saveCreditCardRegistration($registrationRequestTransfer);
114
    }
115
116
    /**
117
     * @param int $idRegistration
118
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
119
     *
120
     * @return \Generated\Shared\Transfer\HeidelpayCreditCardRegistrationTransfer|null
121
     */
122
    protected function findRegistrationByIdAndQuote(int $idRegistration, QuoteTransfer $quoteTransfer): ?HeidelpayCreditCardRegistrationTransfer
123
    {
124
        return $this
125
            ->getClient()
126
            ->findRegistrationByIdAndQuote($idRegistration, $quoteTransfer);
127
    }
128
129
    /**
130
     * @param \Generated\Shared\Transfer\HeidelpayCreditCardRegistrationTransfer $registrationTransfer
131
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
132
     *
133
     * @return void
134
     */
135
    protected function hydrateCreditCardRegistrationToQuote(
136
        HeidelpayCreditCardRegistrationTransfer $registrationTransfer,
137
        QuoteTransfer $quoteTransfer
138
    ): void {
139
        $this->getFactory()
140
            ->createCreditCardRegistrationToQuoteHydrator()
141
            ->hydrateCreditCardRegistrationToQuote($registrationTransfer, $quoteTransfer);
142
    }
143
144
    /**
145
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
146
     */
147
    protected function redirectToSummaryStep(): RedirectResponse
148
    {
149
        $summaryStepUrl = $this->getConfig()->getYvesCheckoutSummaryStepUrl();
150
151
        return new RedirectResponse($summaryStepUrl);
152
    }
153
154
    /**
155
     * @param string $errorCode
156
     *
157
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
158
     */
159
    protected function redirectToPaymentStepWithError(string $errorCode): RedirectResponse
160
    {
161
        $paymentFailedUrl = sprintf(
162
163
            $this->getConfig()->getYvesCheckoutPaymentFailedUrl(),
164
            $errorCode
165
        );
166
167
        return new RedirectResponse($paymentFailedUrl);
168
    }
169
170
    /**
171
     * @param \Generated\Shared\Transfer\HeidelpayRegistrationRequestTransfer $registrationRequestTransfer
172
     *
173
     * @return \Symfony\Component\HttpFoundation\Response
174
     */
175
    protected function getInvalidApiRequestActionUrl(
176
        HeidelpayRegistrationRequestTransfer $registrationRequestTransfer
177
    ): Response {
178
        $redirectUrl = sprintf(
179
            $this->getConfig()->getYvesCheckoutPaymentFailedUrl(),
180
            $registrationRequestTransfer->getError()->getCode()
181
        );
182
183
        return $this->streamRedirectResponse($redirectUrl);
184
    }
185
186
    /**
187
     * @param \Generated\Shared\Transfer\HeidelpayRegistrationSaveResponseTransfer $saveResponseTransfer
188
     *
189
     * @return \Symfony\Component\HttpFoundation\Response
190
     */
191
    protected function getRegistrationSuccessActionUrl(
192
        HeidelpayRegistrationSaveResponseTransfer $saveResponseTransfer
193
    ): Response {
194
        $redirectUrl = sprintf(
195
            $this->getConfig()->getYvesRegistrationSuccessUrl(),
196
            $saveResponseTransfer->getIdRegistration()
197
        );
198
199
        return $this->streamRedirectResponse($redirectUrl);
200
    }
201
202
    /**
203
     * @param string $redirectUrl
204
     *
205
     * @return \Symfony\Component\HttpFoundation\Response
206
     */
207
    protected function streamRedirectResponse(string $redirectUrl): Response
208
    {
209
        $callback = function () use ($redirectUrl) {
210
            echo $redirectUrl;
211
        };
212
213
        return $this->streamedResponse($callback)->send();
214
    }
215
}
216