Complex classes like Zend1DbAdapter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Zend1DbAdapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Zend1DbAdapter |
||
11 | extends NestedTransactionDbAdapterAbstract |
||
12 | implements AdapterInterface |
||
13 | { |
||
14 | protected $options; |
||
15 | |||
16 | protected $dbAdapter; |
||
17 | |||
18 | protected $defaultDbSelect; |
||
19 | |||
20 | 35 | public function __construct(Options $options, ZendDbAdapter $dbAdapter) |
|
25 | |||
26 | /** |
||
27 | * @return Options |
||
28 | */ |
||
29 | 34 | private function getOptions() |
|
33 | |||
34 | /** |
||
35 | * @return ZendDbAdapter |
||
36 | */ |
||
37 | 31 | public function getDbAdapter() |
|
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 | } |
||
56 | |||
57 | 14 | $dbAdapter->fetchAll($select); |
|
58 | 14 | } |
|
59 | |||
60 | 15 | protected function _isInTransaction() |
|
66 | |||
67 | 15 | protected function _beginTransaction() |
|
71 | |||
72 | 14 | protected function _commitTransaction() |
|
76 | |||
77 | 1 | protected function _rollbackTransaction() |
|
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 | ); |
||
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 | } |
||
113 | |||
114 | $binds = array( |
||
115 | 12 | ':shift' => $shift, |
|
116 | 12 | ':fromIndex' => $fromIndex, |
|
117 | ); |
||
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 | } |
||
139 | |||
140 | $binds = array( |
||
141 | 12 | ':shift' => $shift, |
|
142 | 12 | ':fromIndex' => $fromIndex, |
|
143 | ); |
||
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 | ); |
||
157 | |||
158 | $where = array( |
||
159 | 4 | $dbAdapter->quoteIdentifier($options->getIdColumnName()) . ' = ?' => $nodeId, |
|
160 | ); |
||
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 | } |
||
176 | |||
177 | 8 | return $dbAdapter->fetchAll($select); |
|
178 | } |
||
179 | |||
180 | 7 | public function getRoot($scope=null) |
|
186 | |||
187 | 22 | public function getNode($nodeId) |
|
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 | } 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 | } 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 | } |
||
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) |
|
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 | ); |
||
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 | } |
||
285 | |||
286 | 9 | $dbAdapter->insert($options->getTableName(), $data); |
|
287 | 9 | if ('' != $options->getSequenceName()) { |
|
288 | 9 | $lastGeneratedValue = $dbAdapter->lastSequenceId($options->getSequenceName()); |
|
289 | } else { |
||
290 | $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 | ); |
||
306 | |||
307 | 2 | if ($options->getScopeColumnName()) { |
|
308 | 1 | $where[$dbAdapter->quoteIdentifier($options->getScopeColumnName()) . ' = ?'] = $scope; |
|
309 | } |
||
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 | ); |
||
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 | } |
||
365 | |||
366 | $binds = array( |
||
367 | 5 | ':shift' => $shift, |
|
368 | 5 | ':leftFrom' => $leftIndexFrom, |
|
369 | 5 | ':rightTo' => $rightIndexTo, |
|
370 | ); |
||
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 | ); |
||
392 | 2 | $select->where( |
|
393 | 2 | $dbAdapter->quoteIdentifier($options->getRightColumnName()) . ' >= ?', $nodeInfo->getRight() |
|
394 | ); |
||
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 | ); |
||
401 | } |
||
402 | |||
403 | 2 | if (true == $excludeLastNode) { |
|
404 | 1 | $select->where( |
|
405 | 1 | $dbAdapter->quoteIdentifier($options->getLevelColumnName()) . ' < ?', $nodeInfo->getLevel() |
|
406 | ); |
||
407 | } |
||
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 | } |
||
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 | ); |
||
435 | } |
||
436 | |||
437 | 3 | if (null != $levels) { |
|
438 | 2 | $endLevel = $nodeInfo->getLevel() + (int) $startLevel + abs($levels); |
|
439 | 2 | $select->where( |
|
440 | 2 | $dbAdapter->quoteIdentifier($options->getLevelColumnName()) . ' < ?', $endLevel |
|
441 | ); |
||
442 | } |
||
443 | |||
444 | 3 | if (null != $excludeBranch && null != ($excludeNodeInfo = $this->getNodeInfo($excludeBranch))) { |
|
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 | ); |
||
455 | 1 | $select->where($where); |
|
456 | } else { |
||
457 | 3 | $select->where( |
|
458 | 3 | $dbAdapter->quoteIdentifier($options->getLeftColumnName()) . ' >= ?', $nodeInfo->getLeft() |
|
459 | ); |
||
460 | 3 | $select->where( |
|
461 | 3 | $dbAdapter->quoteIdentifier($options->getRightColumnName()) . ' <= ?', $nodeInfo->getRight() |
|
462 | ); |
||
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) |
|
480 | } |
||
481 |