Total Complexity | 47 |
Total Lines | 365 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like memberavatarstatus 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 memberavatarstatus, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class memberavatarstatus |
||
25 | { |
||
26 | /** @var int No Avatar Size */ |
||
27 | const NO_AVATAR_SIZE = 1000; |
||
28 | |||
29 | /** @var int Avatar Minimum Size */ |
||
30 | const AV_MIN_SZ = 9; |
||
31 | |||
32 | /** @var int Avatar Default Size Small */ |
||
33 | const AV_DEF_SZ_SML = 2.0; |
||
34 | |||
35 | /** @var int Avatar Default Size Big */ |
||
36 | const AV_DEF_SZ_BIG = 50; |
||
37 | |||
38 | /** @var int Avatar Maximum Size Small */ |
||
39 | const AV_MAX_SZ_SML = 99; |
||
40 | |||
41 | /** @var int Avatar Maximum Size Big */ |
||
42 | const AV_MAX_SZ_BIG = 999; |
||
43 | |||
44 | /** @var \phpbb\auth\auth */ |
||
45 | protected $auth; |
||
46 | |||
47 | /** @var \phpbb\config\config */ |
||
48 | protected $config; |
||
49 | |||
50 | /** @var \phpbb\language\language */ |
||
51 | protected $language; |
||
52 | |||
53 | /** @var \phpbb\log\log */ |
||
54 | protected $phpbb_log; |
||
55 | |||
56 | /** @var string */ |
||
57 | protected $phpbb_root_path; |
||
58 | |||
59 | /** |
||
60 | * Constructor for listener |
||
61 | * |
||
62 | * @param \phpbb\auth\auth $auth phpBB auth |
||
63 | * @param \phpbb\config\config $config phpBB config |
||
64 | * @param \phpbb\language\language $language phpBB language |
||
65 | * @param \phpbb\log\log $phpbb_log phpBB log |
||
66 | * @param \ $root_path phpBB root_path |
||
67 | * @access public |
||
68 | */ |
||
69 | public function __construct( |
||
70 | auth $auth, |
||
71 | config $config, |
||
72 | language $language, |
||
73 | log $phpbb_log, |
||
74 | $phpbb_root_path |
||
75 | ) |
||
76 | { |
||
77 | $this->auth = $auth; |
||
78 | $this->config = $config; |
||
79 | $this->language = $language; |
||
80 | $this->phpbb_log = $phpbb_log; |
||
81 | $this->phpbb_root_path = $phpbb_root_path; |
||
82 | } |
||
83 | |||
84 | |||
85 | |||
86 | /** |
||
87 | * MAS Get Avatar SQL Query |
||
88 | * |
||
89 | * @param String $config_key takes Config Key String |
||
90 | * @param String $sql_uid Specifies User ID to be Matched with. |
||
91 | * @param String $sql_obj Specifies SQL Object |
||
92 | * @param String $prefix Specifies the prefix to be Set in SQL Select |
||
93 | * @return Array of data |
||
94 | * @access public |
||
95 | */ |
||
96 | public function mas_avatar_sql_query($sql_ary, $config_key, $sql_uid, $sql_obj, $prefix, $lj_on_ex = '') |
||
97 | { |
||
98 | $config_key .= '_av'; |
||
99 | $prefix = ($prefix != '') ? $prefix .= '_' : $prefix; |
||
100 | |||
101 | if ($this->config['allow_avatar'] && $this->config[$config_key]) |
||
102 | { |
||
103 | $sql_ary['SELECT'] .= ', ' . $sql_obj . '.user_avatar as ' . $prefix . 'avatar, ' . $sql_obj . '.user_avatar_type as ' . $prefix . 'avatar_type, ' . $sql_obj . '.user_avatar_width as ' . $prefix . 'avatar_width, ' . $sql_obj . '.user_avatar_height as ' . $prefix . 'avatar_height'; |
||
104 | $sql_ary['LEFT_JOIN'][] = array( |
||
105 | 'FROM' => array(USERS_TABLE => $sql_obj), |
||
|
|||
106 | 'ON' => $sql_uid . ' = ' . $sql_obj . '.user_id' . $lj_on_ex, |
||
107 | ); |
||
108 | } |
||
109 | |||
110 | return $sql_ary; |
||
111 | } |
||
112 | |||
113 | |||
114 | |||
115 | /** |
||
116 | * MAS Get Avatar Size |
||
117 | * |
||
118 | * @param int $av_sz takes Avatar Size |
||
119 | * @param int $av_default_sz Specifies Default Size in px |
||
120 | * @param int $av_max_sz Specifies Avatar MAX Size in px |
||
121 | * @return int with Avatar Size in px |
||
122 | * @access public |
||
123 | */ |
||
124 | public function mas_get_avatar_size($av_sz, $av_default_sz = self::AV_DEF_SZ_SML, $av_max_sz = self::AV_MAX_SZ_SML) |
||
125 | { |
||
126 | // $av_sz , Need to set this between self::AV_MIN_SZ to $av_max_sz Only , Default is $av_default_sz. |
||
127 | if ($av_sz < self::AV_MIN_SZ || $av_sz > $av_max_sz) |
||
128 | { |
||
129 | $av_sz = $av_default_sz; |
||
130 | } |
||
131 | return $av_sz; |
||
132 | } |
||
133 | |||
134 | |||
135 | |||
136 | /** |
||
137 | * MAS Get Config Avatar Size |
||
138 | * |
||
139 | * @param String $config_key takes Config Key String |
||
140 | * @param int $av_default_sz Specifies Default Size in px |
||
141 | * @param int $av_max_sz Specifies Avatar MAX Size in px |
||
142 | * @return int with Avatar Size in px |
||
143 | * @access public |
||
144 | */ |
||
145 | public function mas_get_config_avatar_size($config_key, $av_default_sz = self::AV_DEF_SZ_SML, $av_max_sz = self::AV_MAX_SZ_SML) |
||
146 | { |
||
147 | |||
148 | // config -> dark1_mas_XX_sz , Need to set this between self::AV_MIN_SZ to $av_max_sz Only , Default is $av_default_sz. |
||
149 | $av_sz = $this->mas_get_avatar_size($this->config[$config_key], $av_default_sz, $av_max_sz); |
||
150 | |||
151 | // Check if correction is required. |
||
152 | if ($av_sz != $this->config[$config_key]) |
||
153 | { |
||
154 | $this->config->set($config_key, $av_sz); |
||
155 | $this->phpbb_log->add('admin', '', '', 'MAS_LOG_CONFIG', time(), array($config_key, $this->language->lang('MAS_ERR_AV_SIZE'), $av_default_sz)); |
||
156 | } |
||
157 | return $this->config[$config_key]; |
||
158 | } |
||
159 | |||
160 | |||
161 | |||
162 | /** |
||
163 | * MAS Get No Avatar IMG |
||
164 | * |
||
165 | * @param Void |
||
166 | * @return String with No Avatar IMG |
||
167 | * @access public |
||
168 | */ |
||
169 | public function mas_get_no_avatar_img() |
||
170 | { |
||
171 | $avatar_row = array( |
||
172 | 'avatar' => $this->phpbb_root_path . 'ext/dark1/memberavatarstatus/image/avatar.png', |
||
173 | 'avatar_type' => AVATAR_REMOTE, |
||
174 | 'avatar_width' => self::NO_AVATAR_SIZE, |
||
175 | 'avatar_height' => self::NO_AVATAR_SIZE, |
||
176 | ); |
||
177 | return str_replace('" />', '" title="' . $this->language->lang('MAS_NO_AVATAR_TEXT') . '" />', phpbb_get_user_avatar($avatar_row, $this->language->lang('MAS_NO_AVATAR_TEXT'))); |
||
178 | } |
||
179 | |||
180 | |||
181 | |||
182 | /** |
||
183 | * MAS Get Avatar |
||
184 | * |
||
185 | * @param String $config_key takes Config Key String |
||
186 | * @param String $prefix Specifies the prefix to be Searched in the $row |
||
187 | * @param Array $row is array of data |
||
188 | * @return String with Avatar Data |
||
189 | * @access public |
||
190 | */ |
||
191 | public function mas_get_avatar($config_key, $prefix, $row) |
||
192 | { |
||
193 | $avatar = ''; |
||
194 | $config_key .= '_av'; |
||
195 | $prefix = ($prefix != '') ? $prefix .= '_' : $prefix; |
||
196 | |||
197 | if ($this->config['allow_avatar'] && $this->config[$config_key]) |
||
198 | { |
||
199 | // $avatar_row |
||
200 | $avatar_row = array( |
||
201 | 'avatar' => $row[$prefix . 'avatar'], |
||
202 | 'avatar_type' => $row[$prefix . 'avatar_type'], |
||
203 | 'avatar_width' => $row[$prefix . 'avatar_width'], |
||
204 | 'avatar_height' => $row[$prefix . 'avatar_height'], |
||
205 | ); |
||
206 | $avatar = phpbb_get_user_avatar($avatar_row); |
||
207 | } |
||
208 | |||
209 | return $avatar; |
||
210 | } |
||
211 | |||
212 | |||
213 | |||
214 | /** |
||
215 | * MAS Get Online SQL Query |
||
216 | * |
||
217 | * @param String $config_key takes Config Key String |
||
218 | * @param String $sql_uid Specifies User ID to be Matched with. |
||
219 | * @param String $sql_obj Specifies SQL Object |
||
220 | * @param String $prefix Specifies the prefix to be Set in SQL Select |
||
221 | * @return Array of data |
||
222 | * @access public |
||
223 | */ |
||
224 | public function mas_online_sql_query($sql_ary, $config_key, $sql_uid, $sql_obj, $prefix, $lj_on_ex = '') |
||
225 | { |
||
226 | $config_key .= '_ol'; |
||
227 | $prefix = ($prefix != '') ? $prefix .= '_' : $prefix; |
||
228 | |||
229 | if ($this->config['load_onlinetrack'] && $this->config[$config_key]) |
||
230 | { |
||
231 | $sql_ary['SELECT'] .= ', ' . $sql_obj . '.session_time as ' . $prefix . 'session_time, ' . $sql_obj . '.session_viewonline as ' . $prefix . 'session_viewonline'; |
||
232 | $sql_ary['LEFT_JOIN'][] = array( |
||
233 | 'FROM' => array(SESSIONS_TABLE => $sql_obj), |
||
234 | 'ON' => $sql_uid . ' = ' . $sql_obj . '.session_user_id AND ' . $sql_obj . '.session_time >= ' . (time() - ($this->config['load_online_time'] * 60)) . ' AND ' . $sql_obj . '.session_user_id <> ' . ANONYMOUS . $lj_on_ex, |
||
235 | ); |
||
236 | } |
||
237 | |||
238 | return $sql_ary; |
||
239 | } |
||
240 | |||
241 | |||
242 | |||
243 | /** |
||
244 | * MAS Get Online Status |
||
245 | * |
||
246 | * @param Array $online_row takes user details to find Online Status |
||
247 | * @return Bool Online Status |
||
248 | * @access public |
||
249 | */ |
||
250 | public function mas_get_online_status($online_row) |
||
251 | { |
||
252 | $online = false; |
||
253 | |||
254 | if ($this->config['load_onlinetrack']) |
||
255 | { |
||
256 | $online = (time() - ($this->config['load_online_time'] * 60) < $online_row['session_time'] && ((isset($online_row['session_viewonline']) && $online_row['session_viewonline']) || $this->auth->acl_get('u_viewonline'))) ? true : false; |
||
257 | } |
||
258 | |||
259 | return $online; |
||
260 | } |
||
261 | |||
262 | |||
263 | |||
264 | /** |
||
265 | * MAS Get Online |
||
266 | * |
||
267 | * @param String $config_key takes Config Key String |
||
268 | * @param String $prefix Specifies the prefix to be Searched in the $row |
||
269 | * @param Array $row is array of data |
||
270 | * @return String with Online Data |
||
271 | * @access public |
||
272 | */ |
||
273 | public function mas_get_online($config_key, $prefix, $row) |
||
274 | { |
||
275 | $online = ''; |
||
276 | $config_key .= '_ol'; |
||
277 | $prefix = ($prefix != '') ? $prefix .= '_' : $prefix; |
||
278 | |||
279 | if ($this->config['load_onlinetrack'] && $this->config[$config_key]) |
||
280 | { |
||
281 | $online_row = array( |
||
282 | 'session_time' => $row[$prefix . 'session_time'], |
||
283 | 'session_viewonline' => $row[$prefix . 'session_viewonline'], |
||
284 | ); |
||
285 | $online = $this->mas_get_online_status($online_row); |
||
286 | } |
||
287 | |||
288 | return $online; |
||
289 | } |
||
290 | |||
291 | |||
292 | |||
293 | /** |
||
294 | * MAS Get converted simple SQL strings in array |
||
295 | * |
||
296 | * @param Array $sql_ary is array of data |
||
297 | * @return Array of data |
||
298 | * @access public |
||
299 | */ |
||
300 | public function mas_convert_sql($sql_ary) |
||
301 | { |
||
302 | $sql_select = $sql_ary['SELECT']; |
||
303 | $sql_from = ''; |
||
304 | |||
305 | if (!empty($sql_ary['LEFT_JOIN'])) |
||
306 | { |
||
307 | foreach ($sql_ary['LEFT_JOIN'] as $join) |
||
308 | { |
||
309 | if (!empty($join)) |
||
310 | { |
||
311 | $sql_from .= ' LEFT JOIN ' . key($join['FROM']) . ' ' . current($join['FROM']) . ' ON (' . $join['ON'] . ')'; |
||
312 | } |
||
313 | } |
||
314 | } |
||
315 | |||
316 | return array( |
||
317 | 'sql_select' => $sql_select, |
||
318 | 'sql_from' => $sql_from, |
||
319 | ); |
||
320 | } |
||
321 | |||
322 | |||
323 | |||
324 | // Following Functions Will be Deprecated in future when Style Template Events are created. |
||
325 | |||
326 | |||
327 | |||
328 | /** |
||
329 | * MAS Get Avatar IMG |
||
330 | * |
||
331 | * @param String $avatar takes User Avatar IMG |
||
332 | * @param int $avatar_size Specifies Avatar Size in px |
||
333 | * @return String with Wrapped User Avatar IMG |
||
334 | * @access public |
||
335 | * |
||
336 | * Will be Deprecated in future when Style Template Events are created. |
||
337 | */ |
||
338 | public function mas_get_avatar_img($avatar, $avatar_size = self::AV_DEF_SZ_SML) |
||
339 | { |
||
340 | $start_avatar = '<div class="mas-avatar" style="width: ' . $avatar_size . 'px; height: ' . $avatar_size . 'px;">'; |
||
341 | $end_avatar = '</div>'; |
||
342 | return $start_avatar . (($avatar) ? $avatar : $this->mas_get_no_avatar_img()) . $end_avatar; |
||
343 | } |
||
344 | |||
345 | |||
346 | |||
347 | /** |
||
348 | * MAS Get Online Status DOT |
||
349 | * |
||
350 | * @param String $online takes User Online Status |
||
351 | * @return String with Wrapped User Online Status |
||
352 | * @access public |
||
353 | * |
||
354 | * Will be Deprecated in future when Style Template Events are created. |
||
355 | */ |
||
356 | public function mas_get_online_status_dot($online) |
||
357 | { |
||
358 | $online_text = $this->language->lang('ONLINE'); |
||
359 | $offline_text = $this->language->lang('OFFLINE'); |
||
360 | $start_online = ' ' . '<div class="mas-wrap-status' . ($online ? ' mas-status-online' : '') . '">'; |
||
361 | $end_online = '</div>'; |
||
362 | $online_dot = '<span class="mas-status-dot" title="' . ($online ? $online_text : $offline_text) . '"/>'; |
||
363 | return $start_online . $online_dot . $end_online; |
||
364 | } |
||
365 | |||
366 | |||
367 | |||
368 | /** |
||
369 | * MAS Get UserName Wrap |
||
370 | * |
||
371 | * @param String $username takes UserName |
||
372 | * @param String $avatar takes User Avatar IMG |
||
373 | * @param String $online takes User Online Status |
||
374 | * @return String with Wrapped Main & UserName |
||
375 | * @access public |
||
376 | * |
||
377 | * Will be Deprecated in future when Style Template Events are created. |
||
378 | */ |
||
379 | public function mas_get_username_wrap($username, $config_key, $avatar, $online) |
||
389 | } |
||
390 | |||
391 | } |