ComputopPostSaveHook::execute()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 12
c 4
b 0
f 0
dl 0
loc 20
rs 9.8666
cc 4
nc 2
nop 2
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\ComputopMethodMapperException;
16
use SprykerEco\Zed\Computop\Business\Exception\PaymentMethodNotFoundException;
17
use SprykerEco\Zed\Computop\Business\Hook\Mapper\Init\InitMapperInterface;
18
use SprykerEco\Zed\Computop\ComputopConfig;
19
20
class ComputopPostSaveHook implements ComputopPostSaveHookInterface
21
{
22
    /**
23
     * @var \SprykerEco\Zed\Computop\ComputopConfig
24
     */
25
    protected $config;
26
27
    /**
28
     * @var \SprykerEco\Zed\Computop\Business\Hook\Mapper\Init\InitMapperInterface[]
29
     */
30
    protected $methodMappers = [];
31
32
    /**
33
     * @param \SprykerEco\Zed\Computop\ComputopConfig $config
34
     */
35
    public function __construct(ComputopConfig $config)
36
    {
37
        $this->config = $config;
38
    }
39
40
    /**
41
     * @param \SprykerEco\Zed\Computop\Business\Hook\Mapper\Init\InitMapperInterface $paymentMethod
42
     *
43
     * @return void
44
     */
45
    public function registerMapper(InitMapperInterface $paymentMethod)
46
    {
47
        $this->methodMappers[$paymentMethod->getMethodName()] = $paymentMethod;
48
    }
49
50
    /**
51
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
52
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
53
     *
54
     * @return \Generated\Shared\Transfer\CheckoutResponseTransfer
55
     */
56
    public function execute(QuoteTransfer $quoteTransfer, CheckoutResponseTransfer $checkoutResponseTransfer)
57
    {
58
        $quoteTransfer->setOrderReference($checkoutResponseTransfer->getSaveOrder()->getOrderReference());
59
        $computopPaymentTransfer = $this->getPaymentTransfer($quoteTransfer);
60
61
        if (
62
            $quoteTransfer->getPayment()->getPaymentSelection() !== ConputopSharedConfig::PAYMENT_METHOD_PAY_NOW
63
            && $quoteTransfer->getPayment()->getPaymentSelection() !== ConputopSharedConfig::PAYMENT_METHOD_EASY_CREDIT
64
            && $quoteTransfer->getPayment()->getPaymentSelection() !== ConputopSharedConfig::PAYMENT_METHOD_CREDIT_CARD
65
        ) {
66
            return $this->setRedirect($computopPaymentTransfer, $checkoutResponseTransfer);
67
        }
68
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
    /**
79
     * @param \Spryker\Shared\Kernel\Transfer\TransferInterface $computopPaymentTransfer
80
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer
81
     *
82
     * @return \Generated\Shared\Transfer\CheckoutResponseTransfer
83
     */
84
    protected function setRedirect(TransferInterface $computopPaymentTransfer, CheckoutResponseTransfer $checkoutResponseTransfer)
85
    {
86
        $checkoutResponseTransfer
87
            ->setIsExternalRedirect(true)
88
            ->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

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