Completed
Pull Request — master (#61)
by Michal
04:05
created

InputParam::isValid()   B

Complexity

Conditions 11
Paths 6

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 11

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 14
cts 14
cp 1
rs 7.3166
c 0
b 0
f 0
cc 11
nc 6
nop 0
crap 11

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Tomaj\NetteApi\Params;
4
5
use Nette\Application\UI\Form;
6
use Nette\Forms\Controls\BaseControl;
7
use Nette\Utils\Html;
8
use Tracy\Debugger;
9
10
abstract class InputParam implements ParamInterface
11
{
12
    const TYPE_POST      = 'POST';
13
    const TYPE_GET       = 'GET';
14
    const TYPE_PUT       = 'PUT';
15
    const TYPE_FILE      = 'FILE';
16
    const TYPE_COOKIE    = 'COOKIE';
17
    const TYPE_POST_RAW  = 'POST_RAW';
18
    const TYPE_POST_JSON = 'POST_JSON';
19
20
    const OPTIONAL = false;
21
    const REQUIRED = true;
22
23
    /** @var string */
24
    protected $type;
25
26
    /** @var string */
27
    protected $key;
28
29
    /** @var bool */
30
    protected $required = self::OPTIONAL;
31
32
    /** @var array|null */
33
    protected $availableValues = null;
34
35
    /** @var bool */
36
    protected $multi = false;
37
38
    /** @var string */
39
    protected $description = '';
40
41
    /** @var mixed */
42
    protected $default;
43
44
    /** @var mixed */
45
    protected $example;
46
47
    protected $errors = [];
48
49 69
    public function __construct(string $key)
50
    {
51 69
        $this->key = $key;
52 69
    }
53
54 45
    public function setRequired(): self
55
    {
56 45
        $this->required = self::REQUIRED;
57 45
        return $this;
58
    }
59
60 24
    public function setAvailableValues(array $availableValues): self
61
    {
62 24
        $this->availableValues = $availableValues;
63 24
        return $this;
64
    }
65
66 12
    public function setMulti(): self
67
    {
68 12
        $this->multi = true;
69 12
        return $this;
70
    }
71
72 21
    public function getType(): string
73
    {
74 21
        return $this->type;
75
    }
76
77 33
    public function getKey(): string
78
    {
79 33
        return $this->key;
80
    }
81
82 21
    public function isRequired(): bool
83
    {
84 21
        return $this->required;
85
    }
86
87 12
    public function getAvailableValues(): ?array
88
    {
89 12
        return $this->availableValues;
90
    }
91
92 42
    public function isMulti(): bool
93
    {
94 42
        return $this->multi;
95
    }
96
97 3
    public function setDescription(string $description): self
98
    {
99 3
        $this->description = $description;
100 3
        return $this;
101
    }
102
103 3
    public function getDescription(): string
104
    {
105 3
        return $this->description;
106
    }
107
108
    /**
109
     * @param mixed $default
110
     * @return self
111
     */
112 3
    public function setDefault($default): self
113
    {
114 3
        $this->default = $default;
115 3
        return $this;
116
    }
117
118
    /**
119
     * @return mixed
120
     */
121 12
    public function getDefault()
122
    {
123 12
        return $this->default;
124
    }
125
126
    /**
127
     * @param mixed $example
128
     * @return self
129
     */
130 3
    public function setExample($example): self
131
    {
132 3
        $this->example = $example;
133 3
        return $this;
134
    }
135
136
    /**
137
     * @return mixed
138
     */
139 12
    public function getExample()
140
    {
141 12
        return $this->example;
142
    }
143
144 12
    public function updateConsoleForm(Form $form): void
145
    {
146 12
        $count = $this->isMulti() ? 5 : 1;  // TODO moznost nastavit kolko inputov sa ma vygenerovat v konzole, default moze byt 5
147 12
        for ($i = 0; $i < $count; $i++) {
148 12
            $key = $this->getKey();
149 12
            if ($this->isMulti()) {
150 3
                $key = $key . '___' . $i;
151
            }
152 12
            $input = $this->addFormInput($form, $key);
153 12
            if ($this->description) {
154 3
                $input->setOption('description', Html::el('div', ['class' => 'param-description'])->setHtml($this->description));
155
            }
156 12
            if ($this->getExample() || $this->getDefault()) {
157 3
                $default = $this->getExample() ?: $this->getDefault();
158 3
                $default = is_array($default) ? ($default[$i] ?? null) : $default;
159 3
                $input->setDefaultValue($default);
160
            }
161
        }
162 12
    }
163
164 6
    protected function addFormInput(Form $form, string $key): BaseControl
165
    {
166 6
        if ($this->getAvailableValues()) {
167 3
            return $form->addSelect($key, $this->getParamLabel(), array_combine($this->getAvailableValues(), $this->getAvailableValues()))
168 3
                ->setPrompt('Select ' . $this->getLabel());
169
        }
170 6
        return $form->addText($key, $this->getParamLabel());
171
    }
172
173 12
    protected function getLabel(): string
174
    {
175 12
        return ucfirst(str_replace('_', ' ', $this->getKey()));
176
    }
177
178 12
    protected function getParamLabel(): string
179
    {
180 12
        $title = $this->getLabel();
181 12
        if ($this->isRequired()) {
182 3
            $title .= ' *';
183
        }
184 12
        $title .= ' (' . $this->getType() . ')';
185 12
        return $title;
186
    }
187
188
    /**
189
     * Check if actual value from environment is valid
190
     */
191 18
    public function isValid(): bool
192
    {
193 18
        $value = $this->getValue();
194 18
        if ($this->required === self::OPTIONAL && ($value === null || $value === '')) {
195 6
            return true;
196
        }
197
198 18
        if ($this->required && ($value === null || $value === '')) {
199 9
            $this->errors[] = 'Field is required';
200 9
            return false;
201
        }
202
203 12
        if ($this->availableValues !== null) {
204 9
            if (is_array($this->availableValues)) {
205 9
                $result = empty(array_diff(($this->isMulti() ? $value : [$value]), $this->availableValues));
206 9
                if ($result === false) {
207 9
                    $this->errors[] = 'Field contains not available value(s)';
208
                }
209 9
                return $result;
210
            }
211
        }
212
213 6
        return true;
214
    }
215
216 9
    public function getErrors(): array
217
    {
218 9
        return $this->errors;
219
    }
220
}
221