PaymentMethodFormTrait::getDataFromRequest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 2
c 1
b 0
f 1
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace ByTIC\Payments\Forms\Traits;
4
5
use ByTIC\MediaLibrary\HasMedia\HasMediaTrait;
6
use ByTIC\MediaLibrary\HasMedia\Traits\AddMediaTrait;
7
use ByTIC\Payments\Gateways\Manager as GatewaysManager;
8
use ByTIC\Payments\Gateways\Providers\AbstractGateway\Traits\GatewayTrait;
9
use ByTIC\Payments\Models\Methods\Traits\RecordTrait as PaymentMethod;
10
use ByTIC\Payments\Models\Methods\Types\CreditCards;
11
use Nip\Form\Elements\AbstractElement as FormElementAbstract;
12
use Nip_Form_Element_Select as FormSelect;
13
14
/**
15
 * Class PaymentMethodFormTrait
16
 * @package ByTIC\Payments\Forms\Traits
17
 *
18
 * @method addInput($name, $label = false, $type = 'input', $isRequired = false)
19
 * @method addHidden($name, $label = false, $type = 'input', $isRequired = false)
20
 * @method addSelect($name, $label = false, $type = 'input', $isRequired = false)
21
 * @method addDisplayGroup(array $elements, $name)
22
 */
23
trait PaymentMethodFormTrait
24
{
25
//    use AbstractFormTrait;
26
27
    /**
28
     * @var null|GatewaysManager
29
     */
30
    protected $paymentGatewaysManager = null;
31
32
    /**
33
     * @var null|GatewayTrait[]
34
     */
35
    protected $paymentGatewaysItems = null;
36
37
    /**
38
     * @var null|array
39
     */
40
    protected $paymentGatewaysNames = null;
41
42
    /**
43
     * @param $request
44
     */
45
    public function getDataFromRequest($request)
46
    {
47
        /** @noinspection PhpUndefinedClassInspection */
48
        parent::getDataFromRequest($request);
49
50
        $this->getDataFromRequestPaymentGateways($request);
51
    }
52
53
    /**
54
     * @param $request
55
     */
56
    protected function getDataFromRequestPaymentGateways($request)
57
    {
58
        $gateways = $this->getPaymentGatewaysItems();
59
        foreach ($gateways as $gateway) {
60
            $gateway->getOptionsForm()->getDataFromRequest($request);
61
        }
62
    }
63
64
    /**
65
     * @return GatewayTrait[]
66
     */
67
    public function getPaymentGatewaysItems()
68
    {
69
        $this->checkPaymentGatewaysValues();
70
71
        return $this->paymentGatewaysItems;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->paymentGatewaysItems also could return the type ByTIC\Payments\Gateways\GatewaysCollection which is incompatible with the documented return type ByTIC\Payments\Gateways\...y\Traits\GatewayTrait[].
Loading history...
72
    }
73
74
    /**
75
     * @param null $paymentGatewaysItems
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $paymentGatewaysItems is correct as it would always require null to be passed?
Loading history...
76
     */
77
    public function setPaymentGatewaysItems($paymentGatewaysItems)
78
    {
79
        $this->paymentGatewaysItems = $paymentGatewaysItems;
80
    }
81
82
    protected function checkPaymentGatewaysValues()
83
    {
84
        if ($this->paymentGatewaysItems == null) {
85
            $this->paymentGatewaysItems = $this->getPaymentGatewaysManager()::getAll();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getPaymentGatewaysManager()::getAll() of type ByTIC\Payments\Gateways\GatewaysCollection is incompatible with the declared type ByTIC\Payments\Gateways\...its\GatewayTrait[]|null of property $paymentGatewaysItems.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
86
            $this->paymentGatewaysNames = $this->paymentGatewaysItems->keys();
87
        }
88
    }
89
90
    /**
91
     * @return GatewaysManager
92
     */
93
    public function getPaymentGatewaysManager()
94
    {
95
        if ($this->paymentGatewaysManager == null) {
96
            $this->initPaymentGatewaysManager();
97
        }
98
99
        return $this->paymentGatewaysManager;
100
    }
101
102
    /**
103
     * @param ?GatewaysManager $paymentGatewaysManager
104
     */
105
    public function setPaymentGatewaysManager($paymentGatewaysManager)
106
    {
107
        $this->paymentGatewaysManager = $paymentGatewaysManager;
108
    }
109
110
    protected function initPaymentGatewaysManager()
111
    {
112
        $this->setPaymentGatewaysManager($this->newPaymentGatewaysManager());
113
    }
114
115
    /**
116
     * @return GatewaysManager
117
     */
118
    protected function newPaymentGatewaysManager()
119
    {
120
        return new GatewaysManager();
121
    }
122
123
    public function processValidation()
124
    {
125
        /** @noinspection PhpUndefinedClassInspection */
126
        parent::processValidation();
127
128
        $this->processValidationPaymentGateways();
129
    }
130
131
    protected function processValidationPaymentGateways()
132
    {
133
        $gateways = $this->getPaymentGatewaysItems();
134
        foreach ($gateways as $gateway) {
135
            $gateway->getOptionsForm()->processValidation();
136
        }
137
    }
138
139
    public function process()
140
    {
141
        $this->saveToModel();
142
        $this->getModel()->save();
0 ignored issues
show
Bug introduced by
It seems like save() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

142
        $this->getModel()->/** @scrutinizer ignore-call */ save();
Loading history...
143
144
        $this->processFormPaymentGateways();
145
    }
146
147
    public function saveToModel()
148
    {
149
        /** @noinspection PhpUndefinedClassInspection */
150
        parent::saveToModel();
151
152
        $this->saveToModelTypeMethod();
153
        $this->saveToModelPaymentGateways();
154
    }
155
156
    protected function saveToModelTypeMethod()
157
    {
158
        /** @var FormSelect $typeInput */
159
        $typeInput = $this->getElement('type');
160
        $type = $typeInput->getValue();
161
162
        if (in_array($type, $this->getPaymentGatewaysNames())) {
0 ignored issues
show
Bug introduced by
It seems like $this->getPaymentGatewaysNames() can also be of type null; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

162
        if (in_array($type, /** @scrutinizer ignore-type */ $this->getPaymentGatewaysNames())) {
Loading history...
163
            $this->getModel()->type = 'credit-cards';
164
            $this->getModel()->setOption('payment_gateway', $type);
0 ignored issues
show
Bug introduced by
It seems like setOption() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

164
            $this->getModel()->/** @scrutinizer ignore-call */ setOption('payment_gateway', $type);
Loading history...
165
        }
166
    }
167
168
    /**
169
     * @param $name
170
     * @return FormElementAbstract
171
     */
172
    abstract public function getElement($name);
173
174
    /**
175
     * @return array|null
176
     */
177
    public function getPaymentGatewaysNames()
178
    {
179
        $this->checkPaymentGatewaysValues();
180
181
        return $this->paymentGatewaysNames;
182
    }
183
184
    /**
185
     * @return PaymentMethod|HasMediaTrait|AddMediaTrait
186
     */
187
    abstract public function getModel();
188
189
    protected function saveToModelPaymentGateways()
190
    {
191
        $gateways = $this->getPaymentGatewaysItems();
192
        foreach ($gateways as $gateway) {
193
            $gateway->getOptionsForm()->saveToModel();
194
        }
195
    }
196
197
    protected function processFormPaymentGateways()
198
    {
199
        $gateways = $this->getPaymentGatewaysItems();
200
        foreach ($gateways as $gateway) {
201
            $gateway->getOptionsForm()->process();
202
        }
203
    }
204
205
    protected function initNameElements()
206
    {
207
        $this->addInput('internal_name', translator()->trans('internal_name'), true);
208
        $this->addInput('name', translator()->trans('name'), true);
209
210
        $this->addDisplayGroup(['internal_name', 'name'], 'Details');
211
    }
212
213
    protected function initTypeElement()
214
    {
215
        if ($this->getModel()->getPurchasesCount() > 0) {
0 ignored issues
show
Bug introduced by
It seems like getPurchasesCount() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

215
        if ($this->getModel()->/** @scrutinizer ignore-call */ getPurchasesCount() > 0) {
Loading history...
216
            $this->addHidden('type', translator()->trans('type'), true);
217
            $this->addInput('typeText', translator()->trans('type'), false);
218
            $this->getElement('typeText')->setAttrib('readonly', 'readonly');
219
        } else {
220
            $this->initTypeSelect();
221
        }
222
223
        $this->getElement('type')->setId('payment_type');
224
    }
225
226
    protected function initTypeSelect()
227
    {
228
        $this->addSelect('type', translator()->trans('type'), true);
229
        $types = $this->getModel()->getManager()->getTypes();
0 ignored issues
show
Bug introduced by
It seems like getManager() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

229
        $types = $this->getModel()->/** @scrutinizer ignore-call */ getManager()->getTypes();
Loading history...
230
        foreach ($types as $type) {
231
            $this->getElement('type')
232
                ->addOption($type->getName(), $type->getLabel());
0 ignored issues
show
Bug introduced by
The method addOption() does not exist on Nip\Form\Elements\AbstractElement. It seems like you code against a sub-type of Nip\Form\Elements\AbstractElement such as Nip_Form_Element_Select or Nip_Form_Element_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

232
                ->/** @scrutinizer ignore-call */ addOption($type->getName(), $type->getLabel());
Loading history...
233
        }
234
        $this->appendPaymentGatewaysOptgroupOption();
235
    }
236
237
    protected function appendPaymentGatewaysOptgroupOption()
238
    {
239
        $gateways = $this->getPaymentGatewaysItems();
240
        /** @var FormSelect $typeInput */
241
        $typeInput = $this->getElement('type');
242
        foreach ($gateways as $name => $gateway) {
243
            $typeInput->appendOptgroupOption(
244
                $this->getPaymentGatewaysManager()->getLabel('title'),
245
                $gateway->getName(),
246
                $gateway->getLabel()
247
            );
248
        }
249
    }
250
251
    protected function parseTypeForPaymentGateway()
252
    {
253
        $model = $this->getModel();
254
        if ($model->getType() instanceof CreditCards && $model->getOption('payment_gateway')) {
0 ignored issues
show
Bug introduced by
It seems like getType() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

254
        if ($model->/** @scrutinizer ignore-call */ getType() instanceof CreditCards && $model->getOption('payment_gateway')) {
Loading history...
Bug introduced by
It seems like getOption() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

254
        if ($model->getType() instanceof CreditCards && $model->/** @scrutinizer ignore-call */ getOption('payment_gateway')) {
Loading history...
255
            $this->getElement('type')->setValue($this->getModel()->getOption('payment_gateway'));
256
        }
257
    }
258
259
    protected function initPaymentGatewaysOptionsForm()
260
    {
261
        $gateways = $this->getPaymentGatewaysItems();
262
263
        foreach ($gateways as $name => $gateway) {
264
            $gateway->getOptionsForm()->setForm($this)->init();
265
        }
266
    }
267
268
    protected function getDataFromModelPaymentGateways()
269
    {
270
        $gateways = $this->getPaymentGatewaysItems();
271
        foreach ($gateways as $gateway) {
272
            $gateway->getOptionsForm()->getDataFromModel();
273
        }
274
    }
275
}
276