setComputopPaymentToQuote()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 13
rs 10
cc 3
nc 2
nop 3
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\Yves\Computop\Handler;
9
10
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...
11
use Spryker\Shared\Kernel\Transfer\AbstractTransfer;
12
use SprykerEco\Shared\Computop\ComputopConfig;
13
use SprykerEco\Yves\Computop\ComputopConfigInterface;
14
use SprykerEco\Yves\Computop\Exception\PaymentMethodNotFoundException;
15
16
class ComputopPaymentHandler implements ComputopPaymentHandlerInterface
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $paymentMethods = [];
22
23
    /**
24
     * @var \SprykerEco\Yves\Computop\ComputopConfigInterface
25
     */
26
    protected $config;
27
28
    /**
29
     * @param \SprykerEco\Yves\Computop\ComputopConfigInterface $config
30
     */
31
    public function __construct(ComputopConfigInterface $config)
32
    {
33
        $this->config = $config;
34
35
        $paymentMethods = $this->config->getPaymentMethodsWithoutOrderCall();
36
        $this->paymentMethods = array_combine($paymentMethods, $paymentMethods);
37
    }
38
39
    /**
40
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
41
     *
42
     * @return \Generated\Shared\Transfer\QuoteTransfer
43
     */
44
    public function addPaymentToQuote(QuoteTransfer $quoteTransfer)
45
    {
46
        $paymentSelection = $quoteTransfer->getPayment()->getPaymentSelection();
47
48
        $quoteTransfer = $this->setPaymentProviderMethodSelection($quoteTransfer, $paymentSelection);
49
        $quoteTransfer = $this->setComputopPayment($quoteTransfer, $paymentSelection);
50
51
        return $quoteTransfer;
52
    }
53
54
    /**
55
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
56
     * @param string $paymentSelection
57
     *
58
     * @return \Generated\Shared\Transfer\QuoteTransfer
59
     */
60
    protected function setPaymentProviderMethodSelection(QuoteTransfer $quoteTransfer, $paymentSelection)
61
    {
62
        $quoteTransfer
63
            ->getPayment()
64
            ->setPaymentProvider(ComputopConfig::PROVIDER_NAME)
65
            ->setPaymentMethod($this->paymentMethods[$paymentSelection])
66
            ->setPaymentSelection($this->paymentMethods[$paymentSelection]);
67
68
        return $quoteTransfer;
69
    }
70
71
    /**
72
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
73
     * @param string $paymentSelection
74
     *
75
     * @return \Generated\Shared\Transfer\QuoteTransfer
76
     */
77
    protected function setComputopPayment(QuoteTransfer $quoteTransfer, $paymentSelection)
78
    {
79
        $computopPaymentTransfer = $this->getComputopPaymentTransfer($quoteTransfer, $paymentSelection);
80
81
        return $this->setComputopPaymentToQuote($quoteTransfer, $paymentSelection, clone $computopPaymentTransfer);
82
    }
83
84
    /**
85
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
86
     * @param string $paymentSelection
87
     *
88
     * @throws \SprykerEco\Yves\Computop\Exception\PaymentMethodNotFoundException
89
     *
90
     * @return \Spryker\Shared\Kernel\Transfer\AbstractTransfer
91
     */
92
    protected function getComputopPaymentTransfer(QuoteTransfer $quoteTransfer, $paymentSelection)
93
    {
94
        $paymentMethod = ucfirst($paymentSelection);
95
        $method = 'get' . $paymentMethod;
96
        $paymentTransfer = $quoteTransfer->getPayment();
97
98
        if (!method_exists($paymentTransfer, $method) || ($quoteTransfer->getPayment()->$method() === null)) {
99
            throw new PaymentMethodNotFoundException(sprintf('Selected payment method "%s" not found in PaymentTransfer', $paymentMethod));
100
        }
101
        $computopPaymentTransfer = $quoteTransfer->getPayment()->$method();
102
103
        return $computopPaymentTransfer;
104
    }
105
106
    /**
107
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
108
     * @param string $paymentSelection
109
     * @param \Spryker\Shared\Kernel\Transfer\AbstractTransfer $computopPaymentTransfer
110
     *
111
     * @throws \SprykerEco\Yves\Computop\Exception\PaymentMethodNotFoundException
112
     *
113
     * @return \Generated\Shared\Transfer\QuoteTransfer
114
     */
115
    protected function setComputopPaymentToQuote(QuoteTransfer $quoteTransfer, $paymentSelection, AbstractTransfer $computopPaymentTransfer)
116
    {
117
        $paymentMethod = ucfirst($paymentSelection);
118
        $method = 'set' . $paymentMethod;
119
        $paymentTransfer = $quoteTransfer->getPayment();
120
121
        if (!method_exists($paymentTransfer, $method) || ($quoteTransfer->getPayment()->$method() === null)) {
122
            throw new PaymentMethodNotFoundException(sprintf('Selected payment method "%s" not found in PaymentTransfer', $paymentMethod));
123
        }
124
125
        $quoteTransfer->getPayment()->$method($computopPaymentTransfer);
126
127
        return $quoteTransfer;
128
    }
129
}
130