Passed
Pull Request — master (#4)
by Aleksey
07:29 queued 04:02
created

CrefoPayCheckoutPostSaveHook   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
c 1
b 0
f 0
dl 0
loc 109
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A setRedirectToPaymentSystem() 0 7 1
A execute() 0 26 4
A isMethodWithRedirect() 0 3 1
A processFailureResponse() 0 9 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\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
    protected const ERROR_TYPE_PAYMENT_FAILED = 'payment failed';
23
    protected const ERROR_MESSAGE_PAYMENT_FAILED = 'Something went wrong with your payment. Try again!';
24
25
    /**
26
     * @var \SprykerEco\Zed\CrefoPay\Dependency\Facade\CrefoPayToCrefoPayApiFacadeInterface
27
     */
28
    protected $crefoPayApiFacade;
29
30
    /**
31
     * @var \SprykerEco\Zed\CrefoPay\Business\Hook\Checkout\Mapper\CrefoPayCheckoutHookMapperInterface
32
     */
33
    protected $mapper;
34
35
    /**
36
     * @var \SprykerEco\Zed\CrefoPay\Business\Hook\Checkout\Saver\CrefoPayCheckoutHookSaverInterface
37
     */
38
    protected $saver;
39
40
    /**
41
     * @param \SprykerEco\Zed\CrefoPay\Dependency\Facade\CrefoPayToCrefoPayApiFacadeInterface $crefoPayApiFacade
42
     * @param \SprykerEco\Zed\CrefoPay\Business\Hook\Checkout\Mapper\CrefoPayCheckoutHookMapperInterface $mapper
43
     * @param \SprykerEco\Zed\CrefoPay\Business\Hook\Checkout\Saver\CrefoPayCheckoutHookSaverInterface $saver
44
     */
45
    public function __construct(
46
        CrefoPayToCrefoPayApiFacadeInterface $crefoPayApiFacade,
47
        CrefoPayCheckoutHookMapperInterface $mapper,
48
        CrefoPayCheckoutHookSaverInterface $saver
49
    ) {
50
        $this->crefoPayApiFacade = $crefoPayApiFacade;
51
        $this->mapper = $mapper;
52
        $this->saver = $saver;
53
    }
54
55
    /**
56
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
57
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
58
     *
59
     * @return void
60
     */
61
    public function execute(QuoteTransfer $quoteTransfer, CheckoutResponseTransfer $checkoutResponseTransfer): void
62
    {
63
        if ($quoteTransfer->getPayment()->getPaymentProvider() !== CrefoPayConfig::PROVIDER_NAME) {
64
            return;
65
        }
66
67
        $requestTransfer = $this->mapper
68
            ->mapQuoteTransferToRequestTransfer(
69
                $quoteTransfer,
70
                new CrefoPayApiRequestTransfer()
71
            );
72
73
        $responseTransfer = $this->crefoPayApiFacade->performReserveApiCall($requestTransfer);
74
        $this->saver->savePaymentEntities($requestTransfer, $responseTransfer);
75
76
        if (!$responseTransfer->getIsSuccess()) {
77
            $this->processFailureResponse($checkoutResponseTransfer);
78
79
            return;
80
        }
81
82
        if (!$this->isMethodWithRedirect($responseTransfer)) {
83
            return;
84
        }
85
86
        $this->setRedirectToPaymentSystem($checkoutResponseTransfer, $responseTransfer);
87
    }
88
89
    /**
90
     * @param \Generated\Shared\Transfer\CrefoPayApiResponseTransfer $responseTransfer
91
     *
92
     * @return bool
93
     */
94
    protected function isMethodWithRedirect(CrefoPayApiResponseTransfer $responseTransfer): bool
95
    {
96
        return !empty($responseTransfer->getReserveResponse()->getRedirectUrl());
97
    }
98
99
    /**
100
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
101
     * @param \Generated\Shared\Transfer\CrefoPayApiResponseTransfer $responseTransfer
102
     *
103
     * @return void
104
     */
105
    protected function setRedirectToPaymentSystem(
106
        CheckoutResponseTransfer $checkoutResponseTransfer,
107
        CrefoPayApiResponseTransfer $responseTransfer
108
    ): void {
109
        $checkoutResponseTransfer->setIsExternalRedirect(true);
110
        $checkoutResponseTransfer->setRedirectUrl(
111
            $responseTransfer->getReserveResponse()->getRedirectUrl()
112
        );
113
    }
114
115
    /**
116
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
117
     *
118
     * @return void
119
     */
120
    protected function processFailureResponse(
121
        CheckoutResponseTransfer $checkoutResponseTransfer
122
    ): void {
123
        $error = (new CheckoutErrorTransfer())
124
            ->setErrorType(static::ERROR_TYPE_PAYMENT_FAILED)
125
            ->setMessage(static::ERROR_MESSAGE_PAYMENT_FAILED);
126
127
        $checkoutResponseTransfer->setIsSuccess(false);
128
        $checkoutResponseTransfer->addError($error);
129
    }
130
}
131