Total Complexity | 46 |
Total Lines | 293 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like admin_statistics 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 admin_statistics, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class admin_statistics |
||
20 | { |
||
21 | const CONFIG_APP = 'admin'; |
||
22 | const CONFIG_LAST_SUBMIT = 'last_statistics_submit'; |
||
23 | const CONFIG_POSTPONE_SUBMIT = 'postpone_statistics_submit'; |
||
24 | const CONFIG_SUBMIT_ID = 'statistics_submit_id'; |
||
25 | const CONFIG_COUNTRY = 'country_submit'; |
||
26 | const CONFIG_USAGE_TYPE = 'usage_type_submit'; |
||
27 | const CONFIG_INSTALL_TYPE = 'install_type_submit'; |
||
28 | |||
29 | const SUBMIT_URL = 'https://www.egroupware.org/usage-statistic'; |
||
30 | const STATISTIC_URL = 'https://www.egroupware.org/usage-statistic'; |
||
31 | |||
32 | const SUBMISION_RATE = 2592000; // 30 days |
||
33 | |||
34 | /** |
||
35 | * Which methods of this class can be called as menuation |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | public $public_functions = array( |
||
40 | 'submit' => true, |
||
41 | ); |
||
42 | |||
43 | /** |
||
44 | * Display and allow to submit statistical data |
||
45 | * |
||
46 | * @param array $_content =null |
||
47 | */ |
||
48 | public function submit($_content=null) |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Gather statistical data to submit |
||
150 | * |
||
151 | * @return array key => value pairs |
||
152 | */ |
||
153 | protected static function gather_data() |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Get percentage of users allowed to use an application |
||
223 | * |
||
224 | * @param string $app |
||
225 | * @return int number of users allowed to run application |
||
226 | */ |
||
227 | static function gather_app_users($app) |
||
228 | { |
||
229 | $users = array(); |
||
230 | if (($access = $GLOBALS['egw']->acl->get_ids_for_location('run',1,$app))) |
||
231 | { |
||
232 | foreach($access as $uid) |
||
233 | { |
||
234 | if ($uid > 0) |
||
235 | { |
||
236 | $users[] = $uid; |
||
237 | } |
||
238 | elseif (($members = $GLOBALS['egw']->accounts->members($uid,true))) |
||
239 | { |
||
240 | $users = array_merge($users,$members); |
||
241 | } |
||
242 | } |
||
243 | $users = array_unique($users); |
||
244 | } |
||
245 | return count($users); |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * Get percentage of users allowed to use an application |
||
250 | * |
||
251 | * @param string $app |
||
252 | * @return int |
||
253 | */ |
||
254 | static function gather_app_entries($app) |
||
255 | { |
||
256 | // main table for each application |
||
257 | static $app2table = array( |
||
258 | 'addressbook' => 'egw_addressbook', |
||
259 | 'bookmarks' => 'egw_bookmarks', |
||
260 | 'calendar' => 'egw_cal_dates', |
||
261 | 'infolog' => 'egw_infolog', |
||
262 | 'filemanager' => 'egw_sqlfs', |
||
263 | 'gallery' => 'g2_Item', |
||
264 | 'news_admin' => 'egw_news WHERE news_submittedby > 0', // exclude imported rss feeds |
||
265 | 'polls' => 'egw_polls', |
||
266 | 'projectmanager' => 'egw_pm_projects', |
||
267 | 'phpbrain' => 'egw_kb_articles', |
||
268 | 'resources' => 'egw_resources', |
||
269 | 'sitemgr' => 'egw_sitemgr_pages', |
||
270 | 'syncml' => 'egw_syncmlsummary', |
||
271 | 'timesheet' => 'egw_timesheet', |
||
272 | 'tracker' => 'egw_tracker', |
||
273 | 'wiki' => 'egw_wiki_pages', |
||
274 | 'mydms' => 'phpgw_mydms_Documents', |
||
275 | ); |
||
276 | if (($table = $app2table[$app])) |
||
277 | { |
||
278 | try { |
||
279 | $entries = (int)$GLOBALS['egw']->db->query('SELECT COUNT(*) FROM '.$table)->fetchColumn(); |
||
280 | //echo "$app ($table): $entries<br />\n"; |
||
281 | } |
||
282 | catch(Api\Db\Exception $e) { |
||
283 | unset($e); |
||
284 | $entries = null; |
||
285 | } |
||
286 | } |
||
287 | return $entries; |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Check if next submission is due, in which case we call submit and NOT return to the admin hook |
||
292 | * |
||
293 | * @param boolean $redirect should we redirect or return true |
||
294 | * @return boolean true if statistic submission is due |
||
295 | */ |
||
296 | public static function check($redirect=true) |
||
312 | )); |
||
313 | } |
||
314 | } |
||
315 |