Options   A
last analyzed

Complexity

Total Complexity 37

Size/Duplication

Total Lines 263
Duplicated Lines 0 %

Test Coverage

Coverage 98.53%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 60
c 2
b 1
f 0
dl 0
loc 263
rs 9.44
ccs 67
cts 68
cp 0.9853
wmc 37

21 Methods

Rating   Name   Duplication   Size   Complexity  
A getParentIdColumnName() 0 3 2
A setLevelColumnName() 0 9 2
A setParentIdColumnName() 0 9 2
A setDbSelectBuilder() 0 3 1
A getSequenceName() 0 3 1
A getDbSelectBuilder() 0 3 1
A __construct() 0 14 2
A setIdColumnName() 0 9 2
A getIdColumnName() 0 3 2
A setLeftColumnName() 0 9 2
A setScopeColumnName() 0 3 1
A setSequenceName() 0 3 1
A setRightColumnName() 0 9 2
A getLevelColumnName() 0 3 2
A getScopeColumnName() 0 3 2
A getRightColumnName() 0 3 2
A setOptions() 0 6 3
A setTableName() 0 9 2
A getTableName() 0 3 1
A addTableName() 0 7 2
A getLeftColumnName() 0 3 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace StefanoTree\NestedSet;
6
7
use StefanoTree\Exception\InvalidArgumentException;
8
9
class Options
10
{
11
    private $tableName = '';
12
13
    private $sequenceName = null;
14
15
    private $idColumnName = '';
16
17
    private $leftColumnName = 'lft';
18
    private $rightColumnName = 'rgt';
19
    private $levelColumnName = 'level';
20
    private $parentIdColumnName = 'parent_id';
21
    private $scopeColumnName = null;
22
23
    private $dbSelectBuilder = null;
24
25
    /**
26
     * @param array $options
27
     *
28
     * @throws InvalidArgumentException
29
     */
30 171
    public function __construct(array $options)
31
    {
32
        $requiredOptions = array(
33
            'tableName', 'idColumnName',
34
        );
35
36 171
        $missingKeys = array_diff_key(array_flip($requiredOptions), $options);
37
38 171
        if (count($missingKeys)) {
39 1
            throw new InvalidArgumentException(implode(', ', array_flip($missingKeys))
40
                .' must be set');
41
        }
42
43 170
        $this->setOptions($options);
44
    }
45
46
    /**
47
     * @param array $options
48
     */
49 170
    protected function setOptions(array $options): void
50
    {
51 170
        foreach ($options as $name => $value) {
52 170
            $methodName = 'set'.ucfirst($name);
53 170
            if (method_exists($this, $methodName)) {
54 170
                $this->{$methodName}($value);
55
            }
56
        }
57
    }
58
59
    /**
60
     * @param string $tableName
61
     *
62
     * @throws InvalidArgumentException
63
     */
64 170
    public function setTableName(string $tableName): void
65
    {
66 170
        $tableName = trim($tableName);
67
68 170
        if (empty($tableName)) {
69 1
            throw new InvalidArgumentException('tableName cannot be empty');
70
        }
71
72 170
        $this->tableName = $tableName;
73
    }
74
75
    /**
76
     * @return string
77
     */
78 108
    public function getTableName(): string
79
    {
80 108
        return $this->tableName;
81
    }
82
83
    /**
84
     * @param string $sequenceName
85
     */
86 1
    public function setSequenceName(string $sequenceName): void
87
    {
88 1
        $this->sequenceName = trim($sequenceName);
89
    }
90
91
    /**
92
     * @return null|string
93
     */
94 17
    public function getSequenceName(): ?string
95
    {
96 17
        return $this->sequenceName;
97
    }
98
99
    /**
100
     * @param string $idColumnName
101
     *
102
     * @throws InvalidArgumentException
103
     */
104 170
    public function setIdColumnName(string $idColumnName): void
105
    {
106 170
        $idColumnName = trim($idColumnName);
107
108 170
        if (empty($idColumnName)) {
109 1
            throw new InvalidArgumentException('idColumnName cannot be empty');
110
        }
111
112 170
        $this->idColumnName = $idColumnName;
113
    }
114
115
    /**
116
     * @param bool $withTableName
117
     *
118
     * @return string
119
     */
120 103
    public function getIdColumnName(bool $withTableName = false): string
121
    {
122 103
        return ($withTableName) ? $this->addTableName($this->idColumnName) : $this->idColumnName;
123
    }
124
125
    /**
126
     * @param string $leftColumnName
127
     *
128
     * @throws InvalidArgumentException
129
     */
130 2
    public function setLeftColumnName(string $leftColumnName): void
131
    {
132 2
        $leftColumnName = trim($leftColumnName);
133
134 2
        if (empty($leftColumnName)) {
135 1
            throw new InvalidArgumentException('leftColumnName cannot be empty');
136
        }
137
138 1
        $this->leftColumnName = $leftColumnName;
139
    }
140
141
    /**
142
     * @param bool $withTableName
143
     *
144
     * @return string
145
     */
146 80
    public function getLeftColumnName(bool $withTableName = false): string
147
    {
148 80
        return ($withTableName) ? $this->addTableName($this->leftColumnName) : $this->leftColumnName;
149
    }
150
151
    /**
152
     * @param string $rightColumnName
153
     *
154
     * @throws InvalidArgumentException
155
     */
156 2
    public function setRightColumnName(string $rightColumnName): void
157
    {
158 2
        $rightColumnName = trim($rightColumnName);
159
160 2
        if (empty($rightColumnName)) {
161 1
            throw new InvalidArgumentException('rightColumnName cannot be empty');
162
        }
163
164 1
        $this->rightColumnName = $rightColumnName;
165
    }
166
167
    /**
168
     * @param bool $withTableName
169
     *
170
     * @return string
171
     */
172 79
    public function getRightColumnName(bool $withTableName = false): string
173
    {
174 79
        return ($withTableName) ? $this->addTableName($this->rightColumnName) : $this->rightColumnName;
175
    }
176
177
    /**
178
     * @param string $levelColumnName
179
     *
180
     * @throws InvalidArgumentException
181
     */
182 2
    public function setLevelColumnName(string $levelColumnName): void
183
    {
184 2
        $levelColumnName = trim($levelColumnName);
185
186 2
        if (empty($levelColumnName)) {
187 1
            throw new InvalidArgumentException('levelColumnName cannot be empty');
188
        }
189
190 1
        $this->levelColumnName = $levelColumnName;
191
    }
192
193
    /**
194
     * @param bool $withTableName
195
     *
196
     * @return string
197
     */
198 75
    public function getLevelColumnName(bool $withTableName = false): string
199
    {
200 75
        return ($withTableName) ? $this->addTableName($this->levelColumnName) : $this->levelColumnName;
201
    }
202
203
    /**
204
     * @param string $parentIdColumnName
205
     *
206
     * @throws InvalidArgumentException
207
     */
208 2
    public function setParentIdColumnName(string $parentIdColumnName): void
209
    {
210 2
        $parentIdColumnName = trim($parentIdColumnName);
211
212 2
        if (empty($parentIdColumnName)) {
213 1
            throw new InvalidArgumentException('parentIdColumnName cannot be empty');
214
        }
215
216 1
        $this->parentIdColumnName = $parentIdColumnName;
217
    }
218
219
    /**
220
     * @param bool $withTableName
221
     *
222
     * @return string
223
     */
224 81
    public function getParentIdColumnName(bool $withTableName = false): string
225
    {
226 81
        return ($withTableName) ? $this->addTableName($this->parentIdColumnName) : $this->parentIdColumnName;
227
    }
228
229
    /**
230
     * @param string $scopeColumnName
231
     */
232 39
    public function setScopeColumnName(string $scopeColumnName): void
233
    {
234 39
        $this->scopeColumnName = trim($scopeColumnName);
235
    }
236
237
    /**
238
     * @param bool $withTableName
239
     *
240
     * @return null|string
241
     */
242 81
    public function getScopeColumnName(bool $withTableName = false): ?string
243
    {
244 81
        return ($withTableName) ? $this->addTableName($this->scopeColumnName) : $this->scopeColumnName;
245
    }
246
247 85
    private function addTableName(?string $value): ?string
248
    {
249 85
        if (null === $value) {
250
            return null;
251
        }
252
253 85
        return sprintf('%s.%s', $this->getTableName(), $value);
254
    }
255
256
    /**
257
     * Modify base DB select. Must be without where, order parts.
258
     *
259
     * @param null|callable $builder
260
     */
261 6
    public function setDbSelectBuilder(?callable $builder): void
262
    {
263 6
        $this->dbSelectBuilder = $builder;
264
    }
265
266
    /**
267
     * @return null|callable
268
     */
269 29
    public function getDbSelectBuilder(): ?callable
270
    {
271 29
        return $this->dbSelectBuilder;
272
    }
273
}
274