Total Complexity | 43 |
Total Lines | 266 |
Duplicated Lines | 0 % |
Changes | 13 | ||
Bugs | 0 | Features | 0 |
Complex classes like Persistence 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.
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 Persistence, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Persistence |
||
23 | { |
||
24 | |||
25 | /* |
||
26 | * @param Configuration $config |
||
27 | * |
||
28 | * @return PDO |
||
29 | */ |
||
30 | public static function getPDO($config) { |
||
31 | $options = array( |
||
32 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION |
||
33 | ); |
||
34 | |||
35 | if ($config->getDsn()) { |
||
36 | try { |
||
37 | return new PDO($config->getDsn(), $config->getUser(), $config->getPassword(), $options); |
||
38 | } catch (Exception $e) { |
||
39 | throw new Exception("Could not create a db connection to '".$config->getDsn()."'. Check permissions, configuration, and documentation. ".$e->getMessage()); |
||
40 | } |
||
41 | } |
||
42 | |||
43 | } |
||
44 | |||
45 | /* |
||
46 | * @param $pdo PDO |
||
47 | * @param $config Configuration |
||
48 | * |
||
49 | */ |
||
50 | public static function crateDatabase($pdo, $config) { |
||
51 | try { |
||
52 | $db_hit_table = $config->getHitTableName(); |
||
53 | $db_options_table = $config->getOptionsTableName(); |
||
54 | $db_from_table = $config->getFromTableName(); |
||
55 | $db_url_table = $config->getUrlTableName(); |
||
56 | |||
57 | $stmt = $pdo->prepare("CREATE TABLE $db_hit_table (id VARCHAR(255), count INT)"); |
||
58 | $stmt->execute(); |
||
59 | $stmt = $pdo->prepare("CREATE TABLE $db_options_table (id VARCHAR(255), time TIMESTAMP, user VARCHAR(255))"); |
||
60 | $stmt->execute(); |
||
61 | $stmt = $pdo->prepare("CREATE TABLE $db_from_table (id VARCHAR(255), from_id VARCHAR(255), count INT)"); |
||
62 | $stmt->execute(); |
||
63 | $stmt = $pdo->prepare("CREATE TABLE $db_url_table (id VARCHAR(255), url VARCHAR(255), count INT)"); |
||
64 | $stmt->execute(); |
||
65 | |||
66 | } catch (Exception $e) { |
||
67 | throw new Exception("Could not create the database. ".$e->getMessage()); |
||
68 | } |
||
69 | return true; |
||
70 | } |
||
71 | |||
72 | /* |
||
73 | * @param $pdo PDO |
||
74 | * @param $config Configuration |
||
75 | * |
||
76 | */ |
||
77 | public static function deleteDatabase($pdo, $config) { |
||
78 | $resp = true; |
||
79 | |||
80 | $db_hit_table = $config->getHitTableName(); |
||
81 | $db_options_table = $config->getOptionsTableName(); |
||
82 | $db_from_table = $config->getFromTableName(); |
||
83 | $db_url_table = $config->getUrlTableName(); |
||
84 | |||
85 | $resp = $resp && Persistence::dropTable($pdo, $db_hit_table); |
||
86 | $resp = $resp && Persistence::dropTable($pdo, $db_options_table); |
||
87 | $resp = $resp && Persistence::dropTable($pdo, $db_from_table); |
||
88 | $resp = $resp && Persistence::dropTable($pdo, $db_url_table); |
||
89 | |||
90 | return $resp; |
||
91 | } |
||
92 | |||
93 | /* |
||
94 | * @param $pdo PDO |
||
95 | * @param $config Configuration |
||
96 | * |
||
97 | */ |
||
98 | private static function dropTable($pdo, $tableName) { |
||
99 | try { |
||
100 | $stmt = $pdo->prepare("DROP TABLE $tableName"); |
||
101 | $stmt->execute(); |
||
102 | |||
103 | } catch (Exception $e) { |
||
104 | throw new Exception("Problem deleting the table $tableName. ".$e->getMessage()); |
||
105 | } |
||
106 | return true; |
||
107 | } |
||
108 | |||
109 | /* |
||
110 | * @param $pdo PDO |
||
111 | * @param $config Configuration |
||
112 | * |
||
113 | */ |
||
114 | public static function checkTables($pdo, $config) { |
||
115 | $resp = true; |
||
116 | try { |
||
117 | |||
118 | $resp = $resp && HitDAO::checkHitTable($pdo, $config); |
||
119 | $resp = $resp && OptionsDAO::checkOptionsTable($pdo, $config); |
||
120 | $resp = $resp && FromDAO::checkFromTable($pdo, $config); |
||
121 | $resp = $resp && UrlDAO::checkUrlTable($pdo, $config); |
||
122 | } catch (Exception $e) { |
||
123 | return false; |
||
124 | } |
||
125 | return $resp; |
||
126 | |||
127 | } |
||
128 | |||
129 | /* |
||
130 | * @param options |
||
131 | * |
||
132 | */ |
||
133 | public static function getURL($config, $options = []) { |
||
134 | if (array_key_exists('url', $options)) { |
||
135 | $url = $options['url']; |
||
136 | } else if (array_key_exists('HTTP_HOST', $_SERVER)) { |
||
137 | $url = "http://".$_SERVER['HTTP_HOST']; |
||
138 | if (array_key_exists('REQUEST_URI', $_SERVER)) { |
||
139 | $url = $url.$_SERVER['REQUEST_URI']; |
||
140 | } |
||
141 | } else { |
||
142 | $url = "No Info"; |
||
143 | } |
||
144 | |||
145 | if ($config->getRemoveQueryString()) { |
||
146 | $url = preg_replace('/\?.*/', '', $url); |
||
147 | } |
||
148 | return $url; |
||
149 | } |
||
150 | |||
151 | /* |
||
152 | * @param $pdo PDO |
||
153 | * @param $config Configuration |
||
154 | * |
||
155 | */ |
||
156 | public static function updateCount($pdo, $config, $options = []) { |
||
157 | $url = Persistence::getUrl($config, $options); |
||
158 | |||
159 | if (array_key_exists('id', $options)) { |
||
160 | $id = $options['id']; |
||
161 | } else { |
||
162 | $id = $url; |
||
163 | } |
||
164 | |||
165 | HitDAO::countHit($pdo, $config, $id, $url); |
||
166 | FromDAO::countFrom($pdo, $config, $id, $options); |
||
167 | OptionsDAO::countOptions($pdo, $config, $id, $options); |
||
168 | |||
169 | return true; |
||
170 | } |
||
171 | |||
172 | |||
173 | /* |
||
174 | * @param $pdo PDO |
||
175 | * @param $config Configuration |
||
176 | * |
||
177 | */ |
||
178 | public static function getCountsFromTest($pdo, $config, $tests){ |
||
179 | $resp = []; |
||
180 | foreach ($tests as $test) { |
||
181 | $counts = self::getCountsById($pdo, $config, $test[0]); |
||
182 | if (count($counts) > 0) $resp[] = $counts; |
||
183 | } |
||
184 | return $resp; |
||
185 | } |
||
186 | |||
187 | /* |
||
188 | * @param $pdo PDO |
||
189 | * @param $config Configuration |
||
190 | * |
||
191 | */ |
||
192 | public static function getFromByTest($pdo, $config, $tests){ |
||
201 | } |
||
202 | |||
203 | /* |
||
204 | * @param $pdo PDO |
||
205 | * @param $config Configuration |
||
206 | * |
||
207 | */ |
||
208 | public static function getCounts($pdo, $config) |
||
209 | { |
||
210 | $resp = []; |
||
211 | try { |
||
212 | |||
213 | $dbHitTable = $config->getHitTableName(); |
||
214 | $dbUrlTable = $config->getUrlTableName(); |
||
215 | $stmt = $pdo->prepare("SELECT h.id, u.url, h.count FROM $dbHitTable h, $dbUrlTable u WHERE h.id=u.id"); |
||
216 | if ($stmt->execute()) { |
||
217 | while ($row = $stmt->fetch()) { |
||
218 | $resp[] = [$row[0], $row[1], $row[2]]; |
||
219 | } |
||
220 | } |
||
221 | |||
222 | } catch (Exception $e) { |
||
223 | throw new Exception("Error reading the database. Method getCounts().".$e->getMessage()); |
||
224 | } |
||
225 | return $resp; |
||
226 | } |
||
227 | |||
228 | /* |
||
229 | * @param $pdo PDO |
||
230 | * @param $config Configuration |
||
231 | */ |
||
232 | public static function checkUrlTable($pdo, $config) { |
||
233 | return UrlDAO::checkUrlTable($pdo, $config); |
||
234 | } |
||
235 | |||
236 | /* |
||
237 | * @param $pdo PDO |
||
238 | * @param $config Configuration |
||
239 | * |
||
240 | */ |
||
241 | public static function getCountsById($pdo, $config, $id) { |
||
242 | $resp = []; |
||
243 | |||
244 | $dbHitTable = $config->getHitTableName(); |
||
245 | $stmt = $pdo->prepare("SELECT h.id, h.count FROM $dbHitTable h WHERE h.id=?"); |
||
246 | if ($stmt->execute([$id])) { |
||
247 | while ($row = $stmt->fetch()) { |
||
248 | $resp = [$row[0], $row[1]]; |
||
249 | } |
||
250 | } |
||
251 | return $resp; |
||
252 | } |
||
253 | |||
254 | /* |
||
255 | * @param $pdo PDO |
||
256 | * @param $config Configuration |
||
257 | * |
||
258 | */ |
||
259 | public static function getAllHits($pdo, $config) { |
||
260 | return HitDAO::getAllHits($pdo, $config); |
||
261 | } |
||
262 | |||
263 | /* |
||
264 | * @param $pdo PDO |
||
265 | * @param $config Configuration |
||
266 | * |
||
267 | */ |
||
268 | public static function findUrls($pdo, $config, $by = []) { |
||
269 | return UrlDAO::findUrls($pdo, $config, $by); |
||
270 | } |
||
271 | |||
272 | /* |
||
273 | * @param $pdo PDO |
||
274 | * @param $config Configuration |
||
275 | * |
||
276 | */ |
||
277 | public static function findIdByTimeUser($pdo, $config, $by=[]) { |
||
278 | return OptionsDAO::findIdByTimeUser($pdo, $config, $by); |
||
279 | } |
||
280 | |||
281 | /* |
||
282 | * @param $pdo PDO |
||
283 | * @param $config Configuration |
||
284 | * |
||
285 | */ |
||
286 | public static function findByFrom($pdo, $config, $by = []) { |
||
288 | } |
||
289 | |||
290 | } |
||
291 | |||
292 | |||
293 | |||
294 |