PostSaveHook   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 80
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A isPaymentAuthorizationSuccessful() 0 10 2
A setPaymentFailedRedirect() 0 7 1
A isAfterPayPaymentProvider() 0 3 1
A execute() 0 9 3
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\AfterPay\Business\Hook;
9
10
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...
11
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...
12
use SprykerEco\Shared\AfterPay\AfterPayConfig as SharedAfterPayConfig;
13
use SprykerEco\Zed\AfterPay\AfterPayConfig;
14
use SprykerEco\Zed\AfterPay\Business\Payment\Transaction\TransactionLogReaderInterface;
15
16
class PostSaveHook implements PostSaveHookInterface
17
{
18
    /**
19
     * @var \SprykerEco\Zed\AfterPay\Business\Payment\Transaction\TransactionLogReaderInterface
20
     */
21
    protected $transactionLogReader;
22
23
    /**
24
     * @var \SprykerEco\Zed\AfterPay\AfterPayConfig
25
     */
26
    protected $config;
27
28
    /**
29
     * @param \SprykerEco\Zed\AfterPay\Business\Payment\Transaction\TransactionLogReaderInterface $transactionLogReader
30
     * @param \SprykerEco\Zed\AfterPay\AfterPayConfig $config
31
     */
32
    public function __construct(
33
        TransactionLogReaderInterface $transactionLogReader,
34
        AfterPayConfig $config
35
    ) {
36
        $this->transactionLogReader = $transactionLogReader;
37
        $this->config = $config;
38
    }
39
40
    /**
41
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
42
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
43
     *
44
     * @return \Generated\Shared\Transfer\CheckoutResponseTransfer
45
     */
46
    public function execute(QuoteTransfer $quoteTransfer, CheckoutResponseTransfer $checkoutResponseTransfer): CheckoutResponseTransfer
47
    {
48
        if (!$this->isAfterPayPaymentProvider($quoteTransfer) || $this->isPaymentAuthorizationSuccessful($quoteTransfer->getOrderReference())) {
49
            return $checkoutResponseTransfer;
50
        }
51
52
        $this->setPaymentFailedRedirect($checkoutResponseTransfer);
53
54
        return $checkoutResponseTransfer;
55
    }
56
57
    /**
58
     * @param string $orderReference
59
     *
60
     * @return bool
61
     */
62
    protected function isPaymentAuthorizationSuccessful(string $orderReference): bool
63
    {
64
        $transactionLogTransfer = $this->transactionLogReader
65
            ->findOrderAuthorizeTransactionLogByIdSalesOrder($orderReference);
66
67
        if (!$transactionLogTransfer) {
68
            return false;
69
        }
70
71
        return $transactionLogTransfer->getOutcome() === SharedAfterPayConfig::API_TRANSACTION_OUTCOME_ACCEPTED;
72
    }
73
74
    /**
75
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
76
     *
77
     * @return void
78
     */
79
    protected function setPaymentFailedRedirect(CheckoutResponseTransfer $checkoutResponseTransfer): void
80
    {
81
        $paymentFailedUrl = $this->config->getPaymentAuthorizationFailedUrl();
82
83
        $checkoutResponseTransfer
84
            ->setIsExternalRedirect(true)
85
            ->setRedirectUrl($paymentFailedUrl);
86
    }
87
88
    /**
89
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
90
     *
91
     * @return bool
92
     */
93
    protected function isAfterPayPaymentProvider(QuoteTransfer $quoteTransfer): bool
94
    {
95
        return $quoteTransfer->getPayment()->getPaymentProvider() === SharedAfterPayConfig::PROVIDER_NAME;
96
    }
97
}
98