Issues (33)

src/Form.php (8 issues)

1
<?php
2
3
namespace ByTIC\Payments\Mobilpay;
4
5
use ByTIC\Payments\Gateways\Providers\AbstractGateway\Form as AbstractForm;
6
use ByTIC\Payments\Models\Methods\Types\CreditCards;
7
use Symfony\Component\HttpFoundation\File\UploadedFile;
8
9
/**
10
 * Class Form
11
 * @package ByTIC\Payments\Mobilpay
12
 */
13
class Form extends AbstractForm
14
{
15
    public function initElements()
16
    {
17
        parent::initElements();
18
        $this->initElementSandbox();
19
        $this->addInput('signature', 'Signature', false);
20
        $this->addInput('sellerId', 'Seller ID', false);
21
        $this->addInput('username', 'Username', false);
22
23
        $this->addFile('file', 'Certificate', false);
24
        $this->addTextarea('certificate', 'Certificate', false);
25
        $element = $this->getForm()->getElement('mobilpay[certificate]');
26
        $element->setAttrib('readonly', 'readonly');
27
28
        $this->addFile('private-key', 'Private key', false);
29
        $this->addTextarea('privateKey', 'Private key', false);
30
        $element = $this->getForm()->getElement('mobilpay[privateKey]');
31
        $element->setAttrib('readonly', 'readonly');
32
    }
33
34
    public function getDataFromModel()
35
    {
36
        $type = $this->getForm()->getModel()->getType();
37
        if ($type instanceof CreditCards) {
0 ignored issues
show
$type is never a sub-type of ByTIC\Payments\Models\Methods\Types\CreditCards.
Loading history...
38
            $type->getGateway();
39
        }
40
        parent::getDataFromModel();
41
    }
42
43
    /**
44
     * @return bool
45
     */
46
    public function processValidation()
47
    {
48
        parent::processValidation();
49
50
        if (isset($_FILES['mobilpay'])) {
51
            $files = ['file', 'private-key'];
52
            foreach ($files as $fileType) {
53
                $element = $this->getForm()->getElement('mobilpay['.$fileType.']');
54
55
                /** @var UploadedFile $uploadedFile */
56
                $uploadedFile = $element->getValue();
0 ignored issues
show
Are you sure the assignment to $uploadedFile is correct as $element->getValue() targeting Nip\Form\Elements\AbstractElement::getValue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
57
                if ($uploadedFile instanceof UploadedFile) {
58
                    if ($uploadedFile->isValid()) {
59
                        $extension = $fileType == 'file' ? 'cer' : 'key';
60
                        if ($uploadedFile->getClientOriginalExtension() != $extension) {
61
                            $element->addError(
62
                                'Invalid extension ['.$extension.']['.$uploadedFile->getClientOriginalName().']'
63
                            );
64
                        }
65
                    } else {
66
                        $element->addError($uploadedFile->getErrorMessage());
67
                    }
68
                }
69
            }
70
        }
71
72
        return true;
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78
    protected function generateModelOptions()
79
    {
80
        $options = parent::generateModelOptions();
0 ignored issues
show
The assignment to $options is dead and can be removed.
Loading history...
81
82
        $files = ['certificate', 'privateKey'];
0 ignored issues
show
The assignment to $files is dead and can be removed.
Loading history...
83
84
        return $this->files[$type];
0 ignored issues
show
Bug Best Practice introduced by
The property files does not exist on ByTIC\Payments\Mobilpay\Form. Did you maybe forget to declare it?
Loading history...
Comprehensibility Best Practice introduced by
The variable $type seems to be never defined.
Loading history...
85
    }
86
87
    /**
88
     * @return array
89
     */
90
    public function getOptionsForSaveToModel()
91
    {
92
        $options = parent::getOptionsForSaveToModel();
93
        unset($options['file'], $options['private-key']);
94
        return $options;
95
    }
96
97
    /**
98
     * @return bool
99
     */
100
    public function process()
101
    {
102
        parent::process();
103
104
        $model = $this->getForm()->getModel();
105
        $options = $model->getPaymentGatewayOptions();
106
107
        $files = ['file' => 'certificate', 'private-key' => 'privateKey'];
108
        foreach ($files as $file => $variable) {
109
            $fileData = $this->getForm()->getElement('mobilpay[' . $file . ']')->getValue();
0 ignored issues
show
Are you sure the assignment to $fileData is correct as $this->getForm()->getEle...file . ']')->getValue() targeting Nip\Form\Elements\AbstractElement::getValue() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
110
            if (is_array($fileData) && $fileData['tmp_name']) {
111
                $content = file_get_contents($fileData["tmp_name"]);
112
                $options[$variable] = $content;
113
            }
114
        }
115
116
        $model->setPaymentGatewayOptions($options);
117
        $model->saveRecord();
118
119
        return $options;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $options returns the type Nip_Form which is incompatible with the documented return type boolean.
Loading history...
120
    }
121
}
122