| Total Complexity | 47 |
| Total Lines | 417 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like PicturesHandler 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 PicturesHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 53 | class PicturesHandler extends XoopsObjectHandler |
||
| 54 | { |
||
| 55 | /** |
||
| 56 | * Class constructor |
||
| 57 | * @param XoopsDatabase $db |
||
| 58 | */ |
||
| 59 | |||
| 60 | public function __construct($db) |
||
| 61 | { |
||
| 62 | parent::__construct($db, 'adslight_pictures', 'Pictures', 'cod_img', 'title'); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * create a new light_pictures |
||
| 67 | * |
||
| 68 | * @param bool $isNew flag the new objects as "new"? |
||
| 69 | * @return XoopsObject light_pictures |
||
| 70 | */ |
||
| 71 | public function create($isNew = true) |
||
| 72 | { |
||
| 73 | $adslightPictures = new Pictures(); |
||
| 74 | if ($isNew) { |
||
| 75 | $adslightPictures->setNew(); |
||
| 76 | } else { |
||
| 77 | $adslightPictures->unsetNew(); |
||
| 78 | } |
||
| 79 | |||
| 80 | return $adslightPictures; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * retrieve a light_pictures |
||
| 85 | * |
||
| 86 | * @param int $id of the light_pictures |
||
| 87 | * @param $lid |
||
| 88 | * |
||
| 89 | * @return mixed reference to the {@link light_pictures} object, FALSE if failed |
||
| 90 | */ |
||
| 91 | public function get($id, $lid = null) |
||
| 92 | { |
||
| 93 | global $moduleDirName; |
||
| 94 | |||
| 95 | $sql = 'SELECT * FROM ' . $this->db->prefix('adslight_pictures') . ' WHERE cod_img=' . $id . ' AND lid=' . $lid . ''; |
||
| 96 | if (!$result = $this->db->query($sql)) { |
||
| 97 | return false; |
||
| 98 | } |
||
| 99 | $numrows = $this->db->getRowsNum($result); |
||
| 100 | if (1 == $numrows) { |
||
| 101 | $adslightPictures = new Pictures(); |
||
| 102 | $adslightPictures->assignVars($this->db->fetchArray($result)); |
||
| 103 | |||
| 104 | return $adslightPictures; |
||
| 105 | } |
||
| 106 | |||
| 107 | return false; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * insert a new AdslightPicture object into the database |
||
| 112 | * |
||
| 113 | * @param XoopsObject $adslightPictures |
||
| 114 | * @param bool $force |
||
| 115 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
| 116 | */ |
||
| 117 | public function insert(\XoopsObject $adslightPictures, $force = false) |
||
| 118 | { |
||
| 119 | global $xoopsConfig, $lid, $moduleDirName; |
||
| 120 | if (!$adslightPictures instanceof Pictures) { |
||
| 121 | return false; |
||
| 122 | } |
||
| 123 | if (!$adslightPictures->isDirty()) { |
||
| 124 | return true; |
||
| 125 | } |
||
| 126 | if (!$adslightPictures->cleanVars()) { |
||
| 127 | return false; |
||
| 128 | } |
||
| 129 | foreach ($adslightPictures->cleanVars as $k => $v) { |
||
| 130 | ${$k} = $v; |
||
| 131 | } |
||
| 132 | $now = time(); |
||
| 133 | if ($adslightPictures->isNew()) { |
||
| 134 | // add/modify of Pictures |
||
| 135 | $adslightPictures = new Pictures(); |
||
| 136 | |||
| 137 | $format = 'INSERT INTO %s (cod_img, title, date_added, date_modified, lid, uid_owner, url)'; |
||
| 138 | $format .= 'VALUES (%u, %s, %s, %s, %s, %s, %s)'; |
||
| 139 | $sql = sprintf($format, $this->db->prefix('adslight_pictures'), $cod_img, $this->db->quoteString($title), $now, $now, $this->db->quoteString($lid), $this->db->quoteString($uid_owner), $this->db->quoteString($url)); |
||
| 140 | $force = true; |
||
| 141 | } else { |
||
| 142 | $format = 'UPDATE %s SET '; |
||
| 143 | $format .= 'cod_img=%u, title=%s, date_added=%s, date_modified=%s, lid=%s, uid_owner=%s, url=%s'; |
||
| 144 | $format .= ' WHERE cod_img = %u'; |
||
| 145 | $sql = sprintf($format, $this->db->prefix('adslight_pictures'), $cod_img, $this->db->quoteString($title), $now, $now, $this->db->quoteString($lid), $this->db->quoteString($uid_owner), $this->db->quoteString($url), $cod_img); |
||
| 146 | } |
||
| 147 | if (false !== $force) { |
||
| 148 | $result = $this->db->queryF($sql); |
||
| 149 | } else { |
||
| 150 | $result = $this->db->query($sql); |
||
| 151 | } |
||
| 152 | if (!$result) { |
||
| 153 | return false; |
||
| 154 | } |
||
| 155 | if (empty($cod_img)) { |
||
| 156 | $cod_img = $this->db->getInsertId(); |
||
| 157 | } |
||
| 158 | $adslightPictures->assignVars([ |
||
| 159 | 'cod_img' => $cod_img, |
||
| 160 | 'lid' => $lid, |
||
| 161 | 'url' => $url |
||
| 162 | ]); |
||
| 163 | |||
| 164 | return true; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * delete Pictures object from the database |
||
| 169 | * |
||
| 170 | * @param XoopsObject $adslightPictures reference to the Pictures to delete |
||
| 171 | * @param bool $force |
||
| 172 | * @return bool FALSE if failed. |
||
| 173 | */ |
||
| 174 | public function delete(\XoopsObject $adslightPictures, $force = false) |
||
| 175 | { |
||
| 176 | global $moduleDirName; |
||
| 177 | |||
| 178 | if (!$adslightPictures instanceof Pictures) { |
||
| 179 | return false; |
||
| 180 | } |
||
| 181 | $sql = sprintf('DELETE FROM %s WHERE cod_img = %u', $this->db->prefix('adslight_pictures'), $adslightPictures->getVar('cod_img')); |
||
| 182 | if (false !== $force) { |
||
| 183 | $result = $this->db->queryF($sql); |
||
| 184 | } else { |
||
| 185 | $result = $this->db->query($sql); |
||
| 186 | } |
||
| 187 | if (!$result) { |
||
| 188 | return false; |
||
| 189 | } |
||
| 190 | |||
| 191 | return true; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * retrieve Pictures object(s) from the database |
||
| 196 | * |
||
| 197 | * @param CriteriaElement $criteria {@link CriteriaElement} conditions to be met |
||
| 198 | * @param bool $id_as_key use the UID as key for the array? |
||
| 199 | * @return array array of {@link Pictures} objects |
||
| 200 | */ |
||
| 201 | public function &getObjects(CriteriaElement $criteria = null, $id_as_key = false) |
||
| 202 | { |
||
| 203 | global $moduleDirName; |
||
| 204 | |||
| 205 | $ret = []; |
||
| 206 | $limit = $start = 0; |
||
| 207 | $sql = 'SELECT * FROM ' . $this->db->prefix('adslight_pictures'); |
||
| 208 | if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
||
| 209 | $sql .= ' ' . $criteria->renderWhere(); |
||
| 210 | if ('' != $criteria->getSort()) { |
||
| 211 | $sql .= ' ORDER BY ' . $criteria->getSort() . ' ' . $criteria->getOrder(); |
||
| 212 | } |
||
| 213 | $limit = $criteria->getLimit(); |
||
| 214 | $start = $criteria->getStart(); |
||
| 215 | } |
||
| 216 | $result = $this->db->query($sql, $limit, $start); |
||
| 217 | if (!$result) { |
||
| 218 | return $ret; |
||
| 219 | } |
||
| 220 | while (false !== ($myrow = $this->db->fetchArray($result))) { |
||
| 221 | $adslightPictures = new Pictures(); |
||
| 222 | $adslightPictures->assignVars($myrow); |
||
| 223 | if (!$id_as_key) { |
||
| 224 | $ret[] = $adslightPictures; |
||
| 225 | } else { |
||
| 226 | $ret[$myrow['cod_img']] = $adslightPictures; |
||
| 227 | } |
||
| 228 | unset($adslightPictures); |
||
| 229 | } |
||
| 230 | |||
| 231 | return $ret; |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * count Pictures matching a condition |
||
| 236 | * |
||
| 237 | * @param CriteriaElement $criteria {@link CriteriaElement} to match |
||
| 238 | * @return int count of Pictures |
||
| 239 | */ |
||
| 240 | public function getCount(CriteriaElement $criteria = null) |
||
| 241 | { |
||
| 242 | global $moduleDirName; |
||
| 243 | |||
| 244 | $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('adslight_pictures'); |
||
| 245 | if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
||
| 246 | $sql .= ' ' . $criteria->renderWhere(); |
||
| 247 | } |
||
| 248 | $result = $this->db->query($sql); |
||
| 249 | if (!$result) { |
||
| 250 | return 0; |
||
| 251 | } |
||
| 252 | list($count) = $this->db->fetchRow($result); |
||
| 253 | |||
| 254 | return $count; |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * delete Pictures matching a set of conditions |
||
| 259 | * |
||
| 260 | * @param CriteriaElement $criteria {@link CriteriaElement} |
||
| 261 | * @return bool FALSE if deletion failed |
||
| 262 | */ |
||
| 263 | public function deleteAll(CriteriaElement $criteria = null) |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Render a form to send pictures |
||
| 279 | * |
||
| 280 | * @param int $uid |
||
| 281 | * @param int $lid |
||
| 282 | * @param int $maxbytes the maximum size of a picture |
||
| 283 | * @param XoopsTpl $xoopsTpl the one in which the form will be rendered |
||
| 284 | * @return bool TRUE |
||
| 285 | * |
||
| 286 | * obs: Some functions wont work on php 4 so edit lines down under acording to your version |
||
| 287 | */ |
||
| 288 | public function renderFormSubmit($uid, $lid, $maxbytes, $xoopsTpl) |
||
| 289 | { |
||
| 290 | global $moduleDirName, $main_lang; |
||
| 291 | $uid = (int)$uid; |
||
| 292 | $lid = (int)$lid; |
||
| 293 | $form = new \XoopsThemeForm(_ADSLIGHT_SUBMIT_PIC_TITLE, 'form_picture', XOOPS_URL . "/modules/adslight/add_photo.php?lid={$lid}&uid=" . $xoopsUser->getVar('uid'), 'post', true); |
||
| 294 | $field_url = new \XoopsFormFile(_ADSLIGHT_SELECT_PHOTO, 'sel_photo', 2000000); |
||
| 295 | $field_desc = new \XoopsFormText(_ADSLIGHT_CAPTION, 'caption', 35, 55); |
||
| 296 | |||
| 297 | $form->setExtra('enctype="multipart/form-data"'); |
||
| 298 | $button_send = new \XoopsFormButton('', 'submit_button', _ADSLIGHT_UPLOADPICTURE, 'submit'); |
||
| 299 | $field_warning = new \XoopsFormLabel(sprintf(_ADSLIGHT_YOUCANUPLOAD, $maxbytes / 1024)); |
||
| 300 | $field_lid = new \XoopsFormHidden('lid', $lid); |
||
| 301 | $field_uid = new \XoopsFormHidden('uid', $uid); |
||
| 302 | |||
| 303 | $field_token = $GLOBALS['xoopsSecurity']->getTokenHTML(); |
||
| 304 | |||
| 305 | $form->addElement($field_warning); |
||
| 306 | $form->addElement($field_url, true); |
||
| 307 | $form->addElement($field_desc, true); |
||
| 308 | $form->addElement($field_lid, true); |
||
| 309 | $form->addElement($field_uid, true); |
||
| 310 | |||
| 311 | $form->addElement($field_token, true); |
||
| 312 | |||
| 313 | $form->addElement($button_send); |
||
| 314 | if (str_replace('.', '', PHP_VERSION) > 499) { |
||
| 315 | $form->assign($xoopsTpl); |
||
| 316 | } else { |
||
| 317 | $form->display(); |
||
| 318 | } |
||
| 319 | |||
| 320 | return true; |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Render a form to edit the description of the pictures |
||
| 325 | * |
||
| 326 | * @param string $caption The description of the picture |
||
| 327 | * @param int $cod_img the id of the image in database |
||
| 328 | * @param text $filename the url to the thumb of the image so it can be displayed |
||
| 329 | * @return bool TRUE |
||
| 330 | */ |
||
| 331 | public function renderFormEdit($caption, $cod_img, $filename) |
||
| 332 | { |
||
| 333 | global $moduleDirName, $main_lang; |
||
| 334 | |||
| 335 | $form = new \XoopsThemeForm(_ADSLIGHT_EDIT_CAPTION, 'form_picture', 'editdesc.php', 'post', true); |
||
| 336 | $field_desc = new \XoopsFormText($caption, 'caption', 35, 55); |
||
| 337 | $form->setExtra('enctype="multipart/form-data"'); |
||
| 338 | $button_send = new \XoopsFormButton(_ADSLIGHT_EDIT, 'submit_button', _SUBMIT, 'submit'); |
||
| 339 | //@todo - replace alt with language string |
||
| 340 | $field_warning = new \XoopsFormLabel("<img src='{$filename}' alt='sssss'>"); |
||
| 341 | $field_cod_img = new \XoopsFormHidden('cod_img', $cod_img); |
||
| 342 | // $field_lid = new \XoopsFormHidden('lid', $lid); |
||
| 343 | $field_marker = new \XoopsFormHidden('marker', 1); |
||
| 344 | |||
| 345 | $field_token = $GLOBALS['xoopsSecurity']->getTokenHTML(); |
||
| 346 | |||
| 347 | $form->addElement($field_warning); |
||
| 348 | $form->addElement($field_desc); |
||
| 349 | $form->addElement($field_cod_img); |
||
| 350 | $form->addElement($field_marker); |
||
| 351 | $form->addElement($field_token); |
||
| 352 | $form->addElement($button_send); |
||
| 353 | $form->display(); |
||
| 354 | |||
| 355 | return true; |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Upload the file and Save into database |
||
| 360 | * |
||
| 361 | * @param text $title A litle description of the file |
||
| 362 | * @param text $path_upload The path to where the file should be uploaded |
||
| 363 | * @param int $thumbwidth the width in pixels that the thumbnail will have |
||
| 364 | * @param int $thumbheight the height in pixels that the thumbnail will have |
||
| 365 | * @param int $pictwidth the width in pixels that the pic will have |
||
| 366 | * @param int $pictheight the height in pixels that the pic will have |
||
| 367 | * @param int $maxfilebytes the maximum size a file can have to be uploaded in bytes |
||
| 368 | * @param int $maxfilewidth the maximum width in pixels that a pic can have |
||
| 369 | * @param int $maxfileheight the maximum height in pixels that a pic can have |
||
| 370 | * @return bool FALSE if upload fails or database fails |
||
| 371 | */ |
||
| 372 | public function receivePicture($title, $path_upload, $thumbwidth, $thumbheight, $pictwidth, $pictheight, $maxfilebytes, $maxfilewidth, $maxfileheight) |
||
| 373 | { |
||
| 374 | global $xoopsDB, $lid; |
||
| 375 | //busca id do user logado |
||
| 376 | $uid = $GLOBALS['xoopsUser']->getVar('uid'); |
||
| 377 | $lid = Request::getInt('lid', 0, 'POST'); |
||
| 378 | //create a hash so it does not erase another file |
||
| 379 | $hash1 = time(); |
||
| 380 | $hash = substr($hash1, 0, 4); |
||
| 381 | // mimetypes and settings put this in admin part later |
||
| 382 | $allowed_mimetypes = [ |
||
| 383 | 'image/jpeg', |
||
| 384 | 'image/gif' |
||
| 385 | ]; |
||
| 386 | $maxfilesize = $maxfilebytes; |
||
| 387 | // create the object to upload |
||
| 388 | $uploader = new \XoopsMediaUploader($path_upload, $allowed_mimetypes, $maxfilesize, $maxfilewidth, $maxfileheight); |
||
| 389 | // fetch the media |
||
| 390 | if ($uploader->fetchMedia($_POST['xoops_upload_file'][0])) { |
||
| 391 | //lets create a name for it |
||
| 392 | $uploader->setPrefix("pic_{$lid}_"); |
||
| 393 | //now let s upload the file |
||
| 394 | if (!$uploader->upload()) { |
||
| 395 | // if there are errors lets return them |
||
| 396 | echo '<div style="color:#FF0000; background-color:#FFEAF4; border-color:#FF0000; border-width:thick; border-style:solid; text-align:center;"><p>' . $uploader->getErrors() . '</p></div>'; |
||
| 397 | |||
| 398 | return false; |
||
| 399 | } else { |
||
| 400 | // now let s create a new object picture and set its variables |
||
| 401 | $picture = $this->create(); |
||
| 402 | $url = $uploader->getSavedFileName(); |
||
| 403 | $picture->setVar('url', $url); |
||
| 404 | $picture->setVar('title', $title); |
||
| 405 | $uid = $GLOBALS['xoopsUser']->getVar('uid'); |
||
| 406 | $lid = $lid; |
||
| 407 | $picture->setVar('lid', $lid); |
||
| 408 | $picture->setVar('uid_owner', $uid); |
||
| 409 | $this->insert($picture); |
||
| 410 | $saved_destination = $uploader->getSavedDestination(); |
||
| 411 | $this->resizeImage($saved_destination, $thumbwidth, $thumbheight, $pictwidth, $pictheight, $path_upload); |
||
| 412 | } |
||
| 413 | } else { |
||
| 414 | echo '<div style="color:#FF0000; background-color:#FFEAF4; border-color:#FF0000; border-width:thick; border-style:solid; text-align:center;"><p>' . $uploader->getErrors() . '</p></div>'; |
||
| 415 | |||
| 416 | return false; |
||
| 417 | } |
||
| 418 | |||
| 419 | return true; |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Resize a picture and save it to $path_upload |
||
| 424 | * |
||
| 425 | * @param string $img the path to the file |
||
| 426 | * @param string $path_upload The path to where the files should be saved after resizing |
||
| 427 | * @param int $thumbwidth the width in pixels that the thumbnail will have |
||
| 428 | * @param int $thumbheight the height in pixels that the thumbnail will have |
||
| 429 | * @param int $pictwidth the width in pixels that the pic will have |
||
| 430 | * @param int $pictheight the height in pixels that the pic will have |
||
| 431 | * @return nothing |
||
| 432 | */ |
||
| 433 | public function resizeImage($img, $thumbwidth, $thumbheight, $pictwidth, $pictheight, $path_upload) |
||
| 470 | } |
||
| 471 | } |
||
| 472 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.