Completed
Push — master ( 82e7b9...66591e )
by Bartko
03:50
created

Zend1DbAdapter::deleteAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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