Complex classes like Zend2DbAdapter 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 Zend2DbAdapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Zend2DbAdapter |
||
| 12 | implements AdapterInterface |
||
|
|
|||
| 13 | { |
||
| 14 | private $options; |
||
| 15 | |||
| 16 | private $dbAdapter; |
||
| 17 | |||
| 18 | private $defaultDbSelect = null; |
||
| 19 | |||
| 20 | 35 | public function __construct(Options $options, DbAdapter $dbAdapter) |
|
| 25 | |||
| 26 | /** |
||
| 27 | * @return Options |
||
| 28 | */ |
||
| 29 | 34 | private function getOptions() |
|
| 33 | |||
| 34 | /** |
||
| 35 | * @return DbAdapter |
||
| 36 | */ |
||
| 37 | 31 | private function getDbAdapter() |
|
| 41 | |||
| 42 | /** |
||
| 43 | * Data cannot contain keys like idColumnName, levelColumnName, ... |
||
| 44 | * |
||
| 45 | * @param array $data |
||
| 46 | * @return array |
||
| 47 | */ |
||
| 48 | 2 | private function cleanData(array $data) |
|
| 49 | { |
||
| 50 | 2 | $options = $this->getOptions(); |
|
| 51 | |||
| 52 | $disallowedDataKeys = array( |
||
| 53 | 2 | $options->getIdColumnName(), |
|
| 54 | 2 | $options->getLeftColumnName(), |
|
| 55 | 2 | $options->getRightColumnName(), |
|
| 56 | 2 | $options->getLevelColumnName(), |
|
| 57 | 2 | $options->getParentIdColumnName(), |
|
| 58 | 2 | $options->getScopeColumnName(), |
|
| 59 | ); |
||
| 60 | |||
| 61 | 2 | return array_diff_key($data, array_flip($disallowedDataKeys)); |
|
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param Db\Sql\Select $dbSelect |
||
| 66 | * @return void |
||
| 67 | */ |
||
| 68 | 1 | public function setDefaultDbSelect(Db\Sql\Select $dbSelect) |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Return clone of default db select |
||
| 75 | * @return Db\Sql\Select |
||
| 76 | */ |
||
| 77 | 33 | public function getDefaultDbSelect() |
|
| 78 | { |
||
| 79 | 33 | $options = $this->getOptions(); |
|
| 80 | |||
| 81 | 33 | if (null == $this->defaultDbSelect) { |
|
| 82 | 32 | $this->defaultDbSelect = new Db\Sql\Select($options->getTableName()); |
|
| 83 | } |
||
| 84 | |||
| 85 | 33 | $dbSelect = clone $this->defaultDbSelect; |
|
| 86 | |||
| 87 | 33 | return $dbSelect; |
|
| 88 | } |
||
| 89 | |||
| 90 | 14 | public function lockTree($scope) |
|
| 91 | { |
||
| 92 | 14 | $options = $this->getOptions(); |
|
| 93 | |||
| 94 | 14 | $dbAdapter = $this->getDbAdapter(); |
|
| 95 | |||
| 96 | 14 | $select = $this->getDefaultDbSelect(); |
|
| 97 | 14 | $select->columns(array( |
|
| 98 | 14 | 'i' => $options->getIdColumnName(), |
|
| 99 | )); |
||
| 100 | |||
| 101 | 14 | if ($options->getScopeColumnName()) { |
|
| 102 | 4 | $select->where(array( |
|
| 103 | 4 | $options->getScopeColumnName() => $scope, |
|
| 104 | )); |
||
| 105 | } |
||
| 106 | |||
| 107 | 14 | $sql = $select->getSqlString($dbAdapter->getPlatform()) . ' FOR UPDATE'; |
|
| 108 | |||
| 109 | 14 | $dbAdapter->query($sql, DbAdapter::QUERY_MODE_EXECUTE); |
|
| 110 | 14 | } |
|
| 111 | |||
| 112 | 15 | public function beginTransaction() |
|
| 117 | |||
| 118 | 14 | public function commitTransaction() |
|
| 123 | |||
| 124 | 1 | public function rollbackTransaction() |
|
| 129 | |||
| 130 | 2 | public function update($nodeId, array $data) |
|
| 131 | { |
||
| 132 | 2 | $options = $this->getOptions(); |
|
| 133 | |||
| 134 | 2 | $dbAdapter = $this->getDbAdapter(); |
|
| 135 | |||
| 136 | 2 | $data = $this->cleanData($data); |
|
| 137 | |||
| 138 | 2 | $update = new Db\Sql\Update($options->getTableName()); |
|
| 139 | 2 | $update->set($data) |
|
| 140 | 2 | ->where(array( |
|
| 141 | 2 | $options->getIdColumnName() => $nodeId, |
|
| 142 | )); |
||
| 143 | |||
| 144 | 2 | $dbAdapter->query($update->getSqlString($dbAdapter->getPlatform()), |
|
| 145 | 2 | DbAdapter::QUERY_MODE_EXECUTE); |
|
| 146 | 2 | } |
|
| 147 | |||
| 148 | 9 | public function insert(NodeInfo $nodeInfo, array $data) |
|
| 149 | { |
||
| 150 | 9 | $options = $this->getOptions(); |
|
| 151 | |||
| 152 | 9 | $dbAdapter = $this->getDbAdapter(); |
|
| 153 | |||
| 154 | 9 | $data[$options->getParentIdColumnName()] = $nodeInfo->getParentId(); |
|
| 155 | 9 | $data[$options->getLevelColumnName()] = $nodeInfo->getLevel(); |
|
| 156 | 9 | $data[$options->getLeftColumnName()] = $nodeInfo->getLeft(); |
|
| 157 | 9 | $data[$options->getRightColumnName()] = $nodeInfo->getRight(); |
|
| 158 | |||
| 159 | 9 | if ($options->getScopeColumnName()) { |
|
| 160 | 3 | $data[$options->getScopeColumnName()] = $nodeInfo->getScope(); |
|
| 161 | } |
||
| 162 | |||
| 163 | 9 | $insert = new Db\Sql\Insert($options->getTableName()); |
|
| 164 | 9 | $insert->values($data); |
|
| 165 | 9 | $dbAdapter->query($insert->getSqlString($dbAdapter->getPlatform()), |
|
| 166 | 9 | DbAdapter::QUERY_MODE_EXECUTE); |
|
| 167 | |||
| 168 | 9 | $lastGeneratedValue = $dbAdapter->getDriver() |
|
| 169 | 9 | ->getLastGeneratedValue($options->getSequenceName()); |
|
| 170 | |||
| 171 | 9 | return $lastGeneratedValue; |
|
| 172 | } |
||
| 173 | |||
| 174 | 2 | public function delete($leftIndex, $rightIndex, $scope=null) |
|
| 175 | { |
||
| 176 | 2 | $options = $this->getOptions(); |
|
| 177 | |||
| 178 | 2 | $dbAdapter = $this->getDbAdapter(); |
|
| 179 | |||
| 180 | 2 | $delete = new Db\Sql\Delete($options->getTableName()); |
|
| 181 | 2 | $delete->where |
|
| 182 | 2 | ->greaterThanOrEqualTo($options->getLeftColumnName(), $leftIndex) |
|
| 183 | 2 | ->AND |
|
| 184 | 2 | ->lessThanOrEqualTo($options->getRightColumnName(), $rightIndex); |
|
| 185 | |||
| 186 | 2 | if ($options->getScopeColumnName()) { |
|
| 187 | 1 | $delete->where |
|
| 188 | 1 | ->AND |
|
| 189 | 1 | ->equalTo($options->getScopeColumnName(), $scope); |
|
| 190 | } |
||
| 191 | |||
| 192 | 2 | $dbAdapter->query($delete->getSqlString($dbAdapter->getPlatform()), |
|
| 193 | 2 | DbAdapter::QUERY_MODE_EXECUTE); |
|
| 194 | 2 | } |
|
| 195 | |||
| 196 | 12 | public function moveLeftIndexes($fromIndex, $shift, $scope=null) |
|
| 197 | { |
||
| 198 | 12 | $options = $this->getOptions(); |
|
| 199 | |||
| 200 | 12 | if (0 == $shift) { |
|
| 201 | return; |
||
| 202 | } |
||
| 203 | |||
| 204 | 12 | $dbAdapter = $this->getDbAdapter(); |
|
| 205 | 12 | $dbPlatform = $dbAdapter->getPlatform(); |
|
| 206 | |||
| 207 | 12 | $sql = 'UPDATE ' . $dbPlatform->quoteIdentifier($options->getTableName()) |
|
| 208 | 12 | . ' SET ' |
|
| 209 | 12 | . $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' = ' |
|
| 210 | 12 | . $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' + :shift' |
|
| 211 | 12 | . ' WHERE ' |
|
| 212 | 12 | . $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' > :fromIndex'; |
|
| 213 | |||
| 214 | 12 | if ($options->getScopeColumnName()) { |
|
| 215 | 3 | $sql .= ' AND ' . $dbPlatform->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbPlatform->quoteValue($scope); |
|
| 216 | } |
||
| 217 | |||
| 218 | $binds = array( |
||
| 219 | 12 | ':shift' => $shift, |
|
| 220 | 12 | ':fromIndex' => $fromIndex, |
|
| 221 | ); |
||
| 222 | |||
| 223 | 12 | $dbAdapter->query($sql) |
|
| 224 | 12 | ->execute($binds); |
|
| 225 | 12 | } |
|
| 226 | |||
| 227 | 12 | public function moveRightIndexes($fromIndex, $shift, $scope=null) |
|
| 228 | { |
||
| 229 | 12 | $options = $this->getOptions(); |
|
| 230 | |||
| 231 | 12 | if (0 == $shift) { |
|
| 232 | return; |
||
| 233 | } |
||
| 234 | |||
| 235 | 12 | $dbAdapter = $this->getDbAdapter(); |
|
| 236 | 12 | $dbPlatform = $dbAdapter->getPlatform(); |
|
| 237 | |||
| 238 | 12 | $sql = 'UPDATE ' . $dbPlatform->quoteIdentifier($options->getTableName()) |
|
| 239 | 12 | . ' SET ' |
|
| 240 | 12 | . $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' = ' |
|
| 241 | 12 | . $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' + :shift' |
|
| 242 | 12 | . ' WHERE ' |
|
| 243 | 12 | . $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' > :fromIndex'; |
|
| 244 | |||
| 245 | 12 | if ($options->getScopeColumnName()) { |
|
| 246 | 3 | $sql .= ' AND ' . $dbPlatform->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbPlatform->quoteValue($scope); |
|
| 247 | } |
||
| 248 | |||
| 249 | $binds = array( |
||
| 250 | 12 | ':shift' => $shift, |
|
| 251 | 12 | ':fromIndex' => $fromIndex, |
|
| 252 | ); |
||
| 253 | |||
| 254 | 12 | $dbAdapter->query($sql) |
|
| 255 | 12 | ->execute($binds); |
|
| 256 | 12 | } |
|
| 257 | |||
| 258 | 4 | public function updateParentId($nodeId, $newParentId) |
|
| 259 | { |
||
| 260 | 4 | $options = $this->getOptions(); |
|
| 261 | |||
| 262 | 4 | $dbAdapter = $this->getDbAdapter(); |
|
| 263 | |||
| 264 | 4 | $update = new Db\Sql\Update($options->getTableName()); |
|
| 265 | 4 | $update->set(array( |
|
| 266 | 4 | $options->getParentIdColumnName() => $newParentId, |
|
| 267 | )) |
||
| 268 | 4 | ->where(array( |
|
| 269 | 4 | $options->getIdColumnName() => $nodeId, |
|
| 270 | )); |
||
| 271 | |||
| 272 | 4 | $dbAdapter->query($update->getSqlString($dbAdapter->getPlatform()), |
|
| 273 | 4 | DbAdapter::QUERY_MODE_EXECUTE); |
|
| 274 | 4 | } |
|
| 275 | |||
| 276 | 5 | public function updateLevels($leftIndexFrom, $rightIndexTo, $shift, $scope=null) |
|
| 277 | { |
||
| 278 | 5 | $options = $this->getOptions(); |
|
| 279 | |||
| 280 | 5 | if (0 == $shift) { |
|
| 281 | 1 | return; |
|
| 282 | } |
||
| 283 | |||
| 284 | 4 | $dbAdapter = $this->getDbAdapter(); |
|
| 285 | 4 | $dbPlatform = $dbAdapter->getPlatform(); |
|
| 286 | |||
| 287 | 4 | $sql = 'UPDATE ' . $dbPlatform->quoteIdentifier($options->getTableName()) |
|
| 288 | 4 | . ' SET ' |
|
| 289 | 4 | . $dbPlatform->quoteIdentifier($options->getLevelColumnName()) . ' = ' |
|
| 290 | 4 | . $dbPlatform->quoteIdentifier($options->getLevelColumnName()) . ' + :shift' |
|
| 291 | 4 | . ' WHERE ' |
|
| 292 | 4 | . $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' >= :leftFrom' |
|
| 293 | 4 | . ' AND ' . $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' <= :rightTo'; |
|
| 294 | |||
| 295 | 4 | if ($options->getScopeColumnName()) { |
|
| 296 | $sql .= ' AND ' . $dbPlatform->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbPlatform->quoteValue($scope); |
||
| 297 | } |
||
| 298 | |||
| 299 | $binds = array( |
||
| 300 | 4 | ':shift' => $shift, |
|
| 301 | 4 | ':leftFrom' => $leftIndexFrom, |
|
| 302 | 4 | ':rightTo' => $rightIndexTo, |
|
| 303 | ); |
||
| 304 | |||
| 305 | 4 | $dbAdapter->query($sql) |
|
| 306 | 4 | ->execute($binds); |
|
| 307 | 4 | } |
|
| 308 | |||
| 309 | 5 | public function moveBranch($leftIndexFrom, $rightIndexTo, $shift, $scope=null) |
|
| 310 | { |
||
| 311 | 5 | if (0 == $shift) { |
|
| 312 | return; |
||
| 313 | } |
||
| 314 | |||
| 315 | 5 | $options = $this->getOptions(); |
|
| 316 | |||
| 317 | 5 | $dbAdapter = $this->getDbAdapter(); |
|
| 318 | 5 | $dbPlatform = $dbAdapter->getPlatform(); |
|
| 319 | |||
| 320 | 5 | $sql = 'UPDATE ' . $dbPlatform->quoteIdentifier($options->getTableName()) |
|
| 321 | 5 | . ' SET ' |
|
| 322 | 5 | . $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' = ' |
|
| 323 | 5 | . $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' + :shift, ' |
|
| 324 | 5 | . $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' = ' |
|
| 325 | 5 | . $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' + :shift' |
|
| 326 | 5 | . ' WHERE ' |
|
| 327 | 5 | . $dbPlatform->quoteIdentifier($options->getLeftColumnName()) . ' >= :leftFrom' |
|
| 328 | 5 | . ' AND ' . $dbPlatform->quoteIdentifier($options->getRightColumnName()) . ' <= :rightTo'; |
|
| 329 | |||
| 330 | 5 | if ($options->getScopeColumnName()) { |
|
| 331 | 1 | $sql .= ' AND ' . $dbPlatform->quoteIdentifier($options->getScopeColumnName()) . ' = ' . $dbPlatform->quoteValue($scope); |
|
| 332 | } |
||
| 333 | |||
| 334 | $binds = array( |
||
| 335 | 5 | ':shift' => $shift, |
|
| 336 | 5 | ':leftFrom' => $leftIndexFrom, |
|
| 337 | 5 | ':rightTo' => $rightIndexTo, |
|
| 338 | ); |
||
| 339 | |||
| 340 | 5 | $dbAdapter->query($sql) |
|
| 341 | 5 | ->execute($binds); |
|
| 342 | 5 | } |
|
| 343 | |||
| 344 | 8 | public function getRoots($scope=null) |
|
| 345 | { |
||
| 346 | 8 | $options = $this->getOptions(); |
|
| 347 | |||
| 348 | 8 | $dbAdapter = $this->getDbAdapter(); |
|
| 349 | |||
| 350 | 8 | $select = $this->getDefaultDbSelect(); |
|
| 351 | 8 | $select->where |
|
| 352 | 8 | ->equalTo($options->getParentIdColumnName(), 0); |
|
| 353 | |||
| 354 | 8 | if (null != $scope && $options->getScopeColumnName()) { |
|
| 355 | 2 | $select->where |
|
| 356 | 2 | ->equalTo($options->getScopeColumnName(), $scope); |
|
| 357 | } |
||
| 358 | |||
| 359 | 8 | $result = $dbAdapter->query($select->getSqlString($dbAdapter->getPlatform()), |
|
| 360 | 8 | DbAdapter::QUERY_MODE_EXECUTE); |
|
| 361 | |||
| 362 | 8 | return $result->toArray(); |
|
| 363 | } |
||
| 364 | |||
| 365 | 7 | public function getRoot($scope=null) |
|
| 371 | |||
| 372 | 22 | public function getNode($nodeId) |
|
| 392 | |||
| 393 | 20 | public function getNodeInfo($nodeId) |
|
| 394 | { |
||
| 395 | 20 | $options = $this->getOptions(); |
|
| 396 | 20 | $result = $this->getNode($nodeId); |
|
| 397 | |||
| 398 | 20 | if (null == $result) { |
|
| 399 | 6 | $result = null; |
|
| 400 | } else { |
||
| 401 | 19 | $id = $result[$options->getIdColumnName()]; |
|
| 402 | 19 | $parentId = $result[$options->getParentIdColumnName()]; |
|
| 403 | 19 | $level = $result[$options->getLevelColumnName()]; |
|
| 404 | 19 | $left = $result[$options->getLeftColumnName()]; |
|
| 418 | |||
| 419 | 2 | public function getPath($nodeId, $startLevel = 0, $excludeLastNode = false) |
|
| 461 | |||
| 462 | 3 | public function getDescendants($nodeId = 1, $startLevel = 0, $levels = null, $excludeBranch = null) |
|
| 524 | } |
||
| 525 |