Passed
Pull Request — master (#37)
by Roman
06:52 queued 03:17
created

ComputopPostSaveHook::getPaymentTransfer()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 9
c 1
b 1
f 0
dl 0
loc 16
rs 9.9666
cc 3
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\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
        $paymentTransfer = $quoteTransfer->getPayment();
60
        if (!$paymentTransfer || $paymentTransfer->getPaymentProvider() !== ConputopSharedConfig::PROVIDER_NAME) {
61
            return $checkoutResponseTransfer;
62
        }
63
64
        $quoteTransfer->setOrderReference($checkoutResponseTransfer->getSaveOrder()->getOrderReference());
65
        $computopPaymentTransfer = $this->getPaymentTransfer($quoteTransfer);
66
67
        if ($this->isPaymentInitRequired($paymentTransfer)) {
68
            $checkoutResponseTransfer->setComputopInitPayment(
69
                (new ComputopInitPaymentTransfer())
70
                    ->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

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

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

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