Test Failed
Push — master ( 99edcd...37bac2 )
by
04:31 queued 02:01
created

AbstractCommand::getCmd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the LineMob package.
5
 *
6
 * (c) Ishmael Doss <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LineMob\Core\Command;
13
14
use LineMob\Core\Input;
15
use LineMob\Core\Storage\CommandDataInterface;
16
use LineMob\Core\Template\TemplateInterface;
17
18
/**
19
 * @property boolean $active
20
 * @property string $logs
21
 *
22
 * @author Ishmael Doss <[email protected]>
23
 */
24
abstract class AbstractCommand implements \ArrayAccess
25
{
26
    /**
27
     * @var string a persistent code
28
     */
29
    protected $code;
30
31
    /**
32
     * @var string
33
     */
34
    protected $name;
35
36
    /**
37
     * @var string
38
     */
39
    protected $description;
40
41
    /**
42
     * @var string
43
     */
44
    protected $cmd;
45
46
    /**
47
     * @var CommandDataInterface
48
     */
49
    public $storage;
50
51
    /**
52
     * @var Input
53
     */
54
    public $input;
55
56
    /**
57
     * @var TemplateInterface
58
     */
59
    public $message;
60
61
    /**
62
     * @var string
63
     */
64
    public $mode;
65
66
    /**
67
     * @var string
68
     */
69
    public $to;
70
71
    /**
72
     * @var string[]
73
     */
74
    public $tos;
75
76
    /**
77
     * @var array
78
     */
79
    protected $data = [];
80
81
    /**
82
     * @var array
83
     */
84
    protected $logData = [];
85
86
    /**
87
     * @param array $data
88
     */
89
    public function __construct(array $data = [])
90
    {
91
        $this->setForzenParameters($data);
92
93
        $this->data = array_replace_recursive($this->data, $data);
94
    }
95
96
    /**
97
     * @param array $data
98
     */
99
    private function setForzenParameters(array &$data)
100
    {
101
        foreach (['name', 'description', 'cmd'] as $value) {
102
            if (array_key_exists($value, $data)) {
103
                $this->$value = $data[$value];
104
                unset($data[$value]);
105
            }
106
        }
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function getCode()
113
    {
114
        return $this->code;
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    public function getName()
121
    {
122
        return $this->name;
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    public function getDescription()
129
    {
130
        return $this->description;
131
    }
132
133
    /**
134
     * @return string
135
     */
136
    public function getCmd()
137
    {
138
        return $this->cmd;
139
    }
140
141
    /**
142
     * @param array $data
143
     */
144
    public function merge(array $data)
145
    {
146
        $this->data = array_replace_recursive($this->data, $data);
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function offsetExists($offset)
153
    {
154
        return array_key_exists($offset, $this->data);
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function offsetGet($offset)
161
    {
162
        if (!$this->offsetExists($offset)) {
163
            return null;
164
        }
165
166
        return $this->data[$offset];
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172
    public function offsetSet($offset, $value)
173
    {
174
        $this->data[$offset] = $value;
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180
    public function offsetUnset($offset)
181
    {
182
        unset($this->data[$offset]);
183
    }
184
185
    /**
186
     * @return array
187
     */
188
    public function getData()
189
    {
190
        return $this->data;
191
    }
192
193
    /**
194
     * {@inheritdoc}
195
     */
196
    public function __get($name)
197
    {
198
        if ('logs' === $name) {
199
            return $this->logData;
200
        }
201
202
        return $this->offsetGet($name);
203
    }
204
205
    /**
206
     * {@inheritdoc}
207
     */
208
    public function __set($name, $value)
209
    {
210
        if ('logs' === $name) {
211
            if (empty($this->logData)) {
212
                $this->logData = [];
213
            }
214
215
            array_push($this->logData, $value);
216
217
            return;
218
        }
219
220
        $this->offsetSet($name, $value);
221
    }
222
223
    /**
224
     * @param string $cmd
225
     * @return bool
226
     */
227
    public function supported($cmd)
228
    {
229
        return $this->cmd === $cmd;
230
    }
231
}
232