CrefoPayCheckoutPostSaveHook::execute()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 14
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 26
rs 9.7998
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\Zed\CrefoPay\Business\Hook\Checkout;
9
10
use Generated\Shared\Transfer\CheckoutErrorTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\CheckoutErrorTransfer 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\CheckoutResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\CheckoutResponseTransfer 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\CrefoPayApiRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...foPayApiRequestTransfer 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\CrefoPayApiResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...oPayApiResponseTransfer 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 SprykerEco\Shared\CrefoPay\CrefoPayConfig;
16
use SprykerEco\Zed\CrefoPay\Business\Hook\Checkout\Mapper\CrefoPayCheckoutHookMapperInterface;
17
use SprykerEco\Zed\CrefoPay\Business\Hook\Checkout\Saver\CrefoPayCheckoutHookSaverInterface;
18
use SprykerEco\Zed\CrefoPay\Dependency\Facade\CrefoPayToCrefoPayApiFacadeInterface;
19
20
class CrefoPayCheckoutPostSaveHook implements CrefoPayCheckoutHookInterface
21
{
22
    /**
23
     * @var string
24
     */
25
    protected const ERROR_TYPE_PAYMENT_FAILED = 'payment failed';
26
27
    /**
28
     * @var string
29
     */
30
    protected const ERROR_MESSAGE_PAYMENT_FAILED = 'Something went wrong with your payment. Try again!';
31
32
    /**
33
     * @var \SprykerEco\Zed\CrefoPay\Dependency\Facade\CrefoPayToCrefoPayApiFacadeInterface
34
     */
35
    protected $crefoPayApiFacade;
36
37
    /**
38
     * @var \SprykerEco\Zed\CrefoPay\Business\Hook\Checkout\Mapper\CrefoPayCheckoutHookMapperInterface
39
     */
40
    protected $mapper;
41
42
    /**
43
     * @var \SprykerEco\Zed\CrefoPay\Business\Hook\Checkout\Saver\CrefoPayCheckoutHookSaverInterface
44
     */
45
    protected $saver;
46
47
    /**
48
     * @param \SprykerEco\Zed\CrefoPay\Dependency\Facade\CrefoPayToCrefoPayApiFacadeInterface $crefoPayApiFacade
49
     * @param \SprykerEco\Zed\CrefoPay\Business\Hook\Checkout\Mapper\CrefoPayCheckoutHookMapperInterface $mapper
50
     * @param \SprykerEco\Zed\CrefoPay\Business\Hook\Checkout\Saver\CrefoPayCheckoutHookSaverInterface $saver
51
     */
52
    public function __construct(
53
        CrefoPayToCrefoPayApiFacadeInterface $crefoPayApiFacade,
54
        CrefoPayCheckoutHookMapperInterface $mapper,
55
        CrefoPayCheckoutHookSaverInterface $saver
56
    ) {
57
        $this->crefoPayApiFacade = $crefoPayApiFacade;
58
        $this->mapper = $mapper;
59
        $this->saver = $saver;
60
    }
61
62
    /**
63
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
64
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
65
     *
66
     * @return void
67
     */
68
    public function execute(QuoteTransfer $quoteTransfer, CheckoutResponseTransfer $checkoutResponseTransfer): void
69
    {
70
        if ($quoteTransfer->getPayment()->getPaymentProvider() !== CrefoPayConfig::PROVIDER_NAME) {
71
            return;
72
        }
73
74
        $requestTransfer = $this->mapper
75
            ->mapQuoteTransferToRequestTransfer(
76
                $quoteTransfer,
77
                new CrefoPayApiRequestTransfer(),
78
            );
79
80
        $responseTransfer = $this->crefoPayApiFacade->performReserveApiCall($requestTransfer);
81
        $this->saver->savePaymentEntities($requestTransfer, $responseTransfer);
82
83
        if (!$responseTransfer->getIsSuccess()) {
84
            $this->processFailureResponse($checkoutResponseTransfer);
85
86
            return;
87
        }
88
89
        if (!$this->isMethodWithRedirect($responseTransfer)) {
90
            return;
91
        }
92
93
        $this->setRedirectToPaymentSystem($checkoutResponseTransfer, $responseTransfer);
94
    }
95
96
    /**
97
     * @param \Generated\Shared\Transfer\CrefoPayApiResponseTransfer $responseTransfer
98
     *
99
     * @return bool
100
     */
101
    protected function isMethodWithRedirect(CrefoPayApiResponseTransfer $responseTransfer): bool
102
    {
103
        return (bool)$responseTransfer->getReserveResponse()->getRedirectUrl();
104
    }
105
106
    /**
107
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
108
     * @param \Generated\Shared\Transfer\CrefoPayApiResponseTransfer $responseTransfer
109
     *
110
     * @return void
111
     */
112
    protected function setRedirectToPaymentSystem(
113
        CheckoutResponseTransfer $checkoutResponseTransfer,
114
        CrefoPayApiResponseTransfer $responseTransfer
115
    ): void {
116
        $checkoutResponseTransfer->setIsExternalRedirect(true);
117
        $checkoutResponseTransfer->setRedirectUrl(
118
            $responseTransfer->getReserveResponse()->getRedirectUrl(),
119
        );
120
    }
121
122
    /**
123
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
124
     *
125
     * @return void
126
     */
127
    protected function processFailureResponse(
128
        CheckoutResponseTransfer $checkoutResponseTransfer
129
    ): void {
130
        $error = (new CheckoutErrorTransfer())
131
            ->setErrorType(static::ERROR_TYPE_PAYMENT_FAILED)
132
            ->setMessage(static::ERROR_MESSAGE_PAYMENT_FAILED);
133
134
        $checkoutResponseTransfer->setIsSuccess(false);
135
        $checkoutResponseTransfer->addError($error);
136
    }
137
}
138