Completed
Push — master ( 059c3f...c080af )
by Mr
06:36
created

Query   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 114
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 5 1
A getAttributes() 0 4 1
A setAttributes() 0 5 1
A getEndpoint() 0 4 1
A setEndpoint() 0 5 1
A __construct() 0 13 3
A getQuery() 0 12 2
1
<?php
2
3
namespace RouterOS;
4
5
use RouterOS\Exceptions\QueryException;
6
use RouterOS\Interfaces\QueryInterface;
7
8
/**
9
 * Class Query for building queries
10
 *
11
 * @package RouterOS
12
 * @since   0.1
13
 */
14
class Query implements QueryInterface
15
{
16
    /**
17
     * Array of query attributes
18
     *
19
     * @var array
20
     */
21
    private $_attributes = [];
22
23
    /**
24
     * Endpoint of query
25
     *
26
     * @var string
27
     */
28
    private $_endpoint;
29
30
    /**
31
     * Query constructor.
32
     *
33
     * @param   array|string $endpoint   Path of endpoint
34
     * @param   array        $attributes List of attributes which should be set
35
     * @throws  QueryException
36
     */
37 22
    public function __construct($endpoint, array $attributes = [])
38
    {
39 22
        if (\is_string($endpoint)) {
40 18
            $this->setEndpoint($endpoint);
41 18
            $this->setAttributes($attributes);
42 4
        } elseif (\is_array($endpoint)) {
43 3
            $query = array_shift($endpoint);
44 3
            $this->setEndpoint($query);
45 3
            $this->setAttributes($endpoint);
46
        } else {
47 1
            throw new QueryException('Specified endpoint is not correct');
48
        }
49 21
    }
50
51
    /**
52
     * Append to array yet another attribute of query
53
     *
54
     * @param   string $word
55
     * @return  \RouterOS\Query
56
     */
57 12
    public function add(string $word): Query
58
    {
59 12
        $this->_attributes[] = $word;
60 12
        return $this;
61
    }
62
63
    /**
64
     * Get attributes array of current query
65
     *
66
     * @return  array
67
     */
68 14
    public function getAttributes(): array
69
    {
70 14
        return $this->_attributes;
71
    }
72
73
    /**
74
     * Set array of attributes
75
     *
76
     * @param   array $attributes
77
     * @since   0.7
78
     * @return  \RouterOS\Query
79
     */
80 21
    public function setAttributes(array $attributes): Query
81
    {
82 21
        $this->_attributes = $attributes;
83 21
        return $this;
84
    }
85
86
    /**
87
     * Get endpoint of current query
88
     *
89
     * @return  string|null
90
     */
91 15
    public function getEndpoint()
92
    {
93 15
        return $this->_endpoint;
94
    }
95
96
    /**
97
     * Set endpoint of query
98
     *
99
     * @param   string|null $endpoint
100
     * @since   0.7
101
     * @return  \RouterOS\Query
102
     */
103 21
    public function setEndpoint(string $endpoint = null): Query
104
    {
105 21
        $this->_endpoint = $endpoint;
106 21
        return $this;
107
    }
108
109
    /**
110
     * Build body of query
111
     *
112
     * @return  array
113
     * @throws  \RouterOS\Exceptions\QueryException
114
     */
115 12
    public function getQuery(): array
116
    {
117 12
        if ($this->getEndpoint() === null) {
118 1
            throw new QueryException('Endpoint of query is not set');
119
        }
120
121 11
        $endpoint   = $this->getEndpoint();
122 11
        $attributes = $this->getAttributes();
123 11
        array_unshift($attributes, $endpoint);
124
125 11
        return $attributes;
126
    }
127
}
128