Completed
Push — develop ( d0fbe3...729812 )
by Bartko
14:43
created

Zend1DbAdapter::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace StefanoTree\NestedSet\Adapter;
3
4
use StefanoLockTable\Adapter\AdapterInterface as LockSqlBuilderInterface;
5
use StefanoLockTable\Factory as LockSqlBuilderFactory;
6
use StefanoTree\NestedSet\NodeInfo;
7
use StefanoTree\NestedSet\Options;
8
use Zend_Db_Adapter_Abstract as ZendDbAdapter;
9
10
class Zend1DbAdapter
11
    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...
12
    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...
13
{
14
    protected $options;
15
16
    protected $dbAdapter;
17
18
    protected $defaultDbSelect;
19
20
    protected $lockSqlBuilder;
21
22 35
    public function __construct(Options $options, ZendDbAdapter $dbAdapter)
23
    {
24 35
        $this->options = $options;
25 35
        $this->dbAdapter = $dbAdapter;
26 35
    }
27
28
    /**
29
     * @return Options
30
     */
31 34
    private function getOptions()
32
    {
33 34
        return $this->options;
34
    }
35
36
    /**
37
     * @return ZendDbAdapter
38
     */
39 31
    public function getDbAdapter()
40
    {
41 31
        return $this->dbAdapter;
42
    }
43
44
    /**
45
     * @return LockSqlBuilderInterface
46
     */
47 15
    private function getLockSqlBuilder()
48
    {
49 15
        if (null == $this->lockSqlBuilder) {
50 15
            $adapterClassName = get_class($this->getDbAdapter());
51 15
            $parts = explode('_', $adapterClassName);
52 15
            $vendorName = end($parts);
53
54 15
            $factory = new LockSqlBuilderFactory();
55 15
            $this->lockSqlBuilder = $factory->createAdapter($vendorName);
56 15
        }
57
58 15
        return $this->lockSqlBuilder;
59
    }
60
61 15
    public function lockTable()
62
    {
63 15
        $options = $this->getOptions();
64 15
        $sql = $this->getLockSqlBuilder()->getLockSqlString($options->getTableName());
65
66 15
        if (null != $sql) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $sql of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
67 15
            $this->getDbAdapter()->query($sql);
68 15
        }
69 15
    }
70
71 15
    public function unlockTable()
72
    {
73 15
        $sql = $this->getLockSqlBuilder()->getUnlockSqlString();
74
75 15
        if (null != $sql) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $sql of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
76 15
            $this->getDbAdapter()->query($sql);
77 15
        }
78 15
    }
79
80 15
    protected function _isInTransaction()
81
    {
82 15
        return $this->getDbAdapter()
83 15
                    ->getConnection()
84 15
                    ->inTransaction();
85
    }
86
87 15
    protected function _beginTransaction()
88
    {
89 15
        $this->getDbAdapter()->beginTransaction();
90 15
    }
91
92 14
    protected function _commitTransaction()
93
    {
94 14
        $this->getDbAdapter()->commit();
95 14
    }
96
97 1
    protected function _rollbackTransaction()
98
    {
99 1
        $this->getDbAdapter()->rollBack();
100 1
    }
101
102 2
    public function update($nodeId, array $data, NodeInfo $nodeInfo = null)
103
    {
104 2
        $options = $this->getOptions();
105
106 2
        $dbAdapter = $this->getDbAdapter();
107
108 2
        $data = $this->cleanData($data);
109
110
        $where = array(
111 2
            $dbAdapter->quoteIdentifier($options->getIdColumnName()) . ' = ?' => $nodeId,
112 2
        );
113 2
        $dbAdapter->update($options->getTableName(), $data, $where);
114 2
    }
115
116 12
    public function moveLeftIndexes($fromIndex, $shift, $scope=null)
117
    {
118 12
        $options = $this->getOptions();
119
120 12
        if (0 == $shift) {
121
            return;
122
        }
123
124 12
        $dbAdapter = $this->getDbAdapter();
125 12
        $sql = 'UPDATE ' . $dbAdapter->quoteIdentifier($options->getTableName())
126 12
            . ' SET ' . $dbAdapter->quoteIdentifier($options->getLeftColumnName())
127 12
                . ' = ' . $dbAdapter->quoteIdentifier($options->getLeftColumnName()) . ' + :shift'
128 12
            . ' WHERE ' . $dbAdapter->quoteIdentifier($options->getLeftColumnName()) . ' > :fromIndex';
129
130 12
        if ($options->getScopeColumnName()) {
131 3
            $sql .= ' AND '. $dbAdapter->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbAdapter->quote($scope);
132 3
        }
133
134
        $binds = array(
135 12
            ':shift' => $shift,
136 12
            ':fromIndex' => $fromIndex,
137 12
        );
138 12
        $dbAdapter->prepare($sql)->execute($binds);
139 12
    }
140
141 12
    public function moveRightIndexes($fromIndex, $shift, $scope=null)
142
    {
143 12
        $options = $this->getOptions();
144
145 12
        if (0 == $shift) {
146
            return;
147
        }
148
149 12
        $dbAdapter = $this->getDbAdapter();
150
151 12
        $sql = 'UPDATE ' . $dbAdapter->quoteIdentifier($options->getTableName())
152 12
            . ' SET ' . $dbAdapter->quoteIdentifier($options->getRightColumnName())
153 12
                . ' = ' . $dbAdapter->quoteIdentifier($options->getRightColumnName()) . ' + :shift'
154 12
            . ' WHERE ' . $dbAdapter->quoteIdentifier($options->getRightColumnName()) . ' > :fromIndex';
155
156 12
        if ($options->getScopeColumnName()) {
157 3
            $sql .= ' AND '. $dbAdapter->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbAdapter->quote($scope);
158 3
        }
159
160
        $binds = array(
161 12
            ':shift' => $shift,
162 12
            ':fromIndex' => $fromIndex,
163 12
        );
164
165 12
        $dbAdapter->prepare($sql)->execute($binds);
166 12
    }
167
168 4
    public function updateParentId($nodeId, $newParentId)
169
    {
170 4
        $options = $this->getOptions();
171
172 4
        $dbAdapter = $this->getDbAdapter();
173
174
        $bind = array(
175 4
            $options->getParentIdColumnName() => $newParentId,
176 4
        );
177
178
        $where = array(
179 4
            $dbAdapter->quoteIdentifier($options->getIdColumnName()) . ' = ?' => $nodeId,
180 4
        );
181 4
        $dbAdapter->update($options->getTableName(), $bind, $where);
182 4
    }
183
184 8
    public function getRoots($scope=null)
185
    {
186 8
        $options = $this->getOptions();
187
188 8
        $dbAdapter = $this->getDbAdapter();
189
190 8
        $select = $this->getDefaultDbSelect()
191 8
            ->where($options->getParentIdColumnName() . ' = ?', 0);
192
193 8
        if (null != $scope && $options->getScopeColumnName()) {
194 2
            $select->where($options->getScopeColumnName() . ' = ?', $scope);
195 2
        }
196
197 8
        return $dbAdapter->fetchAll($select);
198
    }
199
200 7
    public function getRoot($scope=null)
201
    {
202 7
        $result = $this->getRoots($scope);
203
204 7
        return ($result) ? $result[0] : array();
205
    }
206
207 22
    public function getNode($nodeId)
208
    {
209 22
        $options = $this->getOptions();
210
211 22
        $nodeId = (int) $nodeId;
212
213 22
        $dbAdapter = $this->getDbAdapter();
214
215 22
        $select = $this->getDefaultDbSelect()
216 22
                       ->where($options->getIdColumnName() . ' = ?', $nodeId);
217
218 22
        $row = $dbAdapter->fetchRow($select);
219 22
        return $row ? $row : null;
220
    }
221
222 20
    public function getNodeInfo($nodeId)
223
    {
224 20
        $options = $this->getOptions();
225 20
        $result = $this->getNode($nodeId);
226
227 20
        if (null == $result) {
228 6
            $result = null;
229 6
        } else {
230 19
            $id = $result[$options->getIdColumnName()];
231 19
            $parentId = $result[$options->getParentIdColumnName()];
232 19
            $level = $result[$options->getLevelColumnName()];
233 19
            $left = $result[$options->getLeftColumnName()];
234 19
            $right = $result[$options->getRightColumnName()];
235
236 19
            if (isset($result[$options->getScopeColumnName()])) {
237 6
                $scope = $result[$options->getScopeColumnName()];
238 6
            } else {
239 13
                $scope = null;
240
            }
241
242 19
            $result = new NodeInfo($id, $parentId, $level, $left, $right, $scope);
243
        }
244
245 20
        return $result;
246
    }
247
248
    /**
249
     * Return clone of default select
250
     *
251
     * @return \Zend_Db_Select
252
     */
253 33
    public function getDefaultDbSelect()
254
    {
255 33
        $options = $this->getOptions();
256
257 33
        if (null == $this->defaultDbSelect) {
258 32
            $this->defaultDbSelect = $this->dbAdapter->select()
259 32
                                          ->from($options->getTableName());
260 32
        }
261
262 33
        $dbSelect = clone $this->defaultDbSelect;
263
264 33
        return $dbSelect;
265
    }
266
267
    /**
268
     * @param \Zend_Db_Select $dbSelect
269
     * @return void
270
     */
271 1
    public function setDefaultDbSelect(\Zend_Db_Select $dbSelect)
272
    {
273 1
        $this->defaultDbSelect = $dbSelect;
274 1
    }
275
276 2
    private function cleanData(array $data)
277
    {
278 2
        $options = $this->getOptions();
279
280
        $disallowedDataKeys = array(
281 2
            $options->getIdColumnName(),
282 2
            $options->getLeftColumnName(),
283 2
            $options->getRightColumnName(),
284 2
            $options->getLevelColumnName(),
285 2
            $options->getParentIdColumnName(),
286 2
            $options->getScopeColumnName(),
287 2
        );
288
289 2
        return array_diff_key($data, array_flip($disallowedDataKeys));
290
    }
291
292 9
    public function insert(NodeInfo $nodeInfo, array $data)
293
    {
294 9
        $options = $this->getOptions();
295 9
        $dbAdapter = $this->getDbAdapter();
296
297 9
        $data[$options->getParentIdColumnName()] = $nodeInfo->getParentId();
298 9
        $data[$options->getLevelColumnName()] = $nodeInfo->getLevel();
299 9
        $data[$options->getLeftColumnName()] = $nodeInfo->getLeft();
300 9
        $data[$options->getRightColumnName()] = $nodeInfo->getRight();
301
302 9
        if ($options->getScopeColumnName()) {
303 3
            $data[$options->getScopeColumnName()] = $nodeInfo->getScope();
304 3
        }
305
306 9
        $dbAdapter->insert($options->getTableName(), $data);
307 9
        if ('' != $options->getSequenceName()) {
308
            $lastGeneratedValue = $dbAdapter->lastSequenceId($options->getSequenceName());
309
        } else {
310 9
            $lastGeneratedValue = $dbAdapter->lastInsertId();
311
        }
312
313 9
        return $lastGeneratedValue;
314
    }
315
316 2
    public function delete($leftIndex, $rightIndex, $scope=null)
317
    {
318 2
        $options = $this->getOptions();
319
320 2
        $dbAdapter = $this->getDbAdapter();
321
322
        $where = array(
323 2
            $dbAdapter->quoteIdentifier($options->getLeftColumnName()) . ' >= ?' => $leftIndex,
324 2
            $dbAdapter->quoteIdentifier($options->getRightColumnName()) . ' <= ?' => $rightIndex,
325 2
        );
326
327 2
        if ($options->getScopeColumnName()) {
328 1
            $where[$dbAdapter->quoteIdentifier($options->getScopeColumnName()) . ' = ?'] = $scope;
329 1
        }
330
331 2
        $dbAdapter->delete($options->getTableName(), $where);
332 2
    }
333
334 5
    public function updateLevels($leftIndexFrom, $rightIndexTo, $shift, $scope=null)
335
    {
336 5
        $options = $this->getOptions();
337
338 5
        if (0 == $shift) {
339 1
            return;
340
        }
341
342 4
        $dbAdapter = $this->getDbAdapter();
343
344 4
        $sql = 'UPDATE ' . $dbAdapter->quoteIdentifier($options->getTableName())
345 4
            . ' SET ' . $dbAdapter->quoteIdentifier($options->getLevelColumnName())
346 4
                . ' = ' . $dbAdapter->quoteIdentifier($options->getLevelColumnName()) . ' + :shift'
347 4
            . ' WHERE ' . $dbAdapter->quoteIdentifier($options->getLeftColumnName())
348 4
                . ' >= :leftFrom' . ' AND ' . $dbAdapter->quoteIdentifier($options->getRightColumnName())
349 4
                . ' <= :rightTo';
350
351 4
        if ($options->getScopeColumnName()) {
352
            $sql .= ' AND '. $dbAdapter->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbAdapter->quote($scope);
353
        }
354
355
        $binds = array(
356 4
            ':shift' => $shift,
357 4
            ':leftFrom' => $leftIndexFrom,
358 4
            ':rightTo' => $rightIndexTo,
359 4
        );
360
361 4
        $dbAdapter->prepare($sql)->execute($binds);
362 4
    }
363
364 5
    public function moveBranch($leftIndexFrom, $rightIndexTo, $shift, $scope=null)
365
    {
366 5
        if (0 == $shift) {
367
            return;
368
        }
369
370 5
        $options = $this->getOptions();
371
372 5
        $dbAdapter = $this->getDbAdapter();
373
374 5
        $sql = 'UPDATE ' . $dbAdapter->quoteIdentifier($options->getTableName())
375 5
            . ' SET ' . $dbAdapter->quoteIdentifier($options->getLeftColumnName())
376 5
                . ' = ' . $dbAdapter->quoteIdentifier($options->getLeftColumnName()) . ' + :shift, '
377 5
            . $dbAdapter->quoteIdentifier($options->getRightColumnName())
378 5
                . ' = ' . $dbAdapter->quoteIdentifier($options->getRightColumnName()) . ' + :shift'
379 5
            . ' WHERE ' . $dbAdapter->quoteIdentifier($options->getLeftColumnName()) . ' >= :leftFrom'
380 5
                . ' AND ' . $dbAdapter->quoteIdentifier($options->getRightColumnName()) . ' <= :rightTo';
381
382 5
        if ($options->getScopeColumnName()) {
383 1
            $sql .= ' AND '. $dbAdapter->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbAdapter->quote($scope);
384 1
        }
385
386
        $binds = array(
387 5
            ':shift' => $shift,
388 5
            ':leftFrom' => $leftIndexFrom,
389 5
            ':rightTo' => $rightIndexTo,
390 5
        );
391
392 5
        $dbAdapter->prepare($sql)->execute($binds);
393 5
    }
394
395 2
    public function getPath($nodeId, $startLevel = 0, $excludeLastNode = false)
396
    {
397 2
        $options = $this->getOptions();
398
399 2
        $startLevel = (int) $startLevel;
400
401
        // node does not exist
402 2
        if (!$nodeInfo = $this->getNodeInfo($nodeId)) {
403 1
            return;
404
        }
405
406 2
        $dbAdapter = $this->getDbAdapter();
407
408 2
        $select = $this->getDefaultDbSelect();
409 2
        $select->where(
410 2
            $dbAdapter->quoteIdentifier($options->getLeftColumnName()) . ' <= ?', $nodeInfo->getLeft()
411 2
        );
412 2
        $select->where(
413 2
            $dbAdapter->quoteIdentifier($options->getRightColumnName()) . ' >= ?', $nodeInfo->getRight()
414 2
        );
415 2
        $select->order($options->getLeftColumnName() . ' ASC');
416
417 2
        if (0 < $startLevel) {
418 1
            $select->where(
419 1
                $dbAdapter->quoteIdentifier($options->getLevelColumnName()) . ' >= ?', $startLevel
420 1
            );
421 1
        }
422
423 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...
424 1
            $select->where(
425 1
                $dbAdapter->quoteIdentifier($options->getLevelColumnName()) . ' < ?', $nodeInfo->getLevel()
426 1
            );
427 1
        }
428
429 2
        $result = $dbAdapter->fetchAll($select);
430
431 2
        return $result;
432
    }
433
434 3
    public function getDescendants($nodeId = 1, $startLevel = 0, $levels = null, $excludeBranch = null)
435
    {
436 3
        $options = $this->getOptions();
437
438 3
        if (!$nodeInfo = $this->getNodeInfo($nodeId)) {
439 2
            return;
440
        }
441
442 3
        $dbAdapter = $this->getDbAdapter();
443 3
        $select = $this->getDefaultDbSelect();
444 3
        $select->order($options->getLeftColumnName() . ' ASC');
445
446 3
        if ($options->getScopeColumnName()) {
447 1
            $select->where($options->getScopeColumnName() . ' = ?', $nodeInfo->getScope());
448 1
        }
449
450 3
        if (0 != $startLevel) {
451 2
            $level = $nodeInfo->getLevel() + (int) $startLevel;
452 2
            $select->where(
453 2
                $dbAdapter->quoteIdentifier($options->getLevelColumnName()) . ' >= ?', $level
454 2
            );
455 2
        }
456
457 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...
458 2
            $endLevel = $nodeInfo->getLevel() + (int) $startLevel + abs($levels);
459 2
            $select->where(
460 2
                $dbAdapter->quoteIdentifier($options->getLevelColumnName()) . ' < ?', $endLevel
461 2
            );
462 2
        }
463
464 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...
465 1
            $where = sprintf(
466 1
                "(%s OR %s) AND (%s OR %s)",
467 1
                $this->getWhereBetween($options->getLeftColumnName(), $nodeInfo->getLeft(), $excludeNodeInfo->getLeft() - 1),
468 1
                $this->getWhereBetween($options->getLeftColumnName(),
469 1
                    $excludeNodeInfo->getRight() + 1, $nodeInfo->getRight()),
470 1
                $this->getWhereBetween($options->getRightColumnName(),
471 1
                    $excludeNodeInfo->getRight() + 1, $nodeInfo->getRight()),
472 1
                $this->getWhereBetween($options->getRightColumnName(),
473 1
                    $nodeInfo->getLeft(), $excludeNodeInfo->getLeft() - 1)
474 1
            );
475 1
            $select->where($where);
476 1
        } else {
477 3
            $select->where(
478 3
                $dbAdapter->quoteIdentifier($options->getLeftColumnName()) . ' >= ?', $nodeInfo->getLeft()
479 3
            );
480 3
            $select->where(
481 3
                $dbAdapter->quoteIdentifier($options->getRightColumnName()) . ' <= ?', $nodeInfo->getRight()
482 3
            );
483
        }
484
485 3
        $resultArray = $dbAdapter->fetchAll($select);
486
487 3
        if (0 < count($resultArray)) {
488 3
            return $resultArray;
489
        }
490 1
    }
491
492 1
    protected function getWhereBetween($column, $first, $second)
493
    {
494 1
        $dbAdapter = $this->getDbAdapter();
495 1
        $quotedColumn = $dbAdapter->quoteIdentifier($column);
496 1
        $quotedFirst = $dbAdapter->quote($first);
497 1
        $quotedSecond = $dbAdapter->quote($second);
498 1
        return sprintf('(%s BETWEEN %s AND %s)', $quotedColumn, $quotedFirst, $quotedSecond);
499
    }
500
}
501