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 | private $lockSqlBuilder; |
||
21 | |||
22 | 21 | public function __construct(Options $options, DbAdapter $dbAdapter) |
|
27 | |||
28 | /** |
||
29 | * @return Options |
||
30 | */ |
||
31 | 20 | private function getOptions() |
|
35 | |||
36 | /** |
||
37 | * @return DbAdapter |
||
38 | */ |
||
39 | 17 | private function getDbAdapter() |
|
43 | |||
44 | /** |
||
45 | * Data cannot contain keys like idColumnName, levelColumnName, ... |
||
46 | * |
||
47 | * @param array $data |
||
48 | * @return array |
||
49 | */ |
||
50 | 1 | private function cleanData(array $data) |
|
51 | { |
||
52 | 1 | $options = $this->getOptions(); |
|
53 | |||
54 | $disallowedDataKeys = array( |
||
55 | 1 | $options->getIdColumnName(), |
|
56 | 1 | $options->getLeftColumnName(), |
|
57 | 1 | $options->getRightColumnName(), |
|
58 | 1 | $options->getLevelColumnName(), |
|
59 | 1 | $options->getParentIdColumnName(), |
|
60 | ); |
||
61 | |||
62 | 1 | return array_diff_key($data, array_flip($disallowedDataKeys)); |
|
63 | } |
||
64 | |||
65 | /** |
||
66 | * @param Db\Sql\Select $dbSelect |
||
67 | * @return void |
||
68 | */ |
||
69 | 1 | public function setDefaultDbSelect(Db\Sql\Select $dbSelect) |
|
73 | |||
74 | /** |
||
75 | * Return clone of default db select |
||
76 | * @return Db\Sql\Select |
||
77 | */ |
||
78 | 18 | public function getDefaultDbSelect() |
|
79 | { |
||
80 | 18 | $options = $this->getOptions(); |
|
81 | |||
82 | 18 | if (null == $this->defaultDbSelect) { |
|
83 | 17 | $this->defaultDbSelect = new Db\Sql\Select($options->getTableName()); |
|
84 | } |
||
85 | |||
86 | 18 | $dbSelect = clone $this->defaultDbSelect; |
|
87 | |||
88 | 18 | return $dbSelect; |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * @return LockSqlBuilderInterface |
||
93 | */ |
||
94 | 12 | private function getLockSqlBuilder() |
|
95 | { |
||
96 | 12 | if (null == $this->lockSqlBuilder) { |
|
97 | 12 | $vendorName = $this->getDbAdapter() |
|
98 | 12 | ->getDriver() |
|
99 | 12 | ->getDatabasePlatformName(); |
|
100 | |||
101 | 12 | $factory = new LockSqlBuilderFactory(); |
|
102 | 12 | $this->lockSqlBuilder = $factory->createAdapter($vendorName); |
|
103 | } |
||
104 | |||
105 | 12 | return $this->lockSqlBuilder; |
|
106 | } |
||
107 | |||
108 | 12 | public function lockTable() |
|
109 | { |
||
110 | 12 | $tableName = $this->getOptions() |
|
111 | 12 | ->getTableName(); |
|
112 | |||
113 | 12 | $sql = $this->getLockSqlBuilder() |
|
114 | 12 | ->getLockSqlString($tableName); |
|
115 | |||
116 | 12 | if (null != $sql) { |
|
117 | 12 | $this->getDbAdapter() |
|
118 | 12 | ->query($sql, DbAdapter::QUERY_MODE_EXECUTE); |
|
119 | } |
||
120 | 12 | } |
|
121 | |||
122 | 12 | public function unlockTable() |
|
123 | { |
||
124 | 12 | $sql = $this->getLockSqlBuilder() |
|
125 | 12 | ->getUnlockSqlString(); |
|
126 | |||
127 | 12 | if (null != $sql) { |
|
128 | 12 | $this->getDbAdapter() |
|
129 | 12 | ->query($sql, DbAdapter::QUERY_MODE_EXECUTE); |
|
130 | } |
||
131 | 12 | } |
|
132 | |||
133 | 12 | public function beginTransaction() |
|
138 | |||
139 | 12 | public function commitTransaction() |
|
144 | |||
145 | public function rollbackTransaction() |
||
150 | |||
151 | 2 | public function update($nodeId, array $data, NodeInfo $nodeInfo = null) |
|
152 | { |
||
153 | 2 | $options = $this->getOptions(); |
|
154 | |||
155 | 2 | $dbAdapter = $this->getDbAdapter(); |
|
156 | |||
157 | 2 | if (null == $nodeInfo) { |
|
158 | 1 | $data = $this->cleanData($data); |
|
159 | } else { |
||
160 | 1 | $data[$options->getParentIdColumnName()] = $nodeInfo->getParentId(); |
|
161 | 1 | $data[$options->getLevelColumnName()] = $nodeInfo->getLevel(); |
|
162 | 1 | $data[$options->getLeftColumnName()] = $nodeInfo->getLeft(); |
|
163 | 1 | $data[$options->getRightColumnName()] = $nodeInfo->getRight(); |
|
164 | } |
||
165 | |||
166 | 2 | $update = new Db\Sql\Update($options->getTableName()); |
|
167 | 2 | $update->set($data) |
|
168 | 2 | ->where(array( |
|
169 | 2 | $options->getIdColumnName() => $nodeId, |
|
170 | )); |
||
171 | |||
172 | 2 | $dbAdapter->query($update->getSqlString($dbAdapter->getPlatform()), |
|
173 | 2 | DbAdapter::QUERY_MODE_EXECUTE); |
|
174 | 2 | } |
|
175 | |||
176 | 4 | public function insert(NodeInfo $nodeInfo, array $data) |
|
197 | |||
198 | 1 | public function delete($leftIndex, $rightIndex) |
|
213 | |||
214 | 1 | public function deleteAll($expectNodeId) |
|
226 | |||
227 | 9 | public function moveLeftIndexes($fromIndex, $shift) |
|
228 | { |
||
229 | 9 | $options = $this->getOptions(); |
|
230 | |||
231 | 9 | if (0 == $shift) { |
|
232 | return; |
||
253 | |||
254 | 9 | public function moveRightIndexes($fromIndex, $shift) |
|
280 | |||
281 | 4 | public function updateParentId($nodeId, $newParentId) |
|
298 | |||
299 | 4 | public function updateLevels($leftIndexFrom, $rightIndexTo, $shift) |
|
327 | |||
328 | 4 | public function moveBranch($leftIndexFrom, $rightIndexTo, $shift) |
|
358 | |||
359 | 15 | public function getNode($nodeId) |
|
379 | |||
380 | 14 | public function getNodeInfo($nodeId) |
|
399 | |||
400 | 1 | public function getPath($nodeId, $startLevel = 0, $excludeLastNode = false) |
|
436 | |||
437 | 2 | public function getDescendants($nodeId = 1, $startLevel = 0, $levels = null, $excludeBranch = null) |
|
495 | } |
||
496 |