| Total Complexity | 42 |
| Total Lines | 291 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like PerfectCMSImages 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 PerfectCMSImages, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class PerfectCMSImages implements Flushable |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | *.htaccess content for assets ... |
||
| 19 | * |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | private static $htaccess_content = <<<'EOT' |
||
| 23 | <IfModule mod_rewrite.c> |
||
| 24 | RewriteEngine On |
||
| 25 | RewriteBase / |
||
| 26 | |||
| 27 | RewriteCond %{REQUEST_FILENAME} !-f |
||
| 28 | RewriteCond %{REQUEST_FILENAME} !-d |
||
| 29 | RewriteRule ^(.+)\.(v[A-Za-z0-9]+)\.(js|css|png|jpg|gif|svg|webp)$ $1.$3 [L] |
||
| 30 | </IfModule> |
||
| 31 | |||
| 32 | EOT; |
||
| 33 | |||
| 34 | private static $unused_images_folder_name = 'unusedimages'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * used to set the max width of the media value for mobile images, |
||
| 38 | * eg <source srcset="small.jpg, small2x.jpg 2x" media="(max-width: 600px)">. |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | private static $mobile_media_max_width = '600px'; |
||
| 43 | |||
| 44 | /* |
||
| 45 | * Images Titles will be appended to the links only |
||
| 46 | * if the ClassName of the Image is in this array |
||
| 47 | * @var array |
||
| 48 | * |
||
| 49 | */ |
||
| 50 | private static $perfect_cms_images_append_title_to_image_links_classes = [ |
||
| 51 | Image::class, |
||
| 52 | ]; |
||
| 53 | |||
| 54 | private static $retina_multiplier = 2; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * force resample, put htaccess content if it does not exists. |
||
| 58 | */ |
||
| 59 | public static function flush() |
||
| 60 | { |
||
| 61 | if (!Config::inst()->get(Image::class, 'force_resample')) { |
||
| 62 | Config::modify()->merge(Image::class, 'force_resample', true); |
||
| 63 | } |
||
| 64 | |||
| 65 | if (class_exists('HashPathExtension')) { |
||
| 66 | if (!file_exists(ASSETS_PATH)) { |
||
| 67 | Filesystem::makeFolder(ASSETS_PATH); |
||
| 68 | } |
||
| 69 | |||
| 70 | $fileName = ASSETS_PATH . '/.htaccess'; |
||
| 71 | if (!file_exists($fileName)) { |
||
| 72 | $string = Config::inst()->get(PerfectCMSImages::class, 'htaccess_content'); |
||
| 73 | file_put_contents($fileName, $string); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | public static function get_description_for_cms(string $name): string |
||
| 79 | { |
||
| 80 | $widthRecommendation = (int) PerfectCMSImages::get_width($name); |
||
| 81 | $heightRecommendation = (int) PerfectCMSImages::get_height($name); |
||
| 82 | $useRetina = PerfectCMSImages::use_retina($name); |
||
| 83 | $recommendedFileType = PerfectCMSImages::get_file_type($name); |
||
| 84 | $multiplier = PerfectCMSImages::get_multiplier($useRetina); |
||
| 85 | if ('' === $recommendedFileType) { |
||
| 86 | $recommendedFileType = 'jpg'; |
||
| 87 | } |
||
| 88 | |||
| 89 | if (0 !== (int) $widthRecommendation) { |
||
| 90 | //cater for retina |
||
| 91 | $widthRecommendation *= $multiplier; |
||
| 92 | $actualWidthDescription = $widthRecommendation . 'px'; |
||
| 93 | } else { |
||
| 94 | $actualWidthDescription = $widthRecommendation; |
||
|
|
|||
| 95 | $actualWidthDescription = 'flexible'; |
||
| 96 | } |
||
| 97 | |||
| 98 | if (0 !== (int) $heightRecommendation) { |
||
| 99 | //cater for retina |
||
| 100 | $heightRecommendation *= $multiplier; |
||
| 101 | $actualHeightDescription = $heightRecommendation . 'px'; |
||
| 102 | } else { |
||
| 103 | $actualHeightDescription = $heightRecommendation; |
||
| 104 | $actualHeightDescription = 'flexible'; |
||
| 105 | } |
||
| 106 | |||
| 107 | $rightTitle = '<span>'; |
||
| 108 | |||
| 109 | if ('flexible' === $actualWidthDescription) { |
||
| 110 | $rightTitle .= 'Image width is flexible'; |
||
| 111 | } else { |
||
| 112 | $rightTitle .= "Image should to be <strong>{$actualWidthDescription}</strong> wide"; |
||
| 113 | } |
||
| 114 | |||
| 115 | $rightTitle .= ' and '; |
||
| 116 | |||
| 117 | if ('flexible' === $actualHeightDescription) { |
||
| 118 | $rightTitle .= 'height is flexible'; |
||
| 119 | } else { |
||
| 120 | $rightTitle .= " <strong>{$actualHeightDescription}</strong> tall"; |
||
| 121 | } |
||
| 122 | |||
| 123 | $rightTitle .= '<br />'; |
||
| 124 | $maxSizeInKilobytes = PerfectCMSImages::max_size_in_kilobytes($name); |
||
| 125 | if (0 !== $maxSizeInKilobytes) { |
||
| 126 | $rightTitle .= 'Maximum file size: ' . round($maxSizeInKilobytes / 1024, 2) . ' megabyte.'; |
||
| 127 | $rightTitle .= '<br />'; |
||
| 128 | } |
||
| 129 | |||
| 130 | if ('' !== $recommendedFileType) { |
||
| 131 | if (strlen( (string) $recommendedFileType) < 5) { |
||
| 132 | $rightTitle .= 'The recommend file type (file extension) is <strong>' . $recommendedFileType . '</strong>.'; |
||
| 133 | } else { |
||
| 134 | $rightTitle .= '<strong>' . $recommendedFileType . '</strong>'; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | return $rightTitle . '</span>'; |
||
| 139 | } |
||
| 140 | |||
| 141 | public static function use_retina(string $name): bool |
||
| 142 | { |
||
| 143 | return self::get_one_value_for_image($name, 'use_retina', true); |
||
| 144 | } |
||
| 145 | |||
| 146 | public static function get_multiplier(bool $useRetina): int |
||
| 147 | { |
||
| 148 | $multiplier = 1; |
||
| 149 | if ($useRetina) { |
||
| 150 | $multiplier = Config::inst()->get(PerfectCMSImages::class, 'retina_multiplier'); |
||
| 151 | } |
||
| 152 | |||
| 153 | if (!$multiplier) { |
||
| 154 | $multiplier = 1; |
||
| 155 | } |
||
| 156 | |||
| 157 | return $multiplier; |
||
| 158 | } |
||
| 159 | |||
| 160 | public static function is_crop(string $name): bool |
||
| 161 | { |
||
| 162 | return self::get_one_value_for_image($name, 'crop', false); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @return int|string |
||
| 167 | */ |
||
| 168 | public static function get_width(string $name, bool $forceInteger = false) |
||
| 169 | { |
||
| 170 | $v = self::get_one_value_for_image($name, 'width', 0); |
||
| 171 | if ($forceInteger) { |
||
| 172 | $v = (int) $v - 0; |
||
| 173 | } |
||
| 174 | |||
| 175 | return $v; |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @return int|string |
||
| 180 | */ |
||
| 181 | public static function get_height(string $name, bool $forceInteger = false) |
||
| 182 | { |
||
| 183 | $v = self::get_one_value_for_image($name, 'height', 0); |
||
| 184 | if ($forceInteger) { |
||
| 185 | $v = (int) $v - 0; |
||
| 186 | } |
||
| 187 | |||
| 188 | return $v; |
||
| 189 | } |
||
| 190 | |||
| 191 | |||
| 192 | public static function has_mobile($name): bool |
||
| 193 | { |
||
| 194 | return (bool) (self::get_mobile_width($name, true) || self::get_mobile_height($name, true)); |
||
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @return int?string |
||
| 199 | */ |
||
| 200 | public static function get_mobile_width(string $name, bool $forceInteger = false) |
||
| 201 | { |
||
| 202 | $v = self::get_one_value_for_image($name, 'mobile_width', 0); |
||
| 203 | if ($forceInteger) { |
||
| 204 | $v = (int) $v - 0; |
||
| 205 | } |
||
| 206 | |||
| 207 | return $v; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @return int|string |
||
| 212 | */ |
||
| 213 | public static function get_mobile_height(string $name, bool $forceInteger = false) |
||
| 214 | { |
||
| 215 | $v = self::get_one_value_for_image($name, 'mobile_height', 0); |
||
| 216 | if ($forceInteger) { |
||
| 217 | $v = (int) $v - 0; |
||
| 218 | } |
||
| 219 | |||
| 220 | return $v; |
||
| 221 | } |
||
| 222 | |||
| 223 | public static function get_folder(string $name): string |
||
| 224 | { |
||
| 225 | return self::get_one_value_for_image($name, 'folder', 'other-images'); |
||
| 226 | } |
||
| 227 | |||
| 228 | public static function move_to_right_folder(string $name): bool |
||
| 229 | { |
||
| 230 | return self::get_one_value_for_image($name, 'move_to_right_folder', true); |
||
| 231 | } |
||
| 232 | |||
| 233 | public static function loading_style(string $name): string |
||
| 234 | { |
||
| 235 | return self::get_one_value_for_image($name, 'loading_style', 'lazy'); |
||
| 236 | } |
||
| 237 | |||
| 238 | public static function max_size_in_kilobytes(string $name): int |
||
| 239 | { |
||
| 240 | $maxSizeInKilobytes = self::get_one_value_for_image($name, 'max_size_in_kilobytes', 0); |
||
| 241 | if (!$maxSizeInKilobytes) { |
||
| 242 | $maxSizeInKilobytes = Config::inst()->get(PerfectCmsImagesUploadField::class, 'max_size_in_kilobytes'); |
||
| 243 | } |
||
| 244 | |||
| 245 | return (int) $maxSizeInKilobytes - 0; |
||
| 246 | } |
||
| 247 | |||
| 248 | public static function get_file_type(string $name): string |
||
| 249 | { |
||
| 250 | return self::get_one_value_for_image($name, 'filetype', 'jpg'); |
||
| 251 | } |
||
| 252 | |||
| 253 | public static function get_enforce_size(string $name): bool |
||
| 254 | { |
||
| 255 | return self::get_one_value_for_image($name, 'enforce_size', false); |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @return null|string |
||
| 260 | */ |
||
| 261 | public static function get_mobile_media_width(string $name) |
||
| 262 | { |
||
| 263 | return self::get_one_value_for_image( |
||
| 264 | $name, |
||
| 265 | 'mobile_media_max_width', |
||
| 266 | Config::inst()->get(PerfectCMSImages::class, 'mobile_media_max_width') |
||
| 267 | ); |
||
| 268 | } |
||
| 269 | |||
| 270 | public static function get_padding_bg_colour(string $name): string |
||
| 276 | ); |
||
| 277 | } |
||
| 278 | |||
| 279 | public static function image_info_available(string $name): bool |
||
| 284 | } |
||
| 285 | |||
| 286 | public static function get_all_values_for_images(): array |
||
| 287 | { |
||
| 288 | return Config::inst()->get( |
||
| 289 | PerfectCmsImageDataExtension::class, |
||
| 290 | 'perfect_cms_images_image_definitions' |
||
| 291 | ) ?: []; |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param string $name |
||
| 296 | * @param string $key |
||
| 297 | * @param mixed $default - optional |
||
| 298 | * |
||
| 299 | * @return mixed |
||
| 300 | */ |
||
| 301 | protected static function get_one_value_for_image(string $name, string $key, $default = '') |
||
| 306 | // Injector::inst()->get(LoggerInterface::class)->info('no information for image with the name: ' . $name . '.' . $key); |
||
| 307 | } |
||
| 308 | } |
||
| 309 |