AbstractResponder   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 14
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 124
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setOption() 0 8 2
A setOptions() 0 10 3
A getOptions() 0 4 1
A getOption() 0 8 2
A isValidOption() 0 8 2
A registerOption() 0 8 2
A render() 0 13 2
1
<?php
2
3
namespace DM\AjaxCom\Responder;
4
5
abstract class AbstractResponder implements ResponderInterface
6
{
7
    const OBJECT_IDENTIFIER = null;
8
9
    /**
10
     * @var array validOptions - container holding valid options for the responseObject
11
     */
12
    private $validOptions = array();
13
14
    /**
15
     * @var array
16
     */
17
    private $options = array();
18
19
    /**
20
     * Sets value to the option of the operation
21
     *
22
     * @var string $option
23
     * @var string $value
24
     * @return ResponderInterface
25
     */
26
    public function setOption($option, $value)
27
    {
28
        if ($this->isValidOption($option)) {
29
            $this->options[$option] = $value;
30
        }
31
32
        return $this;
33
    }
34
35
    /**
36
     * Sets values to the options of the operation
37
     *
38
     * @var array $option
39
     * @return ResponderInterface
40
     */
41
    public function setOptions(array $options)
42
    {
43
        foreach ($options as $key => $value) {
44
            if ($this->isValidOption($key)) {
45
                $this->options[$key] = $value;
46
            }
47
        }
48
49
        return $this;
50
    }
51
52
    /**
53
     * Gets all options of the operation
54
     *
55
     * @return array
56
     */
57
    public function getOptions()
58
    {
59
        return $this->options;
60
    }
61
62
    /**
63
     * Gets value from options
64
     *
65
     * @var string $option
66
     * @return string value
67
     * @throws \Exception
68
     */
69
    public function getOption($option)
70
    {
71
        if (!isset($this->options[$option])) {
72
            throw new \Exception('This option does not exist');
73
        }
74
75
        return $this->options[$option];
76
    }
77
78
    /**
79
     * Validates a option comparing it to the registered options of the responseobject
80
     *
81
     * @var array $option
82
     * @return bool
83
     * @throws \Exception
84
     */
85
    private function isValidOption($option)
86
    {
87
        if (!in_array($option, $this->validOptions)) {
88
            throw new \Exception('Not a valid option type for this component');
89
        }
90
91
        return true;
92
    }
93
94
    /**
95
     * registers an option from the response object
96
     *
97
     * @var array $option
98
     * @return ResponderInterface
99
     */
100
    protected function registerOption($option)
101
    {
102
        if (!in_array($option, $this->validOptions)) {
103
            array_push($this->validOptions, $option);
104
        }
105
106
        return $this;
107
    }
108
109
110
    /**
111
     * Render the operation item
112
     * @return string operation item
113
     * @throws \Exception
114
     */
115
    public function render()
116
    {
117
        if (!$this::OBJECT_IDENTIFIER) {
118
            throw new \Exception('No operation was set');
119
        }
120
121
        $operation = array(
122
            'operation' => $this::OBJECT_IDENTIFIER,
123
            'options' => $this->options
124
        );
125
126
        return $operation;
127
    }
128
}
129