Completed
Push — develop ( cf0b6f...baacde )
by Bartko
06:00
created

Zend1DbAdapter::_beginTransaction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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