DirectDebitDataProvider::setDefaultPaymentOption()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
dl 0
loc 7
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * MIT License
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace SprykerEco\Yves\Heidelpay\Form\DataProvider;
9
10
use Generated\Shared\Transfer\HeidelpayDirectDebitPaymentTransfer;
11
use Generated\Shared\Transfer\PaymentTransfer;
12
use Generated\Shared\Transfer\QuoteTransfer;
13
use Spryker\Shared\Kernel\Transfer\AbstractTransfer;
14
use Spryker\Yves\StepEngine\Dependency\Form\StepEngineFormDataProviderInterface;
15
use SprykerEco\Yves\Heidelpay\Form\DirectDebitSubForm;
16
17
class DirectDebitDataProvider implements StepEngineFormDataProviderInterface
18
{
19
    protected const PAYMENT_OPTION_NAME_TRANSLATION_PATTERN = 'heidelpay.payment.direct_debit.%s';
20
21
    /**
22
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
23
     *
24
     * @return \Generated\Shared\Transfer\QuoteTransfer
25
     */
26
    public function getData(AbstractTransfer $quoteTransfer): QuoteTransfer
27
    {
28
        if ($quoteTransfer->getPayment() === null) {
0 ignored issues
show
Bug introduced by
The method getPayment() does not exist on Spryker\Shared\Kernel\Transfer\AbstractTransfer. ( Ignorable by Annotation )

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

28
        if ($quoteTransfer->/** @scrutinizer ignore-call */ getPayment() === null) {

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...
29
            $quoteTransfer->setPayment(new PaymentTransfer());
0 ignored issues
show
Bug introduced by
The method setPayment() does not exist on Spryker\Shared\Kernel\Transfer\AbstractTransfer. ( Ignorable by Annotation )

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

29
            $quoteTransfer->/** @scrutinizer ignore-call */ 
30
                            setPayment(new PaymentTransfer());

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...
30
        }
31
32
        if ($quoteTransfer->getPayment()->getHeidelpayDirectDebit() !== null) {
33
            return $quoteTransfer;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $quoteTransfer returns the type Spryker\Shared\Kernel\Transfer\AbstractTransfer which is incompatible with the type-hinted return Generated\Shared\Transfer\QuoteTransfer.
Loading history...
34
        }
35
36
        $quoteTransfer->getPayment()->setHeidelpayDirectDebit(new HeidelpayDirectDebitPaymentTransfer());
37
38
        return $quoteTransfer;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $quoteTransfer returns the type Spryker\Shared\Kernel\Transfer\AbstractTransfer which is incompatible with the type-hinted return Generated\Shared\Transfer\QuoteTransfer.
Loading history...
39
    }
40
41
    /**
42
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
43
     *
44
     * @return array
45
     */
46
    public function getOptions(AbstractTransfer $quoteTransfer): array
47
    {
48
        $directDebitPayment = $this->selectPaymentOption(
49
            $quoteTransfer->getPayment()->getHeidelpayDirectDebit()
50
        );
51
52
        $quoteTransfer->getPayment()->setHeidelpayDirectDebit($directDebitPayment);
53
54
        return [
55
            DirectDebitSubForm::DIRECT_DEBIT_PAYMENT_OPTIONS => $this->fetchPaymentOptions($directDebitPayment),
56
        ];
57
    }
58
59
    /**
60
     * @param \Generated\Shared\Transfer\HeidelpayDirectDebitPaymentTransfer $directDebitPaymentTransfer
61
     *
62
     * @return \Generated\Shared\Transfer\HeidelpayDirectDebitPaymentTransfer
63
     */
64
    protected function selectPaymentOption(HeidelpayDirectDebitPaymentTransfer $directDebitPaymentTransfer): HeidelpayDirectDebitPaymentTransfer
65
    {
66
        $directDebitPaymentTransfer = $this->unsetNotAvailableSelection($directDebitPaymentTransfer);
67
68
        if ($directDebitPaymentTransfer->getSelectedPaymentOption() !== null) {
69
            return $directDebitPaymentTransfer;
70
        }
71
72
        $directDebitPaymentTransfer = $this->setDefaultPaymentOption($directDebitPaymentTransfer);
73
74
        return $directDebitPaymentTransfer;
75
    }
76
77
    /**
78
     * @param \Generated\Shared\Transfer\HeidelpayDirectDebitPaymentTransfer $directDebitPaymentTransfer
79
     *
80
     * @return \Generated\Shared\Transfer\HeidelpayDirectDebitPaymentTransfer
81
     */
82
    protected function unsetNotAvailableSelection(HeidelpayDirectDebitPaymentTransfer $directDebitPaymentTransfer): HeidelpayDirectDebitPaymentTransfer
83
    {
84
        $selectedPaymentOption = $directDebitPaymentTransfer->getSelectedPaymentOption();
85
86
        if ($selectedPaymentOption === null) {
87
            return $directDebitPaymentTransfer;
88
        }
89
90
        $availableOptions = $this->fetchPaymentOptions($directDebitPaymentTransfer);
91
92
        if (!isset($availableOptions[$selectedPaymentOption])) {
93
            $directDebitPaymentTransfer->setSelectedPaymentOption(null);
94
        }
95
96
        return $directDebitPaymentTransfer;
97
    }
98
99
    /**
100
     * @param \Generated\Shared\Transfer\HeidelpayDirectDebitPaymentTransfer $directDebitPaymentTransfer
101
     *
102
     * @return \Generated\Shared\Transfer\HeidelpayDirectDebitPaymentTransfer
103
     */
104
    protected function setDefaultPaymentOption(HeidelpayDirectDebitPaymentTransfer $directDebitPaymentTransfer): HeidelpayDirectDebitPaymentTransfer
105
    {
106
        $options = $this->fetchPaymentOptions($directDebitPaymentTransfer);
107
        $defaultOption = array_shift($options);
108
        $directDebitPaymentTransfer->setSelectedPaymentOption($defaultOption);
109
110
        return $directDebitPaymentTransfer;
111
    }
112
113
    /**
114
     * @param \Generated\Shared\Transfer\HeidelpayDirectDebitPaymentTransfer $directDebitPaymentTransfer
115
     *
116
     * @return string[]
117
     */
118
    protected function fetchPaymentOptions(HeidelpayDirectDebitPaymentTransfer $directDebitPaymentTransfer): array
119
    {
120
        $paymentOptions = [];
121
122
        $paymentOptionsList = $directDebitPaymentTransfer
123
            ->getPaymentOptions()
124
            ->getOptionsList();
125
126
        foreach ($paymentOptionsList as $optionTransfer) {
127
            $paymentOptions[$optionTransfer->getCode()] = sprintf(static::PAYMENT_OPTION_NAME_TRANSLATION_PATTERN, $optionTransfer->getCode());
128
        }
129
130
        return $paymentOptions;
131
    }
132
}
133