| Total Complexity | 66 |
| Total Lines | 510 |
| Duplicated Lines | 0 % |
| Changes | 12 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Standard 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 Standard, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Standard |
||
| 23 | extends Base |
||
| 24 | implements \Aimeos\MShop\Media\Manager\Iface, \Aimeos\MShop\Common\Manager\Factory\Iface |
||
| 25 | { |
||
| 26 | use \Aimeos\MShop\Common\Manager\PropertyRef\Traits; |
||
| 27 | use \Aimeos\MShop\Common\Manager\ListsRef\Traits; |
||
| 28 | use \Aimeos\MShop\Upload; |
||
| 29 | use Preview; |
||
| 30 | |||
| 31 | |||
| 32 | /** |
||
| 33 | * Copies the media item and the referenced files |
||
| 34 | * |
||
| 35 | * @param \Aimeos\MShop\Media\Item\Iface $item Media item whose files should be copied |
||
| 36 | * @return \Aimeos\MShop\Media\Item\Iface Copied media item with new files |
||
| 37 | */ |
||
| 38 | public function copy( \Aimeos\MShop\Media\Item\Iface $item ) : \Aimeos\MShop\Media\Item\Iface |
||
| 39 | { |
||
| 40 | $item = ( clone $item )->setId( null ); |
||
| 41 | |||
| 42 | $path = $item->getUrl(); |
||
| 43 | $mime = $item->getMimeType(); |
||
| 44 | $domain = $item->getDomain(); |
||
| 45 | $previews = $item->getPreviews(); |
||
| 46 | $fsname = $item->getFileSystem(); |
||
| 47 | $fs = $this->context()->fs( $fsname ); |
||
| 48 | |||
| 49 | if( $fs->has( $path ) ) |
||
| 50 | { |
||
| 51 | $newPath = $this->path( substr( basename( $path ), 9 ), $mime, $domain ); |
||
| 52 | $fs->copy( $path, $newPath ); |
||
| 53 | $item->setUrl( $newPath ); |
||
| 54 | } |
||
| 55 | |||
| 56 | if( empty( $previews ) ) { |
||
| 57 | return $this->scale( $item, true ); |
||
| 58 | } |
||
| 59 | |||
| 60 | foreach( $previews as $size => $preview ) |
||
| 61 | { |
||
| 62 | if( $fsname !== 'fs-mimeicon' && $fs->has( $preview ) ) |
||
| 63 | { |
||
| 64 | $newPath = $this->path( substr( basename( $preview ), 9 ), $mime, $domain ); |
||
| 65 | $fs->copy( $preview, $newPath ); |
||
| 66 | $previews[$size] = $newPath; |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | return $item->setPreviews( $previews ); |
||
| 71 | } |
||
| 72 | |||
| 73 | |||
| 74 | /** |
||
| 75 | * Creates a new empty item instance |
||
| 76 | * |
||
| 77 | * @param array $values Values the item should be initialized with |
||
| 78 | * @return \Aimeos\MShop\Media\Item\Iface New media item object |
||
| 79 | */ |
||
| 80 | public function create( array $values = [] ) : \Aimeos\MShop\Common\Item\Iface |
||
| 81 | { |
||
| 82 | $locale = $this->context()->locale(); |
||
| 83 | |||
| 84 | $values['.languageid'] = $locale->getLanguageId(); |
||
| 85 | $values['media.siteid'] = $values['media.siteid'] ?? $locale->getSiteId(); |
||
| 86 | |||
| 87 | return new \Aimeos\MShop\Media\Item\Standard( 'media.', $values ); |
||
| 88 | } |
||
| 89 | |||
| 90 | |||
| 91 | /** |
||
| 92 | * Removes multiple items. |
||
| 93 | * |
||
| 94 | * @param \Aimeos\MShop\Common\Item\Iface[]|string[] $items List of item objects or IDs of the items |
||
| 95 | * @return \Aimeos\MShop\Media\Manager\Iface Manager object for chaining method calls |
||
| 96 | */ |
||
| 97 | public function delete( $items ) : \Aimeos\MShop\Common\Manager\Iface |
||
| 98 | { |
||
| 99 | foreach( map( $items ) as $item ) |
||
| 100 | { |
||
| 101 | if( $item instanceof \Aimeos\MShop\Media\Item\Iface && $item->getFileSystem() === 'fs-media' ) |
||
| 102 | { |
||
| 103 | try |
||
| 104 | { |
||
| 105 | $this->deletePreviews( $item, $item->getPreviews() ); |
||
| 106 | $this->deleteFile( $item->getUrl(), 'fs-media' ); |
||
| 107 | } |
||
| 108 | catch( \Exception $e ) |
||
| 109 | { |
||
| 110 | $this->context()->logger()->warning( $e->getMessage() ); |
||
| 111 | } |
||
| 112 | } |
||
| 113 | } |
||
| 114 | |||
| 115 | return parent::delete( $items )->deleteRefItems( $items ); |
||
| 116 | } |
||
| 117 | |||
| 118 | |||
| 119 | /** |
||
| 120 | * Creates a filter object. |
||
| 121 | * |
||
| 122 | * @param bool|null $default Add default criteria or NULL for relaxed default criteria |
||
| 123 | * @param bool $site TRUE for adding site criteria to limit items by the site of related items |
||
| 124 | * @return \Aimeos\Base\Criteria\Iface Returns the filter object |
||
| 125 | */ |
||
| 126 | public function filter( ?bool $default = false, bool $site = false ) : \Aimeos\Base\Criteria\Iface |
||
| 142 | } |
||
| 143 | |||
| 144 | |||
| 145 | /** |
||
| 146 | * Returns the additional column/search definitions |
||
| 147 | * |
||
| 148 | * @return array Associative list of column names as keys and items implementing \Aimeos\Base\Criteria\Attribute\Iface |
||
| 149 | */ |
||
| 150 | public function getSaveAttributes() : array |
||
| 190 | ], |
||
| 191 | ] ); |
||
| 192 | } |
||
| 193 | |||
| 194 | |||
| 195 | /** |
||
| 196 | * Returns the attributes that can be used for searching. |
||
| 197 | * |
||
| 198 | * @param bool $withsub Return also attributes of sub-managers if true |
||
| 199 | * @return \Aimeos\Base\Criteria\Attribute\Iface[] List of search attribute items |
||
| 200 | */ |
||
| 201 | public function getSearchAttributes( bool $withsub = true ) : array |
||
| 257 | } |
||
| 258 | ), |
||
| 259 | ] ) ); |
||
| 260 | } |
||
| 261 | |||
| 262 | |||
| 263 | /** |
||
| 264 | * Saves the dependent items of the item |
||
| 265 | * |
||
| 266 | * @param \Aimeos\MShop\Common\Item\Iface $item Item object |
||
| 267 | * @param bool $fetch True if the new ID should be returned in the item |
||
| 268 | * @return \Aimeos\MShop\Common\Item\Iface Updated item |
||
| 269 | */ |
||
| 270 | public function saveRefs( \Aimeos\MShop\Common\Item\Iface $item, bool $fetch = true ) : \Aimeos\MShop\Common\Item\Iface |
||
| 271 | { |
||
| 272 | $this->savePropertyItems( $item, 'media', $fetch ); |
||
| 273 | $this->saveListItems( $item, 'media', $fetch ); |
||
| 274 | |||
| 275 | return $item; |
||
| 276 | } |
||
| 277 | |||
| 278 | |||
| 279 | /** |
||
| 280 | * Merges the data from the given map and the referenced items |
||
| 281 | * |
||
| 282 | * @param array $entries Associative list of ID as key and the associative list of property key/value pairs as values |
||
| 283 | * @param array $ref List of referenced items to fetch and add to the entries |
||
| 284 | * @return array Associative list of ID as key and the updated entries as value |
||
| 285 | */ |
||
| 286 | public function searchRefs( array $entries, array $ref ) : array |
||
| 287 | { |
||
| 288 | $parentIds = array_keys( $entries ); |
||
| 289 | |||
| 290 | if( $this->hasRef( $ref, 'media/property' ) ) |
||
| 291 | { |
||
| 292 | $name = 'media/property'; |
||
| 293 | $propTypes = isset( $ref[$name] ) && is_array( $ref[$name] ) ? $ref[$name] : null; |
||
| 294 | |||
| 295 | foreach( $this->getPropertyItems( $parentIds, 'media', $propTypes ) as $id => $list ) { |
||
| 296 | $entries[$id]['.propitems'] = $list; |
||
| 297 | } |
||
| 298 | } |
||
| 299 | |||
| 300 | foreach( $this->getListItems( $parentIds, $ref, 'media' ) as $id => $listItem ) { |
||
| 301 | $entries[$listItem->getParentId()]['.listitems'][$id] = $listItem; |
||
| 302 | } |
||
| 303 | |||
| 304 | return $entries; |
||
| 305 | } |
||
| 306 | |||
| 307 | |||
| 308 | /** |
||
| 309 | * Returns the prefix for the item properties and search keys. |
||
| 310 | * |
||
| 311 | * @return string Prefix for the item properties and search keys |
||
| 312 | */ |
||
| 313 | protected function prefix() : string |
||
| 314 | { |
||
| 315 | return 'media.'; |
||
| 316 | } |
||
| 317 | |||
| 318 | |||
| 319 | /** |
||
| 320 | * Rescales the original file to preview files referenced by the media item |
||
| 321 | * |
||
| 322 | * The height/width configuration for scaling |
||
| 323 | * - mshop/media/<files|preview>/maxheight |
||
| 324 | * - mshop/media/<files|preview>/maxwidth |
||
| 325 | * - mshop/media/<files|preview>/force-size |
||
| 326 | * |
||
| 327 | * @param \Aimeos\MShop\Media\Item\Iface $item Media item whose files should be scaled |
||
| 328 | * @param bool $force True to enforce creating new preview images |
||
| 329 | * @return \Aimeos\MShop\Media\Item\Iface Rescaled media item |
||
| 330 | */ |
||
| 331 | public function scale( \Aimeos\MShop\Media\Item\Iface $item, bool $force = false ) : \Aimeos\MShop\Media\Item\Iface |
||
| 332 | { |
||
| 333 | $mime = $item->getMimeType(); |
||
| 334 | |||
| 335 | if( empty( $url = $item->getUrl() ) |
||
| 336 | || $item->getFileSystem() === 'fs-mimeicon' |
||
| 337 | || strncmp( 'data:', $url, 5 ) === 0 |
||
| 338 | || strncmp( 'image/svg', $mime, 9 ) === 0 |
||
| 339 | || strncmp( 'image/', $mime, 6 ) !== 0 |
||
| 340 | ) { |
||
| 341 | return $item; |
||
| 342 | } |
||
| 343 | |||
| 344 | $fs = $this->context()->fs( $item->getFileSystem() ); |
||
| 345 | $is = ( $fs instanceof \Aimeos\Base\Filesystem\MetaIface ? true : false ); |
||
| 346 | |||
| 347 | if( !$force |
||
| 348 | && !empty( $item->getPreviews() ) |
||
| 349 | && preg_match( '#^[a-zA-Z]{2,6}://#', $url ) !== 1 |
||
| 350 | && ( $is && date( 'Y-m-d H:i:s', $fs->time( $url ) ) < $item->getTimeModified() || $fs->has( $url ) ) |
||
| 351 | ) { |
||
| 352 | return $item; |
||
| 353 | } |
||
| 354 | |||
| 355 | $domain = $item->getDomain() ?: '-'; |
||
| 356 | $sizes = $this->sizes( $domain, $item->getType() ); |
||
| 357 | $image = $this->image( $url ); |
||
| 358 | $quality = $this->quality(); |
||
| 359 | $old = $item->getPreviews(); |
||
| 360 | $previews = []; |
||
| 361 | |||
| 362 | foreach( $this->createPreviews( $image, $sizes ) as $width => $image ) |
||
| 363 | { |
||
| 364 | $path = $old[$width] ?? $this->path( $url, 'image/webp', $domain ); |
||
| 365 | $fs->write( $path, (string) $image->toWebp( $quality ) ); |
||
| 366 | |||
| 367 | $previews[$width] = $path; |
||
| 368 | unset( $old[$width] ); |
||
| 369 | } |
||
| 370 | |||
| 371 | $item = $this->deletePreviews( $item, $old )->setPreviews( $previews ); |
||
| 372 | |||
| 373 | $this->call( 'scaled', $item, $image ); |
||
| 374 | |||
| 375 | return $item; |
||
| 376 | } |
||
| 377 | |||
| 378 | |||
| 379 | /** |
||
| 380 | * Returns the preview image sizes for scaling the images. |
||
| 381 | * |
||
| 382 | * @param string $domain Domain of the image |
||
| 383 | * @param string $type Type of the image |
||
| 384 | * @return array List of image sizes with "maxwidth", "maxheight" and "force-size" properties |
||
| 385 | */ |
||
| 386 | protected function sizes( string $domain, string $type ) : array |
||
| 387 | { |
||
| 388 | $config = $this->context()->config(); |
||
| 389 | |||
| 390 | /** mshop/media/manager/previews/common |
||
| 391 | * Scaling options for preview images |
||
| 392 | * |
||
| 393 | * For responsive images, several preview images of different sizes are |
||
| 394 | * generated. This setting controls how many preview images are generated, |
||
| 395 | * what's their maximum width and height and if the given width/height is |
||
| 396 | * enforced by cropping images that doesn't fit. |
||
| 397 | * |
||
| 398 | * The setting must consist of a list image size definitions like: |
||
| 399 | * |
||
| 400 | * [ |
||
| 401 | * ['maxwidth' => 240, 'maxheight' => 320, 'force-size' => 2], |
||
| 402 | * ['maxwidth' => 720, 'maxheight' => 960, 'force-size' => 1], |
||
| 403 | * ['maxwidth' => 2160, 'maxheight' => 2880, 'force-size' => 0], |
||
| 404 | * ] |
||
| 405 | * |
||
| 406 | * "maxwidth" sets the maximum allowed width of the image whereas |
||
| 407 | * "maxheight" does the same for the maximum allowed height. If both |
||
| 408 | * values are given, the image is scaled proportionally so it fits into |
||
| 409 | * the box defined by both values. |
||
| 410 | * |
||
| 411 | * In case the image has different proportions than the specified ones |
||
| 412 | * and "force-size" is "0", the image is resized to fit entirely into |
||
| 413 | * the specified box. One side of the image will be shorter than it |
||
| 414 | * would be possible by the specified box. |
||
| 415 | * |
||
| 416 | * If "force-size" is "1", scaled images that doesn't fit into the |
||
| 417 | * given maximum width/height are centered and then filled with the |
||
| 418 | * background color. |
||
| 419 | * |
||
| 420 | * The value of "2" will center the image while the given maxwidth and |
||
| 421 | * maxheight are fully covered and crop the parts of the image which |
||
| 422 | * are outside the box created by maxwidth and maxheight. |
||
| 423 | * |
||
| 424 | * By default, images aren't padded or cropped, only scaled. |
||
| 425 | * |
||
| 426 | * The values for "maxwidth" and "maxheight" can also be null or not |
||
| 427 | * used. In that case, the width or height or both is unbound. If none |
||
| 428 | * of the values are given, the image won't be scaled at all. If only |
||
| 429 | * one value is set, the image will be scaled exactly to the given width |
||
| 430 | * or height and the other side is scaled proportionally. |
||
| 431 | * |
||
| 432 | * You can also define different preview sizes for different domains (e.g. |
||
| 433 | * for catalog images) and for different types (e.g. catalog stage images). |
||
| 434 | * Use configuration settings like |
||
| 435 | * |
||
| 436 | * mshop/media/manager/previews/previews/<domain>/ |
||
| 437 | * mshop/media/manager/previews/previews/<domain>/<type>/ |
||
| 438 | * |
||
| 439 | * for example: |
||
| 440 | * |
||
| 441 | * mshop/media/manager/previews/catalog/previews => [ |
||
| 442 | * ['maxwidth' => 240, 'maxheight' => 320, 'force-size' => true], |
||
| 443 | * ] |
||
| 444 | * mshop/media/manager/previews/catalog/previews => [ |
||
| 445 | * ['maxwidth' => 400, 'maxheight' => 300, 'force-size' => false] |
||
| 446 | * ] |
||
| 447 | * mshop/media/manager/previews/catalog/stage/previews => [ |
||
| 448 | * ['maxwidth' => 360, 'maxheight' => 320, 'force-size' => true], |
||
| 449 | * ['maxwidth' => 720, 'maxheight' => 480, 'force-size' => true] |
||
| 450 | * ] |
||
| 451 | * |
||
| 452 | * These settings will create two preview images for catalog stage images, |
||
| 453 | * one with a different size for all other catalog images and all images |
||
| 454 | * from other domains will be sized to 240x320px. The available domains |
||
| 455 | * which can have images are: |
||
| 456 | * |
||
| 457 | * * attribute |
||
| 458 | * * catalog |
||
| 459 | * * product |
||
| 460 | * * service |
||
| 461 | * * supplier |
||
| 462 | * |
||
| 463 | * There are a few image types included per domain ("default" is always |
||
| 464 | * available). You can also add your own types in the admin backend and |
||
| 465 | * extend the frontend to display them where you need them. |
||
| 466 | * |
||
| 467 | * @param array List of image size definitions |
||
| 468 | * @since 2019.07 |
||
| 469 | */ |
||
| 470 | $sizes = $config->get( 'mshop/media/manager/previews/common', [] ); |
||
| 471 | $sizes = $config->get( 'mshop/media/manager/previews/' . $domain, $sizes ); |
||
| 472 | $sizes = $config->get( 'mshop/media/manager/previews/' . $domain . '/' . $type, $sizes ); |
||
| 473 | |||
| 474 | return $sizes; |
||
| 475 | } |
||
| 476 | |||
| 477 | |||
| 478 | /** |
||
| 479 | * Stores the uploaded file and returns the updated item |
||
| 480 | * |
||
| 481 | * @param \Aimeos\MShop\Media\Item\Iface $item Media item for storing the file meta data, "domain" must be set |
||
| 482 | * @param \Psr\Http\Message\UploadedFileInterface|null $file Uploaded file object |
||
| 483 | * @param \Psr\Http\Message\UploadedFileInterface|null $preview Uploaded preview image |
||
| 484 | * @return \Aimeos\MShop\Media\Item\Iface Updated media item including file and preview paths |
||
| 485 | */ |
||
| 486 | public function upload( \Aimeos\MShop\Media\Item\Iface $item, ?UploadedFileInterface $file, UploadedFileInterface $preview = null ) : \Aimeos\MShop\Media\Item\Iface |
||
| 532 | } |
||
| 533 | |||
| 534 | |||
| 535 | /** mshop/media/manager/resource |
||
| 536 | * Name of the database connection resource to use |
||
| 537 | * |
||
| 938 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths