Completed
Push — master ( d81eee...5169df )
by Bartko
06:39
created

Zend2::moveBranch()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 3.0007

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 22
cts 23
cp 0.9565
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 23
nc 3
nop 4
crap 3.0007
1
<?php
2
namespace StefanoTree\NestedSet\Adapter;
3
4
use StefanoTree\NestedSet\NodeInfo;
5
use StefanoTree\NestedSet\Options;
6
use Zend\Db;
7
use Zend\Db\Adapter\Adapter as DbAdapter;
8
9
class Zend2
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
    private $options;
13
14
    private $dbAdapter;
15
16
    private $defaultDbSelect = null;
17
18 139
    public function __construct(Options $options, DbAdapter $dbAdapter)
19
    {
20 139
        $this->options = $options;
21 139
        $this->dbAdapter = $dbAdapter;
22 139
    }
23
24
    /**
25
     * @return Options
26
     */
27 132
    private function getOptions()
28
    {
29 132
        return $this->options;
30
    }
31
32
    /**
33
     * @return DbAdapter
34
     */
35 134
    protected function getDbAdapter()
36
    {
37 134
        return $this->dbAdapter;
38
    }
39
40
    /**
41
     * Data cannot contain keys like idColumnName, levelColumnName, ...
42
     *
43
     * @param array $data
44
     * @return array
45
     */
46 8
    private function cleanData(array $data)
47
    {
48 8
        $options = $this->getOptions();
49
50
        $disallowedDataKeys = array(
51 8
            $options->getIdColumnName(),
52 8
            $options->getLeftColumnName(),
53 8
            $options->getRightColumnName(),
54 8
            $options->getLevelColumnName(),
55 8
            $options->getParentIdColumnName(),
56 8
            $options->getScopeColumnName(),
57
        );
58
59 8
        return array_diff_key($data, array_flip($disallowedDataKeys));
60
    }
61
62
    /**
63
     * Return base db select without any join, etc.
64
     * @return Db\Sql\Select
65
     */
66 93
    public function getBlankDbSelect()
67
    {
68 93
        return new Db\Sql\Select($this->getOptions()->getTableName());
69
    }
70
71
    /**
72
     * @param Db\Sql\Select $dbSelect
73
     * @return void
74
     */
75 1
    public function setDefaultDbSelect(Db\Sql\Select $dbSelect)
76
    {
77 1
        $this->defaultDbSelect = $dbSelect;
78 1
    }
79
80
    /**
81
     * Return default db select
82
     * @return Db\Sql\Select
83
     */
84 36
    public function getDefaultDbSelect()
85
    {
86 36
        if (null === $this->defaultDbSelect) {
87 35
            $this->defaultDbSelect = $this->getBlankDbSelect();
88
        }
89
90 36
        $dbSelect = clone $this->defaultDbSelect;
91
92 36
        return $dbSelect;
93
    }
94
95 22
    public function lockTree()
96
    {
97 22
        $options = $this->getOptions();
98
99 22
        $dbAdapter = $this->getDbAdapter();
100
101 22
        $select = $this->getBlankDbSelect();
102 22
        $select->columns(array(
103 22
            'i' => $options->getIdColumnName(),
104
        ));
105
106 22
        $sql = $select->getSqlString($dbAdapter->getPlatform()) . ' FOR UPDATE';
107
108 22
        $dbAdapter->query($sql, DbAdapter::QUERY_MODE_EXECUTE);
109 22
    }
110
111 21
    public function beginTransaction()
112
    {
113 21
        $this->getDbAdapter()
114 21
             ->getDriver()
115 21
             ->getConnection()
116 21
             ->beginTransaction();
117 21
    }
118
119 17
    public function commitTransaction()
120
    {
121 17
        $this->getDbAdapter()
122 17
             ->getDriver()
123 17
             ->getConnection()
124 17
             ->commit();
125 17
    }
126
127 5
    public function rollbackTransaction()
128
    {
129 5
        $this->getDbAdapter()
130 5
             ->getDriver()
131 5
             ->getConnection()
132 5
             ->rollback();
133 5
    }
134
135 8
    public function update($nodeId, array $data)
136
    {
137 8
        $options = $this->getOptions();
138
139 8
        $dbAdapter = $this->getDbAdapter();
140
141 8
        $data = $this->cleanData($data);
142
143 8
        $update = new Db\Sql\Update($options->getTableName());
144 8
        $update->set($data)
145 8
               ->where(array(
146 8
                    $options->getIdColumnName() => $nodeId,
147
               ));
148
149 8
        $dbAdapter->query($update->getSqlString($dbAdapter->getPlatform()),
150 8
                DbAdapter::QUERY_MODE_EXECUTE);
151 8
    }
152
153 15
    public function insert(NodeInfo $nodeInfo, array $data)
154
    {
155 15
        $options = $this->getOptions();
156
157 15
        $dbAdapter = $this->getDbAdapter();
158
159 15
        $data[$options->getParentIdColumnName()] = $nodeInfo->getParentId();
160 15
        $data[$options->getLevelColumnName()]    = $nodeInfo->getLevel();
161 15
        $data[$options->getLeftColumnName()]     = $nodeInfo->getLeft();
162 15
        $data[$options->getRightColumnName()]    = $nodeInfo->getRight();
163
164 15
        if ($options->getScopeColumnName()) {
165 5
            $data[$options->getScopeColumnName()] = $nodeInfo->getScope();
166
        }
167
168 15
        $insert = new Db\Sql\Insert($options->getTableName());
169 15
        $insert->values($data);
170 15
        $dbAdapter->query($insert->getSqlString($dbAdapter->getPlatform()),
171 15
            DbAdapter::QUERY_MODE_EXECUTE);
172
173 15
        $lastGeneratedValue = $dbAdapter->getDriver()
174 15
                                        ->getLastGeneratedValue($options->getSequenceName());
0 ignored issues
show
Unused Code introduced by
The call to DriverInterface::getLastGeneratedValue() has too many arguments starting with $options->getSequenceName().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
175
176 15
        return $lastGeneratedValue;
177
    }
178
179 6
    public function delete($nodeId)
180
    {
181 6
        $options = $this->getOptions();
182
183 6
        $dbAdapter = $this->getDbAdapter();
184
185 6
        $delete = new Db\Sql\Delete($options->getTableName());
186 6
        $delete->where
187 6
               ->equalTo($options->getIdColumnName(), $nodeId);
188
189 6
        $dbAdapter->query($delete->getSqlString($dbAdapter->getPlatform()),
190 6
            DbAdapter::QUERY_MODE_EXECUTE);
191 6
    }
192
193 16
    public function moveLeftIndexes($fromIndex, $shift, $scope = null)
194
    {
195 16
        $options = $this->getOptions();
196
197 16
        if (0 == $shift) {
198
            return null;
199
        }
200
201 16
        $dbAdapter = $this->getDbAdapter();
202 16
        $dbPlatform = $dbAdapter->getPlatform();
203
204 16
        $sql = 'UPDATE ' . $dbPlatform->quoteIdentifier($options->getTableName())
205 16
                . ' SET '
206 16
                    . $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' = '
207 16
                        . $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' + :shift'
208 16
                . ' WHERE '
209 16
                    . $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' > :fromIndex';
210
211 16
        if ($options->getScopeColumnName()) {
212 5
            $sql .= ' AND ' . $dbPlatform->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbPlatform->quoteValue($scope);
213
        }
214
215
        $binds = array(
216 16
            ':shift' => $shift,
217 16
            ':fromIndex' => $fromIndex,
218
        );
219
220 16
        $dbAdapter->query($sql)
0 ignored issues
show
Bug introduced by
The method execute does only exist in Zend\Db\Adapter\Driver\StatementInterface, but not in Zend\Db\Adapter\Driver\R...tSet\ResultSetInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
221 16
                  ->execute($binds);
222 16
    }
223
224 16
    public function moveRightIndexes($fromIndex, $shift, $scope = null)
225
    {
226 16
        $options = $this->getOptions();
227
228 16
        if (0 == $shift) {
229
            return null;
230
        }
231
232 16
        $dbAdapter = $this->getDbAdapter();
233 16
        $dbPlatform = $dbAdapter->getPlatform();
234
235 16
        $sql = 'UPDATE ' . $dbPlatform->quoteIdentifier($options->getTableName())
236 16
                . ' SET '
237 16
                    . $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' = '
238 16
                        . $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' + :shift'
239 16
                . ' WHERE '
240 16
                    . $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' > :fromIndex';
241
242 16
        if ($options->getScopeColumnName()) {
243 5
            $sql .= ' AND ' . $dbPlatform->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbPlatform->quoteValue($scope);
244
        }
245
246
        $binds = array(
247 16
            ':shift' => $shift,
248 16
            ':fromIndex' => $fromIndex,
249
        );
250
251 16
        $dbAdapter->query($sql)
0 ignored issues
show
Bug introduced by
The method execute does only exist in Zend\Db\Adapter\Driver\StatementInterface, but not in Zend\Db\Adapter\Driver\R...tSet\ResultSetInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
252 16
                  ->execute($binds);
253 16
    }
254
255 6
    public function updateParentId($nodeId, $newParentId)
256
    {
257 6
        $options = $this->getOptions();
258
259 6
        $dbAdapter = $this->getDbAdapter();
260
261 6
        $update = new Db\Sql\Update($options->getTableName());
262 6
        $update->set(array(
263 6
                    $options->getParentIdColumnName() => $newParentId,
264
               ))
265 6
               ->where(array(
266 6
                   $options->getIdColumnName() => $nodeId,
267
               ));
268
269 6
        $dbAdapter->query($update->getSqlString($dbAdapter->getPlatform()),
270 6
            DbAdapter::QUERY_MODE_EXECUTE);
271 6
    }
272
273 9
    public function updateLevels($leftIndexFrom, $rightIndexTo, $shift, $scope = null)
274
    {
275 9
        $options = $this->getOptions();
276
277 9
        if (0 == $shift) {
278 1
            return null;
279
        }
280
281 8
        $dbAdapter = $this->getDbAdapter();
282 8
        $dbPlatform = $dbAdapter->getPlatform();
283
284 8
        $sql = 'UPDATE ' . $dbPlatform->quoteIdentifier($options->getTableName())
285 8
            . ' SET '
286 8
                . $dbPlatform->quoteIdentifier($options->getLevelColumnName()) . ' = '
287 8
                    . $dbPlatform->quoteIdentifier($options->getLevelColumnName()) . ' + :shift'
288 8
            . ' WHERE '
289 8
                . $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' >= :leftFrom'
290 8
                . ' AND ' . $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' <= :rightTo';
291
292 8
        if ($options->getScopeColumnName()) {
293 2
            $sql .= ' AND ' . $dbPlatform->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbPlatform->quoteValue($scope);
294
        }
295
296
        $binds = array(
297 8
            ':shift' => $shift,
298 8
            ':leftFrom' => $leftIndexFrom,
299 8
            ':rightTo' => $rightIndexTo,
300
        );
301
302 8
        $dbAdapter->query($sql)
0 ignored issues
show
Bug introduced by
The method execute does only exist in Zend\Db\Adapter\Driver\StatementInterface, but not in Zend\Db\Adapter\Driver\R...tSet\ResultSetInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
303 8
                  ->execute($binds);
304 8
    }
305
306 9
    public function moveBranch($leftIndexFrom, $rightIndexTo, $shift, $scope = null)
307
    {
308 9
        if (0 == $shift) {
309
            return null;
310
        }
311
312 9
        $options = $this->getOptions();
313
314 9
        $dbAdapter = $this->getDbAdapter();
315 9
        $dbPlatform = $dbAdapter->getPlatform();
316
317 9
        $sql = 'UPDATE ' . $dbPlatform->quoteIdentifier($options->getTableName())
318 9
            . ' SET '
319 9
                . $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' = '
320 9
                    . $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' + :shift, '
321 9
                . $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' = '
322 9
                    . $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' + :shift'
323 9
            . ' WHERE '
324 9
                . $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' >= :leftFrom'
325 9
                . ' AND ' . $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' <= :rightTo';
326
327 9
        if ($options->getScopeColumnName()) {
328 3
            $sql .= ' AND ' . $dbPlatform->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbPlatform->quoteValue($scope);
329
        }
330
331
        $binds = array(
332 9
            ':shift' => $shift,
333 9
            ':leftFrom' => $leftIndexFrom,
334 9
            ':rightTo' => $rightIndexTo,
335
        );
336
337 9
        $dbAdapter->query($sql)
0 ignored issues
show
Bug introduced by
The method execute does only exist in Zend\Db\Adapter\Driver\StatementInterface, but not in Zend\Db\Adapter\Driver\R...tSet\ResultSetInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
338 9
                  ->execute($binds);
339 9
    }
340
341 16
    public function getRoots($scope = null)
342
    {
343 16
        $options = $this->getOptions();
344
345 16
        $dbAdapter = $this->getDbAdapter();
346
347 16
        $select = $this->getBlankDbSelect();
348 16
        $select->where
349 16
            ->isNull($options->getParentIdColumnName());
350 16
        $select->order($options->getIdColumnName());
351
352 16
        if (null != $scope && $options->getScopeColumnName()) {
353 4
            $select->where
354 4
                ->equalTo($options->getScopeColumnName(), $scope);
355
        }
356
357 16
        $result = $dbAdapter->query($select->getSqlString($dbAdapter->getPlatform()),
358 16
            DbAdapter::QUERY_MODE_EXECUTE);
359
360 16
        return $result->toArray();
361
    }
362
363 11
    public function getRoot($scope = null)
364
    {
365 11
        $roots = $this->getRoots($scope);
366
367 11
        return (0 < count($roots)) ?  $roots[0] : array();
368
    }
369
370 6
    public function getNode($nodeId)
371
    {
372 6
        $options = $this->getOptions();
373
374 6
        $nodeId = (int) $nodeId;
375
376 6
        $dbAdapter = $this->getDbAdapter();
377
378 6
        $select = $this->getDefaultDbSelect()
379 6
                       ->where(array($options->getIdColumnName() =>  $nodeId));
380
381 6
        $result = $dbAdapter->query($select->getSqlString($dbAdapter->getPlatform()),
382 6
                DbAdapter::QUERY_MODE_EXECUTE);
383
384 6
        $array = $result->toArray();
385
386 6
        return (0 < count($array)) ? $array[0] : null;
387
    }
388
389
    /**
390
     * @param array $data
391
     * @return NodeInfo
392
     */
393 55
    private function _buildNodeInfoObject(array $data)
394
    {
395 55
        $options = $this->getOptions();
396
397 55
        $id        = $data[$options->getIdColumnName()];
398 55
        $parentId  = $data[$options->getParentIdColumnName()];
399 55
        $level     = $data[$options->getLevelColumnName()];
400 55
        $left      = $data[$options->getLeftColumnName()];
401 55
        $right     = $data[$options->getRightColumnName()];
402
403 55
        if (isset($data[$options->getScopeColumnName()])) {
404 19
            $scope = $data[$options->getScopeColumnName()];
405
        } else {
406 36
            $scope = null;
407
        }
408
409 55
        return new NodeInfo($id, $parentId, $level, $left, $right, $scope);
410
    }
411
412 61
    public function getNodeInfo($nodeId)
413
    {
414 61
        $options = $this->getOptions();
415
416 61
        $nodeId = (int) $nodeId;
417
418 61
        $dbAdapter = $this->getDbAdapter();
419
420 61
        $select = $this->getBlankDbSelect()
421 61
            ->where(array($options->getIdColumnName() =>  $nodeId));
422
423 61
        $result = $dbAdapter->query($select->getSqlString($dbAdapter->getPlatform()),
424 61
            DbAdapter::QUERY_MODE_EXECUTE);
425
426 61
        $array = $result->toArray();
427
428 61
        $result = ($array) ? $this->_buildNodeInfoObject($array[0]) : null;
429
430 61
        return $result;
431
    }
432
433 9
    public function getChildrenNodeInfo($parentNodeId)
434
    {
435 9
        $dbAdapter = $this->getDbAdapter();
436 9
        $options = $this->getOptions();
437
438
        $columns = array(
439 9
            $options->getIdColumnName(),
440 9
            $options->getLeftColumnName(),
441 9
            $options->getRightColumnName(),
442 9
            $options->getParentIdColumnName(),
443 9
            $options->getLevelColumnName(),
444
        );
445
446 9
        if ($options->getScopeColumnName()) {
447 5
            $columns[] = $options->getScopeColumnName();
448
        }
449
450 9
        $select = $this->getBlankDbSelect();
451 9
        $select->columns($columns);
452 9
        $select->order($options->getLeftColumnName());
453 9
        $select->where(array(
454 9
            $options->getParentIdColumnName() => $parentNodeId
455
        ));
456
457 9
        $data = $dbAdapter->query($select->getSqlString($dbAdapter->getPlatform()),
458 9
            DbAdapter::QUERY_MODE_EXECUTE);
459
460 9
        $result = array();
461
462 9
        foreach ($data->toArray() as $nodeData) {
463 7
            $result[] = $this->_buildNodeInfoObject($nodeData);
464
        }
465
466 9
        return $result;
467
    }
468
469 5
    public function updateNodeMetadata(NodeInfo $nodeInfo)
470
    {
471 5
        $dbAdapter = $this->getDbAdapter();
472 5
        $options = $this->getOptions();
473
474 5
        $update = new Db\Sql\Update($options->getTableName());
475
476 5
        $update->set(array(
477 5
            $options->getRightColumnName() => $nodeInfo->getRight(),
478 5
            $options->getLeftColumnName() => $nodeInfo->getLeft(),
479 5
            $options->getLevelColumnName() => $nodeInfo->getLevel(),
480
        ));
481
482 5
        $update->where(array(
483 5
            $options->getIdColumnName() => $nodeInfo->getId(),
484
        ));
485
486 5
        $dbAdapter->query($update->getSqlString($dbAdapter->getPlatform()),
487 5
            DbAdapter::QUERY_MODE_EXECUTE);
488 5
    }
489
490 14
    public function getPath($nodeId, $startLevel = 0, $excludeLastNode = false)
491
    {
492 14
        $options = $this->getOptions();
493
494 14
        $startLevel = (int) $startLevel;
495
496
        // node does not exist
497 14
        $nodeInfo = $this->getNodeInfo($nodeId);
498 14
        if (!$nodeInfo) {
499 3
            return array();
500
        }
501
502 11
        $dbAdapter = $this->getDbAdapter();
503
504 11
        $select = $this->getDefaultDbSelect();
505
506 11
        if ($options->getScopeColumnName()) {
507 3
            $select->where
508 3
                ->equalTo($options->getScopeColumnName(), $nodeInfo->getScope());
509
        }
510
511 11
        $select->where
512 11
               ->lessThanOrEqualTo($options->getLeftColumnName(), $nodeInfo->getLeft())
513 11
               ->AND
514 11
               ->greaterThanOrEqualTo($options->getRightColumnName(), $nodeInfo->getRight());
515
516 11
        $select->order($options->getLeftColumnName() . ' ASC');
517
518 11
        if (0 < $startLevel) {
519 3
            $select->where
520 3
                   ->greaterThanOrEqualTo($options->getLevelColumnName(), $startLevel);
521
        }
522
523 11
        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...
524 4
            $select->where
525 4
                   ->lessThan($options->getLevelColumnName(), $nodeInfo->getLevel());
526
        }
527
528 11
        $result = $dbAdapter->query($select->getSqlString($dbAdapter->getPlatform()),
529 11
            DbAdapter::QUERY_MODE_EXECUTE);
530
531 11
        return $result->toArray();
532
    }
533
534 21
    public function getDescendants($nodeId = 1, $startLevel = 0, $levels = null, $excludeBranch = null)
535
    {
536 21
        $options = $this->getOptions();
537
538 21
        if (!$nodeInfo = $this->getNodeInfo($nodeId)) {
539 4
            return array();
540
        }
541
542 17
        $dbAdapter = $this->getDbAdapter();
543 17
        $select = $this->getDefaultDbSelect();
544 17
        $select->order($options->getLeftColumnName() . ' ASC');
545
546 17
        if ($options->getScopeColumnName()) {
547 3
            $select->where
548 3
                   ->equalTo($options->getScopeColumnName(), $nodeInfo->getScope());
549
        }
550
551 17
        if (0 != $startLevel) {
552 8
            $level = $nodeInfo->getLevel() + (int) $startLevel;
553 8
            $select->where
554 8
                   ->greaterThanOrEqualTo($options->getLevelColumnName(), $level);
555
        }
556
557 17
        if (null != $levels) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $levels of type null|integer against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
558 5
            $endLevel = $nodeInfo->getLevel() + (int) $startLevel + abs($levels);
559 5
            $select->where
560 5
                   ->lessThan($options->getLevelColumnName(), $endLevel);
561
        }
562
563 17
        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 null|integer against null; this is ambiguous if the integer can be zero. Consider using a strict comparison !== instead.
Loading history...
564 3
            $select->where
565 3
                   ->NEST
566 3
                   ->between($options->getLeftColumnName(),
567 3
                        $nodeInfo->getLeft(), $excludeNodeInfo->getLeft() - 1)
568 3
                   ->OR
569 3
                   ->between($options->getLeftColumnName(),
570 3
                        $excludeNodeInfo->getRight() + 1, $nodeInfo->getRight())
571 3
                   ->UNNEST
572 3
                   ->AND
573 3
                   ->NEST
574 3
                   ->between($options->getRightColumnName(),
575 3
                        $excludeNodeInfo->getRight() + 1, $nodeInfo->getRight())
576 3
                   ->OR
577 3
                   ->between($options->getRightColumnName(),
578 3
                        $nodeInfo->getLeft(), $excludeNodeInfo->getLeft() - 1)
579 3
                   ->UNNEST;
580
        } else {
581 15
            $select->where
582 15
                   ->greaterThanOrEqualTo($options->getLeftColumnName(), $nodeInfo->getLeft())
583 15
                   ->AND
584 15
                   ->lessThanOrEqualTo($options->getRightColumnName(), $nodeInfo->getRight());
585
        }
586
587 17
        $result =  $dbAdapter->query($select->getSqlString($dbAdapter->getPlatform()),
588 17
            DbAdapter::QUERY_MODE_EXECUTE);
589
590 17
        $resultArray = $result->toArray();
591
592 17
        return (0 < count($resultArray)) ? $resultArray : array();
593
    }
594
}
595