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 | protected $lockSqlBuilder; |
||
| 21 | |||
| 22 | 21 | public function __construct(Options $options, ZendDbAdapter $dbAdapter) |
|
| 27 | |||
| 28 | /** |
||
| 29 | * @return Options |
||
| 30 | */ |
||
| 31 | 20 | private function getOptions() |
|
| 35 | |||
| 36 | /** |
||
| 37 | * @return ZendDbAdapter |
||
| 38 | */ |
||
| 39 | 17 | public function getDbAdapter() |
|
| 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 | } |
||
| 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) { |
|
| 67 | 12 | $this->getDbAdapter()->query($sql); |
|
| 68 | } |
||
| 69 | 12 | } |
|
| 70 | |||
| 71 | 12 | public function unlockTable() |
|
| 72 | { |
||
| 73 | 12 | $sql = $this->getLockSqlBuilder()->getUnlockSqlString(); |
|
| 74 | |||
| 75 | 12 | if (null != $sql) { |
|
| 76 | $this->getDbAdapter()->query($sql); |
||
| 77 | } |
||
| 78 | 12 | } |
|
| 79 | |||
| 80 | 12 | protected function _isInTransaction() |
|
| 86 | |||
| 87 | 12 | protected function _beginTransaction() |
|
| 91 | |||
| 92 | 12 | protected function _commitTransaction() |
|
| 96 | |||
| 97 | protected function _rollbackTransaction() |
||
| 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 | } 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 | ); |
||
| 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 | ); |
||
| 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 | ); |
||
| 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 | ); |
||
| 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 | ); |
||
| 187 | |||
| 188 | $where = array( |
||
| 189 | 4 | $dbAdapter->quoteIdentifier($options->getIdColumnName()) . ' = ?' => $nodeId, |
|
| 190 | ); |
||
| 191 | 4 | $dbAdapter->update($options->getTableName(), $bind, $where); |
|
| 192 | 4 | } |
|
| 193 | |||
| 194 | 15 | public function getNode($nodeId) |
|
| 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 | } 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 | } |
||
| 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) |
|
| 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 | ); |
||
| 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 | } 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 | ); |
||
| 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 | ); |
||
| 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 | ); |
||
| 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 | ); |
||
| 376 | 1 | $select->where( |
|
| 377 | 1 | $dbAdapter->quoteIdentifier($options->getRightColumnName()) . ' >= ?', $nodeInfo->getRight() |
|
| 378 | ); |
||
| 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 | ); |
||
| 385 | } |
||
| 386 | |||
| 387 | 1 | if (true == $excludeLastNode) { |
|
| 388 | 1 | $select->where( |
|
| 389 | 1 | $dbAdapter->quoteIdentifier($options->getLevelColumnName()) . ' < ?', $nodeInfo->getLevel() |
|
| 390 | ); |
||
| 391 | } |
||
| 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 | ); |
||
| 416 | } |
||
| 417 | |||
| 418 | 2 | if (null != $levels) { |
|
| 419 | 2 | $endLevel = $nodeInfo->getLevel() + (int) $startLevel + abs($levels); |
|
| 420 | 2 | $select->where( |
|
| 421 | 2 | $dbAdapter->quoteIdentifier($options->getLevelColumnName()) . ' < ?', $endLevel |
|
| 422 | ); |
||
| 423 | } |
||
| 424 | |||
| 425 | 2 | if (null != $excludeBranch && null != ($excludeNodeInfo = $this->getNodeInfo($excludeBranch))) { |
|
| 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 | ); |
||
| 436 | 1 | $select->where($where); |
|
| 437 | } else { |
||
| 438 | 2 | $select->where( |
|
| 439 | 2 | $dbAdapter->quoteIdentifier($options->getLeftColumnName()) . ' >= ?', $nodeInfo->getLeft() |
|
| 440 | ); |
||
| 441 | 2 | $select->where( |
|
| 442 | 2 | $dbAdapter->quoteIdentifier($options->getRightColumnName()) . ' <= ?', $nodeInfo->getRight() |
|
| 443 | ); |
||
| 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) |
|
| 461 | } |
||
| 462 |