AbstractResponder::isValidOption()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Everlution\Ajaxcom\Responder;
6
7
/**
8
 * Class AbstractResponder.
9
 *
10
 * @author Everlution s.r.o. <[email protected]>
11
 */
12
abstract class AbstractResponder implements ResponderInterface
13
{
14
    private $validOptions = [];
15
    private $options = [];
16
17
    public function setOption(string $option, $value)
18
    {
19
        if ($this->isValidOption($option)) {
20
            $this->options[$option] = $value;
21
        }
22
23
        return $this;
24
    }
25
26
    private function isValidOption(string $option)
27
    {
28
        if (false === in_array($option, $this->validOptions)) {
29
            throw new \Exception('Not a valid option type for this component');
30
        }
31
32
        return true;
33
    }
34
35
    protected function registerOption($option)
36
    {
37
        if (false === in_array($option, $this->validOptions)) {
38
            $this->validOptions[] = $option;
39
        }
40
41
        return $this;
42
    }
43
44
    public function render(): array
45
    {
46
        $operation = [
47
            'operation' => $this->getIdentifier(),
48
            'options' => $this->options,
49
        ];
50
51
        return $operation;
52
    }
53
54
    abstract protected function getIdentifier(): string;
55
}
56