Form::saveToModel()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 10
cc 4
nc 3
nop 0
crap 20
1
<?php
2
3
namespace ByTIC\Payments\Gateways\Providers\AbstractGateway;
4
5
use ByTIC\Payments\Forms\Traits\PaymentMethodFormTrait;
6
use ByTIC\Payments\Gateways\Providers\AbstractGateway\Traits\GatewayTrait;
7
use Nip\Form\AbstractForm;
8
use Nip\Form\Traits\MagicMethodElementsFormTrait;
9
use Nip_Form as NipForm;
10
use Nip_Form_Element_Select as SelectElement;
11
12
/**
13
 * Class Form
14
 * @package ByTIC\Payments\Gateways\Providers\AbstractGateway
15
 */
16
abstract class Form
17
{
18
    use MagicMethodElementsFormTrait;
19
20
    /**
21
     * @var GatewayTrait
22
     */
23
    protected $gateway;
24
25
    /**
26
     * @var NipForm
27
     */
28
    protected $form;
29
30
    protected $elements;
31
32
    public function init()
33
    {
34
        $this->initElements();
35
        if (is_array($this->elements) && count($this->elements) > 0) {
36
            $this->getForm()->addDisplayGroup($this->elements, $this->getGateway()->getLabel());
37
            $this->getForm()->getDisplayGroup($this->getGateway()->getLabel())
38
                ->setAttrib('class', 'payment_gateway_fieldset')
39
                ->setAttrib('id', 'payment_gateway_' . $this->getGateway()->getName());
40
        }
41
    }
42
43
    /**
44
     * @return void
45
     */
46
    public function initElements()
47
    {
48
    }
49
50
    /**
51
     * @return NipForm|PaymentMethodFormTrait
52
     */
53
    public function getForm()
54
    {
55
        return $this->form;
56
    }
57
58
    /**
59
     * @param $form
60
     * @return $this
61
     */
62
    public function setForm($form)
63
    {
64
        $this->form = $form;
65
66
        return $this;
67
    }
68
69
    /**
70
     * @return GatewayTrait
71
     */
72
    public function getGateway()
73
    {
74
        return $this->gateway;
75
    }
76
77
    /**
78
     * @param GatewayTrait $gateway
79
     * @return $this
80
     */
81
    public function setGateway($gateway)
82
    {
83
        $this->gateway = $gateway;
84
85
        return $this;
86
    }
87
88
    /**
89
     *
90
     */
91
    public function getDataFromModel()
92
    {
93
        if (is_array($this->elements) && count($this->elements) > 0) {
94
            $gName = $this->getGateway()->getName();
95
            $options = $this->getForm()->getModel()->getOption($gName);
96
            foreach ($this->elements as $name => $inputName) {
97
                $element = $this->getForm()->{$inputName};
98
                if (isset($options[$name])) {
99
                    $element->setValue($options[$name]);
100
                }
101
            }
102
        }
103
    }
104
105
    /**
106
     * @param $request
107
     * @return void
108
     */
109
    public function getDataFromRequest($request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

109
    public function getDataFromRequest(/** @scrutinizer ignore-unused */ $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
110
    {
111
    }
112
113
    /**
114
     * @return bool
115
     */
116
    public function processValidation()
117
    {
118
        return true;
119
    }
120
121
    public function saveToModel()
122
    {
123
        if (is_array($this->elements) && count($this->elements) > 0) {
124
            $gName = $this->getGateway()->getName();
125
            if ($this->getForm()->getModel()->getOption('payment_gateway') == $gName) {
126
                $options = $this->getOptionsForSaveToModel();
127
                $this->getForm()->getModel()->setOption($gName, $options);
128
            }
129
        }
130
    }
131
132
    /**
133
     * @return array
134
     */
135
    protected function getOptionsForSaveToModel()
136
    {
137
        $options = [];
138
        foreach ($this->elements as $name => $inputName) {
139
            $element = $this->getForm()->{$inputName};
140
            $options[$name] = $element->getValue();
141
        }
142
        return $options;
143
    }
144
145
    /**
146
     * @return bool
147
     */
148
    public function process()
149
    {
150
        return true;
151
    }
152
153
    protected function initElementSandbox()
154
    {
155
        $this->addRadioGroup('sandbox', 'sandbox', true);
156
        /** @var SelectElement $element */
157
        $element = $this->getForm()->getElement($this->getGateway()->getName() . '[sandbox]');
158
        $element->getRenderer()->setSeparator('');
0 ignored issues
show
Bug introduced by
The method setSeparator() does not exist on Nip\Form\Renderer\Elements\AbstractElementRenderer. It seems like you code against a sub-type of Nip\Form\Renderer\Elements\AbstractElementRenderer such as Nip_Form_Renderer_Elements_Input_Group. ( Ignorable by Annotation )

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

158
        $element->getRenderer()->/** @scrutinizer ignore-call */ setSeparator('');
Loading history...
159
        $element->addOption('yes', 'Yes');
160
        $element->addOption('no', 'No');
161
    }
162
163
    /**
164
     * @param $name
165
     * @param $label
166
     * @param $type
167
     * @param $isRequired
168
     * @return AbstractForm
169
     */
170
    protected function add($name, $label, $type, $isRequired)
171
    {
172
        return $this->getForm()->add($name, $label, $type, $isRequired);
173
    }
174
175
    /**
176
     * @param $arguments
177
     * @return mixed
178
     */
179
    protected function getElementNameFromMagicMethodArguments($arguments)
180
    {
181
        $name = '' . $this->getGateway()->getName() . '[' . $arguments[0] . ']';
182
        $this->elements[$arguments[0]] = $name;
183
184
        return $name;
185
    }
186
}
187