Passed
Pull Request — feature/eco-3623-payone-pay-u-... (#39)
by
unknown
08:17
created

ComputopPostSaveHook::isPaymentInitRequired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
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\PaymentTransfer;
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...
13
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...
14
use Spryker\Shared\Kernel\Transfer\TransferInterface;
15
use SprykerEco\Shared\Computop\ComputopConfig as ConputopSharedConfig;
16
use SprykerEco\Zed\Computop\Business\Exception\ComputopMethodMapperException;
17
use SprykerEco\Zed\Computop\Business\Exception\PaymentMethodNotFoundException;
18
use SprykerEco\Zed\Computop\Business\Hook\Mapper\Init\InitMapperInterface;
19
use SprykerEco\Zed\Computop\ComputopConfig;
20
21
class ComputopPostSaveHook implements ComputopPostSaveHookInterface
22
{
23
    /**
24
     * @var \SprykerEco\Zed\Computop\ComputopConfig
25
     */
26
    protected $config;
27
28
    /**
29
     * @var \SprykerEco\Zed\Computop\Business\Hook\Mapper\Init\InitMapperInterface[]
30
     */
31
    protected $methodMappers = [];
32
33
    /**
34
     * @param \SprykerEco\Zed\Computop\ComputopConfig $config
35
     */
36
    public function __construct(ComputopConfig $config)
37
    {
38
        $this->config = $config;
39
    }
40
41
    /**
42
     * @param \SprykerEco\Zed\Computop\Business\Hook\Mapper\Init\InitMapperInterface $paymentMethod
43
     *
44
     * @return void
45
     */
46
    public function registerMapper(InitMapperInterface $paymentMethod)
47
    {
48
        $this->methodMappers[$paymentMethod->getMethodName()] = $paymentMethod;
49
    }
50
51
    /**
52
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
53
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
54
     *
55
     * @return \Generated\Shared\Transfer\CheckoutResponseTransfer
56
     */
57
    public function execute(QuoteTransfer $quoteTransfer, CheckoutResponseTransfer $checkoutResponseTransfer)
58
    {
59
        $payment = $quoteTransfer->getPayment();
60
61
        if ($payment->getPaymentProvider() !== ConputopSharedConfig::PROVIDER_NAME) {
62
            return $checkoutResponseTransfer;
63
        }
64
65
        $quoteTransfer->setOrderReference($checkoutResponseTransfer->getSaveOrder()->getOrderReference());
66
        $computopPaymentTransfer = $this->getPaymentTransfer($quoteTransfer);
67
68
        if ($this->isPaymentInitRequired($payment)) {
69
            $checkoutResponseTransfer->setComputopInitPayment(
70
                (new ComputopInitPaymentTransfer())
71
                    ->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

71
                    ->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...
72
                    ->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

72
                    ->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...
73
            );
74
75
            return $checkoutResponseTransfer;
76
        }
77
78
        return $this->setRedirect($computopPaymentTransfer, $checkoutResponseTransfer);
79
    }
80
81
    /**
82
     * @param \Spryker\Shared\Kernel\Transfer\TransferInterface $computopPaymentTransfer
83
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
84
     *
85
     * @return \Generated\Shared\Transfer\CheckoutResponseTransfer
86
     */
87
    protected function setRedirect(TransferInterface $computopPaymentTransfer, CheckoutResponseTransfer $checkoutResponseTransfer)
88
    {
89
        $checkoutResponseTransfer
90
            ->setIsExternalRedirect(true)
91
            ->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

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