Total Complexity | 61 |
Total Lines | 439 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like ResourceManager 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 ResourceManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class ResourceManager |
||
14 | { |
||
15 | /** |
||
16 | * The integer value for event-type resources. |
||
17 | * @var integer |
||
18 | */ |
||
19 | const RESOURCE_TYPE_EVENT = 20; |
||
20 | |||
21 | /** |
||
22 | * The integer value for datasource-type resources. |
||
23 | * @var integer |
||
24 | */ |
||
25 | const RESOURCE_TYPE_DS = 21; |
||
26 | |||
27 | /** |
||
28 | * A private method used to return the `tbl_pages` column related to |
||
29 | * the given resource type. |
||
30 | * |
||
31 | * @param integer $type |
||
32 | * The resource type, either `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DS` |
||
33 | * @return string |
||
34 | * A string representing the `tbl_pages` column to target. |
||
35 | */ |
||
36 | private static function getColumnFromType($type) |
||
37 | { |
||
38 | switch ($type) { |
||
39 | case ResourceManager::RESOURCE_TYPE_EVENT: |
||
40 | return 'events'; |
||
41 | case ResourceManager::RESOURCE_TYPE_DS: |
||
42 | return 'data_sources'; |
||
43 | } |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * A method used to return the Manager for the given resource type. |
||
48 | * |
||
49 | * @param integer $type |
||
50 | * The resource type, either `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DS` |
||
51 | * @return string |
||
52 | * An string representing the name of the Manager class that handles the resource. |
||
53 | */ |
||
54 | public static function getManagerFromType($type) |
||
55 | { |
||
56 | switch ($type) { |
||
57 | case ResourceManager::RESOURCE_TYPE_EVENT: |
||
58 | return 'EventManager'; |
||
59 | case ResourceManager::RESOURCE_TYPE_DS: |
||
60 | return 'DatasourceManager'; |
||
61 | } |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Returns the axis a given resource type will be sorted by. |
||
66 | * The following handles are available: `name`, `source`, `release-date` |
||
67 | * and `author`. Defaults to 'name'. |
||
68 | * |
||
69 | * @param integer $type |
||
70 | * The resource type, either `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DS` |
||
71 | * @return string |
||
72 | * The axis handle. |
||
73 | */ |
||
74 | public static function getSortingField($type) |
||
75 | { |
||
76 | $result = Symphony::Configuration()->get(self::getColumnFromType($type) . '_index_sortby', 'sorting'); |
||
77 | |||
78 | return (is_null($result) ? 'name' : $result); |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Returns the sort order for a given resource type. Defaults to 'asc'. |
||
83 | * |
||
84 | * @param integer $type |
||
85 | * The resource type, either `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DS` |
||
86 | * @return string |
||
87 | * Either 'asc' or 'desc'. |
||
88 | */ |
||
89 | public static function getSortingOrder($type) |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Saves the new axis a given resource type will be sorted by. |
||
98 | * |
||
99 | * @param integer $type |
||
100 | * The resource type, either `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DS` |
||
101 | * @param string $sort |
||
102 | * The axis handle. |
||
103 | * @param boolean $write |
||
104 | * If false, the new settings won't be written on the configuration file. |
||
105 | * Defaults to true. |
||
106 | */ |
||
107 | public static function setSortingField($type, $sort, $write = true) |
||
108 | { |
||
109 | Symphony::Configuration()->set(self::getColumnFromType($type) . '_index_sortby', $sort, 'sorting'); |
||
110 | |||
111 | if ($write) { |
||
112 | Symphony::Configuration()->write(); |
||
113 | } |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Saves the new sort order for a given resource type. |
||
118 | * |
||
119 | * @param integer $type |
||
120 | * The resource type, either `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DS` |
||
121 | * @param string $order |
||
122 | * Either 'asc' or 'desc'. |
||
123 | * @param boolean $write |
||
124 | * If false, the new settings won't be written on the configuration file. |
||
125 | * Defaults to true. |
||
126 | */ |
||
127 | public static function setSortingOrder($type, $order, $write = true) |
||
128 | { |
||
129 | Symphony::Configuration()->set(self::getColumnFromType($type) . '_index_order', $order, 'sorting'); |
||
130 | |||
131 | if ($write) { |
||
132 | Symphony::Configuration()->write(); |
||
133 | } |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * This function will return an associative array of resource information. The |
||
138 | * information returned is defined by the `$select` parameter, which will allow |
||
139 | * a developer to restrict what information is returned about the resource. |
||
140 | * Optionally, `$where` (not implemented) and `$order_by` parameters allow a developer to |
||
141 | * further refine their query. |
||
142 | * |
||
143 | * @param integer $type |
||
144 | * The type of the resource (needed to retrieve the correct Manager) |
||
145 | * @param array $select (optional) |
||
146 | * Accepts an array of keys to return from the manager's `listAll()` method. If omitted, |
||
147 | * all keys will be returned. |
||
148 | * @param array $where (optional) |
||
149 | * Not implemented. |
||
150 | * @param string $order_by (optional) |
||
151 | * Allows a developer to return the resources in a particular order. The syntax is the |
||
152 | * same as other `fetch` methods. If omitted this will return resources ordered by `name`. |
||
153 | * @throws SymphonyErrorPage |
||
154 | * @throws Exception |
||
155 | * @return array |
||
156 | * An associative array of resource information, formatted in the same way as the resource's |
||
157 | * manager `listAll()` method. |
||
158 | */ |
||
159 | public static function fetch($type, array $select = array(), array $where = array(), $order_by = null) |
||
160 | { |
||
161 | $manager = self::getManagerFromType($type); |
||
162 | |||
163 | if (!isset($manager)) { |
||
164 | throw new Exception(__('Unable to find a Manager class for this resource.')); |
||
165 | } |
||
166 | |||
167 | $resources = call_user_func(array($manager, 'listAll')); |
||
168 | |||
169 | foreach ($resources as &$r) { |
||
170 | // If source is numeric, it's considered to be a Symphony Section |
||
171 | if (isset($r['source']) && General::intval($r['source']) > 0) { |
||
172 | $section = SectionManager::fetch($r['source']); |
||
173 | |||
174 | if ($section !== false) { |
||
175 | $r['source'] = array( |
||
176 | 'name' => General::sanitize($section->get('name')), |
||
177 | 'handle' => General::sanitize($section->get('handle')), |
||
178 | 'id' => $r['source'] |
||
179 | ); |
||
180 | } else { |
||
181 | unset($r['source']); |
||
182 | } |
||
183 | |||
184 | // If source is set but no numeric, it's considered to be a Symphony Type (e.g. authors or navigation) |
||
185 | } elseif (isset($r['source'])) { |
||
186 | $r['source'] = array( |
||
187 | 'name' => ucwords(General::sanitize($r['source'])), |
||
188 | 'handle' => General::sanitize($r['source']), |
||
189 | ); |
||
190 | |||
191 | // Resource provided by extension? |
||
192 | } else { |
||
193 | $extension = self::__getExtensionFromHandle($type, $r['handle']); |
||
194 | |||
195 | if (!empty($extension)) { |
||
196 | $extension = Symphony::ExtensionManager()->about($extension); |
||
197 | $r['source'] = array( |
||
198 | 'name' => General::sanitize($extension['name']), |
||
199 | 'handle' => Lang::createHandle($extension['name']) |
||
200 | ); |
||
201 | } |
||
202 | } |
||
203 | } |
||
204 | |||
205 | if (empty($select) && empty($where) && is_null($order_by)) { |
||
206 | return $resources; |
||
207 | } |
||
208 | |||
209 | if (!is_null($order_by) && !empty($resources)) { |
||
210 | $author = $label = $source = $name = array(); |
||
211 | $order_by = array_map('strtolower', explode(' ', $order_by)); |
||
212 | $order = ($order_by[1] == 'desc') ? SORT_DESC : SORT_ASC; |
||
213 | $sort = $order_by[0]; |
||
214 | |||
215 | if ($sort == 'author') { |
||
216 | foreach ($resources as $key => $about) { |
||
217 | $author[$key] = $about['author']['name']; |
||
218 | $label[$key] = $key; |
||
219 | } |
||
220 | |||
221 | array_multisort($author, $order, $label, SORT_ASC, $resources); |
||
222 | } elseif ($sort == 'release-date') { |
||
223 | foreach ($resources as $key => $about) { |
||
224 | $author[$key] = $about['release-date']; |
||
225 | $label[$key] = $key; |
||
226 | } |
||
227 | |||
228 | array_multisort($author, $order, $label, SORT_ASC, $resources); |
||
229 | } elseif ($sort == 'source') { |
||
230 | foreach ($resources as $key => $about) { |
||
231 | $source[$key] = $about['source']['handle']; |
||
232 | $label[$key] = $key; |
||
233 | } |
||
234 | |||
235 | array_multisort($source, $order, $label, SORT_ASC, $resources); |
||
236 | } elseif ($sort == 'name') { |
||
237 | foreach ($resources as $key => $about) { |
||
238 | $name[$key] = strtolower($about['name']); |
||
239 | $label[$key] = $key; |
||
240 | } |
||
241 | |||
242 | array_multisort($name, $order, $label, SORT_ASC, $resources); |
||
243 | } |
||
244 | } |
||
245 | |||
246 | $data = array(); |
||
247 | |||
248 | foreach ($resources as $i => $res) { |
||
249 | $data[$i] = array(); |
||
250 | |||
251 | foreach ($res as $key => $value) { |
||
252 | // If $select is empty, we assume every field is requested |
||
253 | if (in_array($key, $select) || empty($select)) { |
||
254 | $data[$i][$key] = $value; |
||
255 | } |
||
256 | } |
||
257 | } |
||
258 | |||
259 | return $data; |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Given the type and handle of a resource, return the extension it belongs to. |
||
264 | * |
||
265 | * @param integer $type |
||
266 | * The resource type, either `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DS` |
||
267 | * @param string $r_handle |
||
268 | * The handle of the resource. |
||
269 | * @throws Exception |
||
270 | * @return string |
||
271 | * The extension handle. |
||
272 | */ |
||
273 | public static function __getExtensionFromHandle($type, $r_handle) |
||
290 | } |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Given the resource handle, this function will return an associative |
||
295 | * array of Page information, filtered by the pages the resource is attached to. |
||
296 | * |
||
297 | * @param integer $type |
||
298 | * The resource type, either `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DS` |
||
299 | * @param string $r_handle |
||
300 | * The handle of the resource. |
||
301 | * @return array |
||
302 | * An associative array of Page information, according to the pages the resource is attached to. |
||
303 | */ |
||
304 | public static function getAttachedPages($type, $r_handle) |
||
305 | { |
||
306 | $col = self::getColumnFromType($type); |
||
307 | |||
308 | $pages = PageManager::fetch(false, array('id'), array(sprintf( |
||
309 | '`%s` = "%s" OR `%s` REGEXP "%s"', |
||
310 | $col, |
||
311 | $r_handle, |
||
312 | $col, |
||
313 | '^' . $r_handle . ',|,' . $r_handle . ',|,' . $r_handle . '$' |
||
314 | ))); |
||
315 | |||
316 | if (is_array($pages)) { |
||
317 | foreach ($pages as $key => &$page) { |
||
318 | $pages[$key] = array( |
||
319 | 'id' => $page['id'], |
||
320 | 'title' => PageManager::resolvePageTitle($page['id']) |
||
321 | ); |
||
322 | } |
||
323 | } |
||
324 | |||
325 | return (is_null($pages) ? array() : $pages); |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Given a resource type, a handle and a page, this function will attach |
||
330 | * the given handle (which represents either a datasource or event) to that page. |
||
331 | * |
||
332 | * @param integer $type |
||
333 | * The resource type, either `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DS` |
||
334 | * @param string $r_handle |
||
335 | * The handle of the resource. |
||
336 | * @param integer $page_id |
||
337 | * The ID of the page. |
||
338 | * @return boolean |
||
339 | */ |
||
340 | public static function attach($type, $r_handle, $page_id) |
||
341 | { |
||
342 | $col = self::getColumnFromType($type); |
||
343 | |||
344 | $pages = PageManager::fetch(false, array($col), array(sprintf( |
||
345 | '`id` = %d', |
||
346 | $page_id |
||
347 | ))); |
||
348 | |||
349 | if (is_array($pages) && count($pages) == 1) { |
||
350 | $result = $pages[0][$col]; |
||
351 | |||
352 | if (!in_array($r_handle, explode(',', $result))) { |
||
353 | if (strlen($result) > 0) { |
||
354 | $result .= ','; |
||
355 | } |
||
356 | |||
357 | $result .= $r_handle; |
||
358 | |||
359 | return PageManager::edit($page_id, array( |
||
360 | $col => MySQL::cleanValue($result) |
||
361 | )); |
||
362 | } |
||
363 | } |
||
364 | |||
365 | return false; |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * Given a resource type, a handle and a page, this function detaches |
||
370 | * the given handle (which represents either a datasource or event) to that page. |
||
371 | * |
||
372 | * @param integer $type |
||
373 | * The resource type, either `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DS` |
||
374 | * @param string $r_handle |
||
375 | * The handle of the resource. |
||
376 | * @param integer $page_id |
||
377 | * The ID of the page. |
||
378 | * @return boolean |
||
379 | */ |
||
380 | public static function detach($type, $r_handle, $page_id) |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * Given a resource type, a handle and an array of pages, this function will |
||
410 | * ensure that the resource is attached to the given pages. Note that this |
||
411 | * function will also remove the resource from all pages that are not provided |
||
412 | * in the `$pages` parameter. |
||
413 | * |
||
414 | * @since Symphony 2.4 |
||
415 | * @param integer $type |
||
416 | * The resource type, either `RESOURCE_TYPE_EVENT` or `RESOURCE_TYPE_DS` |
||
417 | * @param string $r_handle |
||
418 | * The handle of the resource. |
||
419 | * @param array $pages |
||
420 | * An array of Page ID's to attach this resource to. |
||
421 | * @return boolean |
||
422 | */ |
||
423 | public static function setPages($type, $r_handle, $pages = array()) |
||
454 |