Passed
Push — feature/eco-3653-handle-callba... ( c462d3...7e8a07 )
by
unknown
05:33
created

ComputopPostSaveHook::getMethodMapper()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 3
c 1
b 1
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\Computop\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\ComputopInitPaymentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...utopInitPaymentTransfer 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;
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...
13
use Spryker\Shared\Kernel\Transfer\TransferInterface;
14
use SprykerEco\Shared\Computop\ComputopConfig as ConputopSharedConfig;
15
use SprykerEco\Zed\Computop\Business\Exception\PaymentMethodNotFoundException;
16
use SprykerEco\Zed\Computop\Business\Hook\Mapper\Init\InitMapperInterface;
17
use SprykerEco\Zed\Computop\ComputopConfig;
18
19
class ComputopPostSaveHook implements ComputopPostSaveHookInterface
20
{
21
    /**
22
     * @var \SprykerEco\Zed\Computop\ComputopConfig
23
     */
24
    protected $config;
25
26
    /**
27
     * @var \SprykerEco\Zed\Computop\Business\Hook\Mapper\Init\InitMapperInterface[]
28
     */
29
    protected $methodMappers = [];
30
31
    /**
32
     * @param \SprykerEco\Zed\Computop\ComputopConfig $config
33
     */
34
    public function __construct(ComputopConfig $config)
35
    {
36
        $this->config = $config;
37
    }
38
39
    /**
40
     * @param \SprykerEco\Zed\Computop\Business\Hook\Mapper\Init\InitMapperInterface $paymentMethod
41
     *
42
     * @return void
43
     */
44
    public function registerMapper(InitMapperInterface $paymentMethod)
45
    {
46
        $this->methodMappers[$paymentMethod->getMethodName()] = $paymentMethod;
47
    }
48
49
    /**
50
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
51
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
52
     *
53
     * @return \Generated\Shared\Transfer\CheckoutResponseTransfer
54
     */
55
    public function execute(QuoteTransfer $quoteTransfer, CheckoutResponseTransfer $checkoutResponseTransfer)
56
    {
57
        $payment = $quoteTransfer->getPayment();
58
59
        if ($payment->getPaymentProvider() !== ConputopSharedConfig::PROVIDER_NAME) {
60
            return $checkoutResponseTransfer;
61
        }
62
63
        $quoteTransfer->setOrderReference($checkoutResponseTransfer->getSaveOrder()->getOrderReference());
64
        $computopPaymentTransfer = $this->getPaymentTransfer($quoteTransfer);
65
66
        if ($this->isPaymentInitRequired($payment)) {
67
            $checkoutResponseTransfer->setComputopInitPayment(
68
                (new ComputopInitPaymentTransfer())
69
                    ->setData($computopPaymentTransfer->getData())
0 ignored issues
show
Bug introduced by
The method getData() does not exist on Spryker\Shared\Kernel\Transfer\TransferInterface. ( Ignorable by Annotation )

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

69
                    ->setData($computopPaymentTransfer->/** @scrutinizer ignore-call */ getData())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
70
                    ->setLen($computopPaymentTransfer->getLen())
0 ignored issues
show
Bug introduced by
The method getLen() does not exist on Spryker\Shared\Kernel\Transfer\TransferInterface. ( Ignorable by Annotation )

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

70
                    ->setLen($computopPaymentTransfer->/** @scrutinizer ignore-call */ getLen())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
            );
72
73
            return $checkoutResponseTransfer;
74
        }
75
76
        return $this->setRedirect($computopPaymentTransfer, $checkoutResponseTransfer);
77
    }
78
79
    /**
80
     * @param \Spryker\Shared\Kernel\Transfer\TransferInterface $computopPaymentTransfer
81
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
82
     *
83
     * @return \Generated\Shared\Transfer\CheckoutResponseTransfer
84
     */
85
    protected function setRedirect(TransferInterface $computopPaymentTransfer, CheckoutResponseTransfer $checkoutResponseTransfer)
86
    {
87
        $checkoutResponseTransfer
88
            ->setIsExternalRedirect(true)
89
            ->setRedirectUrl($computopPaymentTransfer->getUrl());
0 ignored issues
show
Bug introduced by
The method getUrl() does not exist on Spryker\Shared\Kernel\Transfer\TransferInterface. ( Ignorable by Annotation )

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

89
            ->setRedirectUrl($computopPaymentTransfer->/** @scrutinizer ignore-call */ getUrl());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90
91
        return $checkoutResponseTransfer;
92
    }
93
94
    /**
95
     * @param string $methodName
96
     *
97
     * @return \SprykerEco\Zed\Computop\Business\Hook\Mapper\Init\InitMapperInterface|null
98
     */
99
    protected function getMethodMapper($methodName)
100
    {
101
        if (isset($this->methodMappers[$methodName]) === false) {
102
            return null;
103
        }
104
105
        return $this->methodMappers[$methodName];
106
    }
107
108
    /**
109
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
110
     *
111
     * @throws \SprykerEco\Zed\Computop\Business\Exception\PaymentMethodNotFoundException
112
     *
113
     * @return \Spryker\Shared\Kernel\Transfer\TransferInterface
114
     */
115
    protected function getPaymentTransfer(QuoteTransfer $quoteTransfer)
116
    {
117
        $paymentSelection = $quoteTransfer->getPayment()->getPaymentSelection();
118
119
        $paymentMethod = ucfirst($paymentSelection);
120
        $method = 'get' . $paymentMethod;
121
        $paymentTransfer = $quoteTransfer->getPayment();
122
123
        if (!method_exists($paymentTransfer, $method) || ($quoteTransfer->getPayment()->$method() === null)) {
124
            throw new PaymentMethodNotFoundException(sprintf('Selected payment method "%s" not found in PaymentTransfer', $paymentMethod));
125
        }
126
127
        $computopPaymentTransfer = $quoteTransfer->getPayment()->$method();
128
        $methodMapper = $this->getMethodMapper($paymentSelection);
129
        if (!$methodMapper) {
130
            return $computopPaymentTransfer;
131
        }
132
133
        return $methodMapper->updateComputopPaymentTransfer($quoteTransfer, $computopPaymentTransfer);
134
    }
135
136
    /**
137
     * @param \Generated\Shared\Transfer\PaymentTransfer $payment
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PaymentTransfer 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...
138
     *
139
     * @return bool
140
     */
141
    protected function isPaymentInitRequired(PaymentTransfer $payment): bool
0 ignored issues
show
Bug introduced by
The type SprykerEco\Zed\Computop\...ss\Hook\PaymentTransfer 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...
142
    {
143
        return in_array($payment->getPaymentSelection(), [
144
            ConputopSharedConfig::PAYMENT_METHOD_PAY_NOW,
145
            ConputopSharedConfig::PAYMENT_METHOD_EASY_CREDIT,
146
            ConputopSharedConfig::PAYMENT_METHOD_CREDIT_CARD,
147
        ]);
148
    }
149
}
150