| Total Complexity | 61 |
| Total Lines | 652 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ImageHandler 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 ImageHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 46 | class ImageHandler extends XoopsPersistableObjectHandler |
||
| 47 | { |
||
| 48 | public Helper $helper; |
||
| 49 | public $isAdmin; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Constructor |
||
| 53 | * @param \XoopsDatabase|null $xoopsDatabase |
||
| 54 | * @param \XoopsModules\Suico\Helper|null $helper |
||
| 55 | */ |
||
| 56 | public function __construct( |
||
| 57 | ?XoopsDatabase $xoopsDatabase = null, |
||
| 58 | $helper = null |
||
| 59 | ) { |
||
| 60 | /** @var \XoopsModules\Suico\Helper $this- >helper */ |
||
| 61 | if (null === $helper) { |
||
| 62 | $this->helper = Helper::getInstance(); |
||
| 63 | } else { |
||
| 64 | $this->helper = $helper; |
||
| 65 | } |
||
| 66 | $this->isAdmin = $this->helper->isUserAdmin(); |
||
| 67 | parent::__construct($xoopsDatabase, 'suico_images', Image::class, 'image_id', 'title', 'caption'); |
||
|
|
|||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * create a new Groups |
||
| 72 | * |
||
| 73 | * @param bool $isNew flag the new objects as "new"? |
||
| 74 | * @return \XoopsObject Groups |
||
| 75 | */ |
||
| 76 | public function create( |
||
| 77 | $isNew = true |
||
| 78 | ) { |
||
| 79 | $obj = parent::create($isNew); |
||
| 80 | // if ($isNew) { |
||
| 81 | // $obj->setDefaultPermissions(); |
||
| 82 | // } |
||
| 83 | if ($isNew) { |
||
| 84 | $obj->setNew(); |
||
| 85 | } else { |
||
| 86 | $obj->unsetNew(); |
||
| 87 | } |
||
| 88 | $obj->helper = $this->helper; |
||
| 89 | |||
| 90 | return $obj; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * retrieve a Image |
||
| 95 | * |
||
| 96 | * @param int|null $id of the Image |
||
| 97 | * @param null $fields |
||
| 98 | * @return false|\XoopsModules\Suico\Image reference to the {@link Image} object, FALSE if failed |
||
| 99 | */ |
||
| 100 | public function get2( |
||
| 101 | $id = null, |
||
| 102 | $fields = null |
||
| 103 | ) { |
||
| 104 | $sql = 'SELECT * FROM ' . $this->db->prefix('suico_images') . ' WHERE image_id=' . $id; |
||
| 105 | if (!$result = $this->db->query($sql)) { |
||
| 106 | return false; |
||
| 107 | } |
||
| 108 | $numrows = $this->db->getRowsNum($result); |
||
| 109 | if (1 === $numrows) { |
||
| 110 | $image = new Image(); |
||
| 111 | $image->assignVars($this->db->fetchArray($result)); |
||
| 112 | |||
| 113 | return $image; |
||
| 114 | } |
||
| 115 | |||
| 116 | return false; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * insert a new Image in the database |
||
| 121 | * |
||
| 122 | * @param \XoopsObject $object reference to the {@link Image} object |
||
| 123 | * @param bool $force |
||
| 124 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
| 125 | */ |
||
| 126 | public function insert2( |
||
| 127 | XoopsObject $object, |
||
| 128 | $force = false |
||
| 129 | ) { |
||
| 130 | global $xoopsConfig; |
||
| 131 | if (!$object instanceof Image) { |
||
| 132 | return false; |
||
| 133 | } |
||
| 134 | if (!$object->isDirty()) { |
||
| 135 | return true; |
||
| 136 | } |
||
| 137 | if (!$object->cleanVars()) { |
||
| 138 | return false; |
||
| 139 | } |
||
| 140 | $image_id = ''; |
||
| 141 | foreach ($object->cleanVars as $k => $v) { |
||
| 142 | ${$k} = $v; |
||
| 143 | } |
||
| 144 | // $now = 'date_add(now(), interval ' . $xoopsConfig['server_TZ'] . ' hour)'; |
||
| 145 | if ($object->isNew()) { |
||
| 146 | // ajout/modification d'un Image |
||
| 147 | $object = new Image(); |
||
| 148 | $format = 'INSERT INTO %s (image_id, title, caption, date_created, date_updated, uid_owner, filename, private)'; |
||
| 149 | $format .= 'VALUES (%u, %s, %s, %s, %s, %s, %s, 0)'; |
||
| 150 | $sql = \sprintf( |
||
| 151 | $format, |
||
| 152 | $this->db->prefix('suico_images'), |
||
| 153 | $image_id, |
||
| 154 | $this->db->quoteString($title), |
||
| 155 | $this->db->quoteString($caption), |
||
| 156 | \time(), //$now, |
||
| 157 | \time(), //$now, |
||
| 158 | $this->db->quoteString($uid_owner), |
||
| 159 | $this->db->quoteString($filename) |
||
| 160 | ); |
||
| 161 | $force = true; |
||
| 162 | } else { |
||
| 163 | $format = 'UPDATE %s SET '; |
||
| 164 | $format .= 'image_id=%u, title=%s, caption=%s, date_created=%s, date_updated=%s, uid_owner=%s, filename=%s, private=%s'; |
||
| 165 | $format .= ' WHERE image_id = %u'; |
||
| 166 | $sql = \sprintf( |
||
| 167 | $format, |
||
| 168 | $this->db->prefix('suico_images'), |
||
| 169 | $image_id, |
||
| 170 | $this->db->quoteString($title), |
||
| 171 | $this->db->quoteString($caption), |
||
| 172 | $object->getVar('date_created'), // $now, |
||
| 173 | $object->getVar('date_updated'), // $now, |
||
| 174 | $this->db->quoteString($uid_owner), |
||
| 175 | $this->db->quoteString($filename), |
||
| 176 | $this->db->quoteString($private), |
||
| 177 | $image_id |
||
| 178 | ); |
||
| 179 | } |
||
| 180 | if ($force) { |
||
| 181 | $result = $this->db->queryF($sql); |
||
| 182 | } else { |
||
| 183 | $result = $this->db->query($sql); |
||
| 184 | } |
||
| 185 | if (!$result) { |
||
| 186 | return false; |
||
| 187 | } |
||
| 188 | if (empty($image_id)) { |
||
| 189 | $image_id = $this->db->getInsertId(); |
||
| 190 | } |
||
| 191 | $object->assignVar('image_id', $image_id); |
||
| 192 | |||
| 193 | return true; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * delete a Image from the database |
||
| 198 | * |
||
| 199 | * @param \XoopsObject $object reference to the Image to delete |
||
| 200 | * @param bool $force |
||
| 201 | * @return bool FALSE if failed. |
||
| 202 | */ |
||
| 203 | public function delete( |
||
| 204 | XoopsObject $object, |
||
| 205 | $force = false |
||
| 206 | ) { |
||
| 207 | if (!$object instanceof Image) { |
||
| 208 | return false; |
||
| 209 | } |
||
| 210 | $sql = \sprintf( |
||
| 211 | 'DELETE FROM %s WHERE image_id = %u', |
||
| 212 | $this->db->prefix('suico_images'), |
||
| 213 | (int)$object->getVar('image_id') |
||
| 214 | ); |
||
| 215 | if ($force) { |
||
| 216 | $result = $this->db->queryF($sql); |
||
| 217 | } else { |
||
| 218 | $result = $this->db->query($sql); |
||
| 219 | } |
||
| 220 | if (!$result) { |
||
| 221 | return false; |
||
| 222 | } |
||
| 223 | |||
| 224 | return true; |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * retrieve suico_imagess from the database |
||
| 229 | * |
||
| 230 | * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement} conditions to be met |
||
| 231 | * @param bool $id_as_key use the UID as key for the array? |
||
| 232 | * @param bool $as_object |
||
| 233 | * @return array array of {@link Image} objects |
||
| 234 | */ |
||
| 235 | public function &getObjects( |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * count suico_imagess matching a condition |
||
| 272 | * |
||
| 273 | * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement} to match |
||
| 274 | * @return int count of suico_imagess |
||
| 275 | */ |
||
| 276 | public function getCount( |
||
| 277 | ?CriteriaElement $criteria = null |
||
| 278 | ) { |
||
| 279 | $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('suico_images'); |
||
| 280 | if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
||
| 281 | $sql .= ' ' . $criteria->renderWhere(); |
||
| 282 | } |
||
| 283 | $result = $this->db->query($sql); |
||
| 284 | if (!$result) { |
||
| 285 | return 0; |
||
| 286 | } |
||
| 287 | [$count] = $this->db->fetchRow($result); |
||
| 288 | |||
| 289 | return (int)$count; |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * delete suico_imagess matching a set of conditions |
||
| 294 | * |
||
| 295 | * @param \CriteriaElement|\CriteriaCompo|null $criteria {@link \CriteriaElement} |
||
| 296 | * @param bool $force |
||
| 297 | * @param bool $asObject |
||
| 298 | * @return bool FALSE if deletion failed |
||
| 299 | */ |
||
| 300 | public function deleteAll( |
||
| 301 | ?CriteriaElement $criteria = null, |
||
| 302 | $force = true, |
||
| 303 | $asObject = false |
||
| 304 | ) { |
||
| 305 | $sql = 'DELETE FROM ' . $this->db->prefix('suico_images'); |
||
| 306 | if (isset($criteria) && is_subclass_of($criteria, 'CriteriaElement')) { |
||
| 307 | $sql .= ' ' . $criteria->renderWhere(); |
||
| 308 | } |
||
| 309 | if (!$result = $this->db->query($sql)) { |
||
| 310 | return false; |
||
| 311 | } |
||
| 312 | |||
| 313 | return true; |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Render a form to send pictures |
||
| 318 | * |
||
| 319 | * @param int $maxbytes the maximum size of a picture |
||
| 320 | * @param \XoopsTpl $xoopsTpl the one in which the form will be rendered |
||
| 321 | * @return bool TRUE |
||
| 322 | * |
||
| 323 | * obs: Some functions wont work on php 4 so edit lines down under acording to your version |
||
| 324 | */ |
||
| 325 | public function renderFormSubmit( |
||
| 326 | $maxbytes, |
||
| 327 | $xoopsTpl |
||
| 328 | ) { |
||
| 329 | $form = new XoopsThemeForm(\_MD_SUICO_SUBMIT_PIC_TITLE, 'form_picture', 'submitImage.php', 'post', true); |
||
| 330 | $field_url = new XoopsFormFile(\_MD_SUICO_SELECT_PHOTO, 'sel_photo', 2000000); |
||
| 331 | $field_title = new XoopsFormText(\_MD_SUICO_PHOTOTITLE, 'title', 35, 55); |
||
| 332 | $field_caption = new XoopsFormText(\_MD_SUICO_CAPTION, 'caption', 35, 55); |
||
| 333 | $form->setExtra('enctype="multipart/form-data"'); |
||
| 334 | $buttonSend = new XoopsFormButton('', 'submit_button', \_MD_SUICO_UPLOADPICTURE, 'submit'); |
||
| 335 | $field_warning = new XoopsFormLabel(\sprintf(\_MD_SUICO_YOU_CAN_UPLOAD, $maxbytes / 1024)); |
||
| 336 | $form->addElement($field_warning); |
||
| 337 | $form->addElement($field_url, true); |
||
| 338 | $form->addElement($field_title); |
||
| 339 | $form->addElement($field_caption); |
||
| 340 | $form->addElement($buttonSend); |
||
| 341 | $form->assign($xoopsTpl); //If your server is php 5 |
||
| 342 | |||
| 343 | return true; |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Render a form to edit the description of the pictures |
||
| 348 | * |
||
| 349 | * @param $title |
||
| 350 | * @param string $caption The description of the picture |
||
| 351 | * @param int $image_id the id of the image in database |
||
| 352 | * @param string $filename the url to the thumb of the image so it can be displayed |
||
| 353 | * @return bool TRUE |
||
| 354 | */ |
||
| 355 | public function renderFormEdit( |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Upload the file and Save into database |
||
| 382 | * |
||
| 383 | * @param string $title A litle title of the file |
||
| 384 | * @param string $caption A litle description of the file |
||
| 385 | * @param string $pathUpload The path to where the file should be uploaded |
||
| 386 | * @param int $thumbwidth the width in pixels that the thumbnail will have |
||
| 387 | * @param int $thumbheight the height in pixels that the thumbnail will have |
||
| 388 | * @param int $pictwidth the width in pixels that the pic will have |
||
| 389 | * @param int $pictheight the height in pixels that the pic will have |
||
| 390 | * @param int $maxfilebytes the maximum size a file can have to be uploaded in bytes |
||
| 391 | * @param int $maxfilewidth the maximum width in pixels that a pic can have |
||
| 392 | * @param int $maxfileheight the maximum height in pixels that a pic can have |
||
| 393 | * @return bool FALSE if upload fails or database fails |
||
| 394 | */ |
||
| 395 | public function receivePicture( |
||
| 396 | $title, |
||
| 397 | $caption, |
||
| 398 | $pathUpload, |
||
| 399 | $thumbwidth, |
||
| 400 | $thumbheight, |
||
| 401 | $pictwidth, |
||
| 402 | $pictheight, |
||
| 403 | $maxfilebytes, |
||
| 404 | $maxfilewidth, |
||
| 405 | $maxfileheight |
||
| 406 | ) { |
||
| 407 | global $xoopsUser, $xoopsDB; |
||
| 408 | //search logged user id |
||
| 409 | $uid = $xoopsUser->getVar('uid'); |
||
| 410 | //create a hash so it does not erase another file |
||
| 411 | //$hash1 = date(); |
||
| 412 | //$hash = substr($hash1,0,4); |
||
| 413 | // mimetypes and settings put this in admin part later |
||
| 414 | $allowed_mimetypes = Helper::getInstance()->getConfig( |
||
| 415 | 'mimetypes' |
||
| 416 | ); |
||
| 417 | $maxfilesize = $maxfilebytes; |
||
| 418 | // $uploadDir = \XOOPS_UPLOAD_PATH . '/suico/images/'; |
||
| 419 | // create the object to upload |
||
| 420 | $uploader = new XoopsMediaUploader( |
||
| 421 | $pathUpload, |
||
| 422 | $allowed_mimetypes, |
||
| 423 | $maxfilesize, |
||
| 424 | $maxfilewidth, |
||
| 425 | $maxfileheight |
||
| 426 | ); |
||
| 427 | // fetch the media |
||
| 428 | if ($uploader->fetchMedia(Request::getArray('xoops_upload_file', '', 'POST')[0])) { |
||
| 429 | //lets create a name for it |
||
| 430 | $uploader->setPrefix('pic_' . $uid . '_'); |
||
| 431 | //now let s upload the file |
||
| 432 | if (!$uploader->upload()) { |
||
| 433 | // if there are errors lets return them |
||
| 434 | 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>'; |
||
| 435 | |||
| 436 | return false; |
||
| 437 | } |
||
| 438 | // now let s create a new object picture and set its variables |
||
| 439 | $picture = $this->create(); |
||
| 440 | $filename = $uploader->getSavedFileName(); |
||
| 441 | $picture->setVar('filename', $filename); |
||
| 442 | $picture->setVar('title', $title); |
||
| 443 | $picture->setVar('caption', $caption); |
||
| 444 | $picture->setVar('date_created', \time()); |
||
| 445 | $picture->setVar('date_updated', \time()); |
||
| 446 | $picture->setVar('private', 0); |
||
| 447 | $uid = $xoopsUser->getVar('uid'); |
||
| 448 | $picture->setVar('uid_owner', $uid); |
||
| 449 | $this->insert($picture); |
||
| 450 | $saved_destination = $uploader->getSavedDestination(); |
||
| 451 | //print_r($_FILES); |
||
| 452 | //$this->resizeImage($saved_destination,false, $thumbwidth, $thumbheight, $pictwidth, $pictheight,$pathUpload); |
||
| 453 | //$this->resizeImage($saved_destination,true, $thumbwidth, $thumbheight, $pictwidth, $pictheight,$pathUpload); |
||
| 454 | $this->resizeImage( |
||
| 455 | $saved_destination, |
||
| 456 | $thumbwidth, |
||
| 457 | $thumbheight, |
||
| 458 | $pictwidth, |
||
| 459 | $pictheight, |
||
| 460 | $pathUpload |
||
| 461 | ); |
||
| 462 | } else { |
||
| 463 | 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>'; |
||
| 464 | |||
| 465 | return false; |
||
| 466 | } |
||
| 467 | |||
| 468 | return true; |
||
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Resize a picture and save it to $pathUpload |
||
| 473 | * |
||
| 474 | * @param string $img the path to the file |
||
| 475 | * @param int $thumbwidth the width in pixels that the thumbnail will have |
||
| 476 | * @param int $thumbheight the height in pixels that the thumbnail will have |
||
| 477 | * @param int $pictwidth the width in pixels that the pic will have |
||
| 478 | * @param int $pictheight the height in pixels that the pic will have |
||
| 479 | * @param string $pathUpload The path to where the files should be saved after resizing |
||
| 480 | */ |
||
| 481 | public function resizeImage( |
||
| 546 | } |
||
| 547 | |||
| 548 | /** |
||
| 549 | * @param $limit |
||
| 550 | * @return array |
||
| 551 | */ |
||
| 552 | public function getLastPictures($limit) |
||
| 572 | } |
||
| 573 | |||
| 574 | /** |
||
| 575 | * @param $limit |
||
| 576 | * @return array |
||
| 577 | */ |
||
| 578 | public function getLastPicturesForBlock($limit) |
||
| 625 | } |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Resize a picture and save it to $pathUpload |
||
| 629 | * |
||
| 630 | * @param string $img the path to the file |
||
| 631 | * @param $width |
||
| 632 | * @param $height |
||
| 633 | * @param string $pathUpload The path to where the files should be saved after resizing |
||
| 634 | */ |
||
| 635 | public function makeAvatar( |
||
| 698 | } |
||
| 699 | } |
||
| 700 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.