Total Complexity | 40 |
Total Lines | 250 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like XoopsAvatarHandler 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 XoopsAvatarHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
180 | class XoopsAvatarHandler extends XoopsObjectHandler |
||
181 | { |
||
182 | /** |
||
183 | * Create new Object |
||
184 | * |
||
185 | * @param bool $isNew |
||
186 | * @return XoopsAvatar |
||
187 | */ |
||
188 | public function create($isNew = true) |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Egt Object |
||
200 | * |
||
201 | * @param int $id |
||
202 | * @return XoopsAvatar|false |
||
203 | */ |
||
204 | public function get($id) |
||
205 | { |
||
206 | $avatar = false; |
||
207 | $id = (int)$id; |
||
208 | if ($id > 0) { |
||
209 | $sql = 'SELECT * FROM ' . $this->db->prefix('avatar') . ' WHERE avatar_id=' . $id; |
||
210 | $result = $this->db->query($sql); |
||
|
|||
211 | if (!$this->db->isResultSet($result)) { |
||
212 | return false; |
||
213 | } |
||
214 | $numrows = $this->db->getRowsNum($result); |
||
215 | if ($numrows == 1) { |
||
216 | $avatar = new XoopsAvatar(); |
||
217 | $avatar->assignVars($this->db->fetchArray($result)); |
||
218 | |||
219 | return $avatar; |
||
220 | } |
||
221 | } |
||
222 | |||
223 | return $avatar; |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Insert and Object into the database |
||
228 | * |
||
229 | * @param XoopsObject|XoopsAvatar $avatar a XoopsAvatar object |
||
230 | * |
||
231 | * @return bool true on success, otherwise false |
||
232 | */ |
||
233 | public function insert(XoopsObject $avatar) |
||
234 | { |
||
235 | $className = 'XoopsAvatar'; |
||
236 | if (!($avatar instanceof $className)) { |
||
237 | return false; |
||
238 | } |
||
239 | if (!$avatar->isDirty()) { |
||
240 | return true; |
||
241 | } |
||
242 | if (!$avatar->cleanVars()) { |
||
243 | return false; |
||
244 | } |
||
245 | foreach ($avatar->cleanVars as $k => $v) { |
||
246 | ${$k} = $v; |
||
247 | } |
||
248 | if ($avatar->isNew()) { |
||
249 | $avatar_id = $this->db->genId('avatar_avatar_id_seq'); |
||
250 | $sql = sprintf('INSERT INTO %s (avatar_id, avatar_file, avatar_name, avatar_created, avatar_mimetype, avatar_display, avatar_weight, avatar_type) VALUES (%u, %s, %s, %u, %s, %u, %u, %s)', $this->db->prefix('avatar'), $avatar_id, $this->db->quoteString($avatar_file), $this->db->quoteString($avatar_name), time(), $this->db->quoteString($avatar_mimetype), $avatar_display, $avatar_weight, $this->db->quoteString($avatar_type)); |
||
251 | } else { |
||
252 | $sql = sprintf('UPDATE %s SET avatar_file = %s, avatar_name = %s, avatar_created = %u, avatar_mimetype= %s, avatar_display = %u, avatar_weight = %u, avatar_type = %s WHERE avatar_id = %u', $this->db->prefix('avatar'), $this->db->quoteString($avatar_file), $this->db->quoteString($avatar_name), $avatar_created, $this->db->quoteString($avatar_mimetype), $avatar_display, $avatar_weight, $this->db->quoteString($avatar_type), $avatar_id); |
||
253 | } |
||
254 | if (!$result = $this->db->query($sql)) { |
||
255 | return false; |
||
256 | } |
||
257 | if (empty($avatar_id)) { |
||
258 | $avatar_id = $this->db->getInsertId(); |
||
259 | } |
||
260 | $avatar->assignVar('avatar_id', $avatar_id); |
||
261 | |||
262 | return true; |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * Delete an object from the database |
||
267 | * |
||
268 | * @param XoopsObject|XoopsAvatar $avatar a XoopsAvatar object |
||
269 | * |
||
270 | * @return bool true on success, otherwise false |
||
271 | */ |
||
272 | public function delete(XoopsObject $avatar) |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Fetch a row of objects from the database |
||
292 | * |
||
293 | * @param CriteriaElement|CriteriaCompo $criteria |
||
294 | * @param bool $id_as_key |
||
295 | * @return array |
||
296 | */ |
||
297 | public function &getObjects(CriteriaElement $criteria = null, $id_as_key = false) |
||
298 | { |
||
299 | $ret = array(); |
||
300 | $limit = $start = 0; |
||
301 | $sql = 'SELECT a.*, COUNT(u.user_id) AS count FROM ' . $this->db->prefix('avatar') . ' a LEFT JOIN ' . $this->db->prefix('avatar_user_link') . ' u ON u.avatar_id=a.avatar_id'; |
||
302 | if (isset($criteria) && \method_exists($criteria, 'renderWhere')) { |
||
303 | $sql .= ' ' . $criteria->renderWhere(); |
||
304 | $sql .= ' GROUP BY a.avatar_id ORDER BY avatar_weight, avatar_id'; |
||
305 | $limit = $criteria->getLimit(); |
||
306 | $start = $criteria->getStart(); |
||
307 | } |
||
308 | $result = $this->db->query($sql, $limit, $start); |
||
309 | if (!$this->db->isResultSet($result)) { |
||
310 | throw new \RuntimeException( |
||
311 | \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(), E_USER_ERROR |
||
312 | ); |
||
313 | } |
||
314 | /** @var array $myrow */ |
||
315 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
316 | $avatar = new XoopsAvatar(); |
||
317 | $avatar->assignVars($myrow); |
||
318 | $avatar->setUserCount($myrow['count']); |
||
319 | if (!$id_as_key) { |
||
320 | $ret[] = &$avatar; |
||
321 | } else { |
||
322 | $ret[$myrow['avatar_id']] = &$avatar; |
||
323 | } |
||
324 | unset($avatar); |
||
325 | } |
||
326 | |||
327 | return $ret; |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * Get count |
||
332 | * |
||
333 | * @param CriteriaElement|CriteriaCompo $criteria |
||
334 | * @return int |
||
335 | */ |
||
336 | public function getCount(CriteriaElement $criteria = null) |
||
337 | { |
||
338 | |||
339 | |||
340 | $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('avatar'); |
||
341 | if (isset($criteria) && \method_exists($criteria, 'renderWhere')) { |
||
342 | $sql .= ' ' . $criteria->renderWhere(); |
||
343 | } |
||
344 | $result = $this->db->query($sql); |
||
345 | if (!$this->db->isResultSet($result)) { |
||
346 | return 0; |
||
347 | } |
||
348 | |||
349 | list($count) = $this->db->fetchRow($result); |
||
350 | |||
351 | return (int)$count; |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * Add user |
||
356 | * |
||
357 | * @param int $avatar_id |
||
358 | * @param int $user_id |
||
359 | * @return bool |
||
360 | */ |
||
361 | public function addUser($avatar_id, $user_id) |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * Get User |
||
380 | * |
||
381 | * @param XoopsAvatar $avatar |
||
382 | * @return array|false |
||
383 | */ |
||
384 | public function getUser(XoopsAvatar $avatar) |
||
404 | } |
||
405 | |||
406 | /** |
||
407 | * Get a list of Avatars |
||
408 | * |
||
409 | * @param string|null $avatar_type 'S' for system, 'C' for custom |
||
410 | * @param bool|null $avatar_display null lists all, bool respects avatar_display |
||
411 | * @return array |
||
412 | */ |
||
413 | public function getList($avatar_type = null, $avatar_display = null) |
||
432 |