Completed
Push — develop ( 7ed4f7...32afc4 )
by Bartko
03:33 queued 49s
created

Zend1DbAdapter::getRoots()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 8
nc 2
nop 1
crap 3
1
<?php
2
namespace StefanoTree\NestedSet\Adapter;
3
4
use StefanoTree\NestedSet\NodeInfo;
5
use StefanoTree\NestedSet\Options;
6
use Zend_Db_Adapter_Abstract as ZendDbAdapter;
7
8
class Zend1DbAdapter
9
    extends NestedTransactionDbAdapterAbstract
0 ignored issues
show
Coding Style introduced by
The extends keyword must be on the same line as the class name
Loading history...
Coding Style introduced by
Expected 0 spaces between "NestedTransactionDbAdapterAbstract" and comma; 1 found
Loading history...
10
    implements AdapterInterface
0 ignored issues
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
11
{
12
    protected $options;
13
14
    protected $dbAdapter;
15
16
    protected $defaultDbSelect;
17
18 35
    public function __construct(Options $options, ZendDbAdapter $dbAdapter)
19
    {
20 35
        $this->options = $options;
21 35
        $this->dbAdapter = $dbAdapter;
22 35
    }
23
24
    /**
25
     * @return Options
26
     */
27 34
    private function getOptions()
28
    {
29 34
        return $this->options;
30
    }
31
32
    /**
33
     * @return ZendDbAdapter
34
     */
35 31
    public function getDbAdapter()
36
    {
37 31
        return $this->dbAdapter;
38
    }
39
40 14
    public function lockTree($scope)
41
    {
42 14
        $options = $this->getOptions();
43
44 14
        $dbAdapter = $this->getDbAdapter();
45
46 14
        $select = $this->getDefaultDbSelect()
47 14
                       ->reset(\Zend_Db_Select::COLUMNS)
48 14
                       ->columns(array('i' => $options->getIdColumnName()))
49 14
                       ->forUpdate(true);
50
51 14
        if ($options->getScopeColumnName()) {
52 4
            $select->where($options->getScopeColumnName() . ' = ?', $scope);
53 4
        }
54
55 14
        $dbAdapter->fetchAll($select);
56 14
    }
57
58 15
    protected function _isInTransaction()
59
    {
60 15
        return $this->getDbAdapter()
61 15
                    ->getConnection()
62 15
                    ->inTransaction();
63
    }
64
65 15
    protected function _beginTransaction()
66
    {
67 15
        $this->getDbAdapter()->beginTransaction();
68 15
    }
69
70 14
    protected function _commitTransaction()
71
    {
72 14
        $this->getDbAdapter()->commit();
73 14
    }
74
75 1
    protected function _rollbackTransaction()
76
    {
77 1
        $this->getDbAdapter()->rollBack();
78 1
    }
79
80 2
    public function update($nodeId, array $data, NodeInfo $nodeInfo = null)
81
    {
82 2
        $options = $this->getOptions();
83
84 2
        $dbAdapter = $this->getDbAdapter();
85
86 2
        $data = $this->cleanData($data);
87
88
        $where = array(
89 2
            $dbAdapter->quoteIdentifier($options->getIdColumnName()) . ' = ?' => $nodeId,
90 2
        );
91 2
        $dbAdapter->update($options->getTableName(), $data, $where);
92 2
    }
93
94 12
    public function moveLeftIndexes($fromIndex, $shift, $scope=null)
95
    {
96 12
        $options = $this->getOptions();
97
98 12
        if (0 == $shift) {
99
            return;
100
        }
101
102 12
        $dbAdapter = $this->getDbAdapter();
103 12
        $sql = 'UPDATE ' . $dbAdapter->quoteIdentifier($options->getTableName())
104 12
            . ' SET ' . $dbAdapter->quoteIdentifier($options->getLeftColumnName())
105 12
                . ' = ' . $dbAdapter->quoteIdentifier($options->getLeftColumnName()) . ' + :shift'
106 12
            . ' WHERE ' . $dbAdapter->quoteIdentifier($options->getLeftColumnName()) . ' > :fromIndex';
107
108 12
        if ($options->getScopeColumnName()) {
109 3
            $sql .= ' AND '. $dbAdapter->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbAdapter->quote($scope);
110 3
        }
111
112
        $binds = array(
113 12
            ':shift' => $shift,
114 12
            ':fromIndex' => $fromIndex,
115 12
        );
116 12
        $dbAdapter->prepare($sql)->execute($binds);
117 12
    }
118
119 12
    public function moveRightIndexes($fromIndex, $shift, $scope=null)
120
    {
121 12
        $options = $this->getOptions();
122
123 12
        if (0 == $shift) {
124
            return;
125
        }
126
127 12
        $dbAdapter = $this->getDbAdapter();
128
129 12
        $sql = 'UPDATE ' . $dbAdapter->quoteIdentifier($options->getTableName())
130 12
            . ' SET ' . $dbAdapter->quoteIdentifier($options->getRightColumnName())
131 12
                . ' = ' . $dbAdapter->quoteIdentifier($options->getRightColumnName()) . ' + :shift'
132 12
            . ' WHERE ' . $dbAdapter->quoteIdentifier($options->getRightColumnName()) . ' > :fromIndex';
133
134 12
        if ($options->getScopeColumnName()) {
135 3
            $sql .= ' AND '. $dbAdapter->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbAdapter->quote($scope);
136 3
        }
137
138
        $binds = array(
139 12
            ':shift' => $shift,
140 12
            ':fromIndex' => $fromIndex,
141 12
        );
142
143 12
        $dbAdapter->prepare($sql)->execute($binds);
144 12
    }
145
146 4
    public function updateParentId($nodeId, $newParentId)
147
    {
148 4
        $options = $this->getOptions();
149
150 4
        $dbAdapter = $this->getDbAdapter();
151
152
        $bind = array(
153 4
            $options->getParentIdColumnName() => $newParentId,
154 4
        );
155
156
        $where = array(
157 4
            $dbAdapter->quoteIdentifier($options->getIdColumnName()) . ' = ?' => $nodeId,
158 4
        );
159 4
        $dbAdapter->update($options->getTableName(), $bind, $where);
160 4
    }
161
162 8
    public function getRoots($scope=null)
163
    {
164 8
        $options = $this->getOptions();
165
166 8
        $dbAdapter = $this->getDbAdapter();
167
168 8
        $select = $this->getDefaultDbSelect()
169 8
            ->where($options->getParentIdColumnName() . ' = ?', 0);
170
171 8
        if (null != $scope && $options->getScopeColumnName()) {
172 2
            $select->where($options->getScopeColumnName() . ' = ?', $scope);
173 2
        }
174
175 8
        return $dbAdapter->fetchAll($select);
176
    }
177
178 7
    public function getRoot($scope=null)
179
    {
180 7
        $result = $this->getRoots($scope);
181
182 7
        return ($result) ? $result[0] : array();
183
    }
184
185 22
    public function getNode($nodeId)
186
    {
187 22
        $options = $this->getOptions();
188
189 22
        $nodeId = (int) $nodeId;
190
191 22
        $dbAdapter = $this->getDbAdapter();
192
193 22
        $select = $this->getDefaultDbSelect()
194 22
                       ->where($options->getIdColumnName() . ' = ?', $nodeId);
195
196 22
        $row = $dbAdapter->fetchRow($select);
197 22
        return $row ? $row : null;
198
    }
199
200 20
    public function getNodeInfo($nodeId)
201
    {
202 20
        $options = $this->getOptions();
203 20
        $result = $this->getNode($nodeId);
204
205 20
        if (null == $result) {
206 6
            $result = null;
207 6
        } else {
208 19
            $id = $result[$options->getIdColumnName()];
209 19
            $parentId = $result[$options->getParentIdColumnName()];
210 19
            $level = $result[$options->getLevelColumnName()];
211 19
            $left = $result[$options->getLeftColumnName()];
212 19
            $right = $result[$options->getRightColumnName()];
213
214 19
            if (isset($result[$options->getScopeColumnName()])) {
215 6
                $scope = $result[$options->getScopeColumnName()];
216 6
            } else {
217 13
                $scope = null;
218
            }
219
220 19
            $result = new NodeInfo($id, $parentId, $level, $left, $right, $scope);
221
        }
222
223 20
        return $result;
224
    }
225
226
    /**
227
     * Return clone of default select
228
     *
229
     * @return \Zend_Db_Select
230
     */
231 33
    public function getDefaultDbSelect()
232
    {
233 33
        $options = $this->getOptions();
234
235 33
        if (null == $this->defaultDbSelect) {
236 32
            $this->defaultDbSelect = $this->dbAdapter->select()
237 32
                                          ->from($options->getTableName());
238 32
        }
239
240 33
        $dbSelect = clone $this->defaultDbSelect;
241
242 33
        return $dbSelect;
243
    }
244
245
    /**
246
     * @param \Zend_Db_Select $dbSelect
247
     * @return void
248
     */
249 1
    public function setDefaultDbSelect(\Zend_Db_Select $dbSelect)
250
    {
251 1
        $this->defaultDbSelect = $dbSelect;
252 1
    }
253
254 2
    private function cleanData(array $data)
255
    {
256 2
        $options = $this->getOptions();
257
258
        $disallowedDataKeys = array(
259 2
            $options->getIdColumnName(),
260 2
            $options->getLeftColumnName(),
261 2
            $options->getRightColumnName(),
262 2
            $options->getLevelColumnName(),
263 2
            $options->getParentIdColumnName(),
264 2
            $options->getScopeColumnName(),
265 2
        );
266
267 2
        return array_diff_key($data, array_flip($disallowedDataKeys));
268
    }
269
270 9
    public function insert(NodeInfo $nodeInfo, array $data)
271
    {
272 9
        $options = $this->getOptions();
273 9
        $dbAdapter = $this->getDbAdapter();
274
275 9
        $data[$options->getParentIdColumnName()] = $nodeInfo->getParentId();
276 9
        $data[$options->getLevelColumnName()] = $nodeInfo->getLevel();
277 9
        $data[$options->getLeftColumnName()] = $nodeInfo->getLeft();
278 9
        $data[$options->getRightColumnName()] = $nodeInfo->getRight();
279
280 9
        if ($options->getScopeColumnName()) {
281 3
            $data[$options->getScopeColumnName()] = $nodeInfo->getScope();
282 3
        }
283
284 9
        $dbAdapter->insert($options->getTableName(), $data);
285 9
        if ('' != $options->getSequenceName()) {
286 9
            $lastGeneratedValue = $dbAdapter->lastSequenceId($options->getSequenceName());
287 9
        } else {
288
            $lastGeneratedValue = $dbAdapter->lastInsertId();
289
        }
290
291 9
        return $lastGeneratedValue;
292
    }
293
294 2
    public function delete($leftIndex, $rightIndex, $scope=null)
295
    {
296 2
        $options = $this->getOptions();
297
298 2
        $dbAdapter = $this->getDbAdapter();
299
300
        $where = array(
301 2
            $dbAdapter->quoteIdentifier($options->getLeftColumnName()) . ' >= ?' => $leftIndex,
302 2
            $dbAdapter->quoteIdentifier($options->getRightColumnName()) . ' <= ?' => $rightIndex,
303 2
        );
304
305 2
        if ($options->getScopeColumnName()) {
306 1
            $where[$dbAdapter->quoteIdentifier($options->getScopeColumnName()) . ' = ?'] = $scope;
307 1
        }
308
309 2
        $dbAdapter->delete($options->getTableName(), $where);
310 2
    }
311
312 5
    public function updateLevels($leftIndexFrom, $rightIndexTo, $shift, $scope=null)
313
    {
314 5
        $options = $this->getOptions();
315
316 5
        if (0 == $shift) {
317 1
            return;
318
        }
319
320 4
        $dbAdapter = $this->getDbAdapter();
321
322 4
        $sql = 'UPDATE ' . $dbAdapter->quoteIdentifier($options->getTableName())
323 4
            . ' SET ' . $dbAdapter->quoteIdentifier($options->getLevelColumnName())
324 4
                . ' = ' . $dbAdapter->quoteIdentifier($options->getLevelColumnName()) . ' + :shift'
325 4
            . ' WHERE ' . $dbAdapter->quoteIdentifier($options->getLeftColumnName())
326 4
                . ' >= :leftFrom' . ' AND ' . $dbAdapter->quoteIdentifier($options->getRightColumnName())
327 4
                . ' <= :rightTo';
328
329 4
        if ($options->getScopeColumnName()) {
330
            $sql .= ' AND '. $dbAdapter->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbAdapter->quote($scope);
331
        }
332
333
        $binds = array(
334 4
            ':shift' => $shift,
335 4
            ':leftFrom' => $leftIndexFrom,
336 4
            ':rightTo' => $rightIndexTo,
337 4
        );
338
339 4
        $dbAdapter->prepare($sql)->execute($binds);
340 4
    }
341
342 5
    public function moveBranch($leftIndexFrom, $rightIndexTo, $shift, $scope=null)
343
    {
344 5
        if (0 == $shift) {
345
            return;
346
        }
347
348 5
        $options = $this->getOptions();
349
350 5
        $dbAdapter = $this->getDbAdapter();
351
352 5
        $sql = 'UPDATE ' . $dbAdapter->quoteIdentifier($options->getTableName())
353 5
            . ' SET ' . $dbAdapter->quoteIdentifier($options->getLeftColumnName())
354 5
                . ' = ' . $dbAdapter->quoteIdentifier($options->getLeftColumnName()) . ' + :shift, '
355 5
            . $dbAdapter->quoteIdentifier($options->getRightColumnName())
356 5
                . ' = ' . $dbAdapter->quoteIdentifier($options->getRightColumnName()) . ' + :shift'
357 5
            . ' WHERE ' . $dbAdapter->quoteIdentifier($options->getLeftColumnName()) . ' >= :leftFrom'
358 5
                . ' AND ' . $dbAdapter->quoteIdentifier($options->getRightColumnName()) . ' <= :rightTo';
359
360 5
        if ($options->getScopeColumnName()) {
361 1
            $sql .= ' AND '. $dbAdapter->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbAdapter->quote($scope);
362 1
        }
363
364
        $binds = array(
365 5
            ':shift' => $shift,
366 5
            ':leftFrom' => $leftIndexFrom,
367 5
            ':rightTo' => $rightIndexTo,
368 5
        );
369
370 5
        $dbAdapter->prepare($sql)->execute($binds);
371 5
    }
372
373 2
    public function getPath($nodeId, $startLevel = 0, $excludeLastNode = false)
374
    {
375 2
        $options = $this->getOptions();
376
377 2
        $startLevel = (int) $startLevel;
378
379
        // node does not exist
380 2
        if (!$nodeInfo = $this->getNodeInfo($nodeId)) {
381 1
            return;
382
        }
383
384 2
        $dbAdapter = $this->getDbAdapter();
385
386 2
        $select = $this->getDefaultDbSelect();
387 2
        $select->where(
388 2
            $dbAdapter->quoteIdentifier($options->getLeftColumnName()) . ' <= ?', $nodeInfo->getLeft()
389 2
        );
390 2
        $select->where(
391 2
            $dbAdapter->quoteIdentifier($options->getRightColumnName()) . ' >= ?', $nodeInfo->getRight()
392 2
        );
393 2
        $select->order($options->getLeftColumnName() . ' ASC');
394
395 2
        if (0 < $startLevel) {
396 1
            $select->where(
397 1
                $dbAdapter->quoteIdentifier($options->getLevelColumnName()) . ' >= ?', $startLevel
398 1
            );
399 1
        }
400
401 2
        if (true == $excludeLastNode) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
402 1
            $select->where(
403 1
                $dbAdapter->quoteIdentifier($options->getLevelColumnName()) . ' < ?', $nodeInfo->getLevel()
404 1
            );
405 1
        }
406
407 2
        $result = $dbAdapter->fetchAll($select);
408
409 2
        return $result;
410
    }
411
412 3
    public function getDescendants($nodeId = 1, $startLevel = 0, $levels = null, $excludeBranch = null)
413
    {
414 3
        $options = $this->getOptions();
415
416 3
        if (!$nodeInfo = $this->getNodeInfo($nodeId)) {
417 2
            return;
418
        }
419
420 3
        $dbAdapter = $this->getDbAdapter();
421 3
        $select = $this->getDefaultDbSelect();
422 3
        $select->order($options->getLeftColumnName() . ' ASC');
423
424 3
        if ($options->getScopeColumnName()) {
425 1
            $select->where($options->getScopeColumnName() . ' = ?', $nodeInfo->getScope());
426 1
        }
427
428 3
        if (0 != $startLevel) {
429 2
            $level = $nodeInfo->getLevel() + (int) $startLevel;
430 2
            $select->where(
431 2
                $dbAdapter->quoteIdentifier($options->getLevelColumnName()) . ' >= ?', $level
432 2
            );
433 2
        }
434
435 3
        if (null != $levels) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $levels of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
436 2
            $endLevel = $nodeInfo->getLevel() + (int) $startLevel + abs($levels);
437 2
            $select->where(
438 2
                $dbAdapter->quoteIdentifier($options->getLevelColumnName()) . ' < ?', $endLevel
439 2
            );
440 2
        }
441
442 3
        if (null != $excludeBranch && null != ($excludeNodeInfo = $this->getNodeInfo($excludeBranch))) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $excludeBranch of type integer|null against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
443 1
            $where = sprintf(
444 1
                "(%s OR %s) AND (%s OR %s)",
445 1
                $this->getWhereBetween($options->getLeftColumnName(), $nodeInfo->getLeft(), $excludeNodeInfo->getLeft() - 1),
446 1
                $this->getWhereBetween($options->getLeftColumnName(),
447 1
                    $excludeNodeInfo->getRight() + 1, $nodeInfo->getRight()),
448 1
                $this->getWhereBetween($options->getRightColumnName(),
449 1
                    $excludeNodeInfo->getRight() + 1, $nodeInfo->getRight()),
450 1
                $this->getWhereBetween($options->getRightColumnName(),
451 1
                    $nodeInfo->getLeft(), $excludeNodeInfo->getLeft() - 1)
452 1
            );
453 1
            $select->where($where);
454 1
        } else {
455 3
            $select->where(
456 3
                $dbAdapter->quoteIdentifier($options->getLeftColumnName()) . ' >= ?', $nodeInfo->getLeft()
457 3
            );
458 3
            $select->where(
459 3
                $dbAdapter->quoteIdentifier($options->getRightColumnName()) . ' <= ?', $nodeInfo->getRight()
460 3
            );
461
        }
462
463 3
        $resultArray = $dbAdapter->fetchAll($select);
464
465 3
        if (0 < count($resultArray)) {
466 3
            return $resultArray;
467
        }
468 1
    }
469
470 1
    protected function getWhereBetween($column, $first, $second)
471
    {
472 1
        $dbAdapter = $this->getDbAdapter();
473 1
        $quotedColumn = $dbAdapter->quoteIdentifier($column);
474 1
        $quotedFirst = $dbAdapter->quote($first);
475 1
        $quotedSecond = $dbAdapter->quote($second);
476 1
        return sprintf('(%s BETWEEN %s AND %s)', $quotedColumn, $quotedFirst, $quotedSecond);
477
    }
478
}
479