| Total Complexity | 41 |
| Total Lines | 310 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| 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 | /** |
||
| 35 | * background image for padded images... |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | private static $perfect_cms_images_background_padding_color = '#cccccc'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * used to set the max width of the media value for mobile images, |
||
| 43 | * eg <source srcset="small.jpg, small2x.jpg 2x" media="(max-width: 600px)">. |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private static $mobile_media_max_width = '600px'; |
||
| 48 | |||
| 49 | /* |
||
| 50 | * details of the images |
||
| 51 | * - width: 3200 |
||
| 52 | * - height: 3200 |
||
| 53 | * - folder: "myfolder" |
||
| 54 | * - filetype: "try jpg" |
||
| 55 | * - enforce_size: false |
||
| 56 | * - folder: my-image-folder-a |
||
| 57 | * - filetype: "jpg or a png with a transparant background" |
||
| 58 | * - use_retina: true |
||
| 59 | * - padding_bg_colour: '#dddddd' |
||
| 60 | * - crop: true |
||
| 61 | * |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | private static $perfect_cms_images_image_definitions = []; |
||
| 65 | |||
| 66 | /* |
||
| 67 | * Images Titles will be appended to the links only |
||
| 68 | * if the ClassName of the Image is in this array |
||
| 69 | * @var array |
||
| 70 | * |
||
| 71 | */ |
||
| 72 | private static $perfect_cms_images_append_title_to_image_links_classes = [ |
||
| 73 | Image::class, |
||
| 74 | ]; |
||
| 75 | |||
| 76 | private static $retina_multiplier = 2; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * force resample, put htaccess content if it does not exists. |
||
| 80 | */ |
||
| 81 | public static function flush() |
||
| 82 | { |
||
| 83 | if (! Config::inst()->get(Image::class, 'force_resample')) { |
||
| 84 | Config::modify()->update(Image::class, 'force_resample', true); |
||
| 85 | } |
||
| 86 | |||
| 87 | if (class_exists('HashPathExtension')) { |
||
| 88 | if (! file_exists(ASSETS_PATH)) { |
||
| 89 | Filesystem::makeFolder(ASSETS_PATH); |
||
| 90 | } |
||
| 91 | |||
| 92 | $fileName = ASSETS_PATH . '/.htaccess'; |
||
| 93 | if (! file_exists($fileName)) { |
||
| 94 | $string = Config::inst()->get(PerfectCMSImages::class, 'htaccess_content'); |
||
| 95 | file_put_contents($fileName, $string); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | public static function get_description_for_cms(string $name): string |
||
| 101 | { |
||
| 102 | $widthRecommendation = PerfectCMSImages::get_width($name); |
||
| 103 | $heightRecommendation = PerfectCMSImages::get_height($name); |
||
| 104 | $useRetina = PerfectCMSImages::use_retina($name); |
||
| 105 | $recommendedFileType = PerfectCMSImages::get_file_type($name); |
||
| 106 | $multiplier = PerfectCMSImages::get_multiplier($useRetina); |
||
| 107 | if ('' === $recommendedFileType) { |
||
| 108 | $recommendedFileType = 'jpg'; |
||
| 109 | } |
||
| 110 | |||
| 111 | if (0 !== (int) $widthRecommendation) { |
||
| 112 | //cater for retina |
||
| 113 | $widthRecommendation *= $multiplier; |
||
| 114 | $actualWidthDescription = $widthRecommendation . 'px'; |
||
| 115 | } else { |
||
| 116 | $actualWidthDescription = $widthRecommendation; |
||
|
|
|||
| 117 | $actualWidthDescription = 'flexible'; |
||
| 118 | } |
||
| 119 | |||
| 120 | if (0 !== (int) $heightRecommendation) { |
||
| 121 | //cater for retina |
||
| 122 | $heightRecommendation *= $multiplier; |
||
| 123 | $actualHeightDescription = $heightRecommendation . 'px'; |
||
| 124 | } else { |
||
| 125 | $actualHeightDescription = $heightRecommendation; |
||
| 126 | $actualHeightDescription = 'flexible'; |
||
| 127 | } |
||
| 128 | |||
| 129 | $rightTitle = '<span>'; |
||
| 130 | |||
| 131 | if ('flexible' === $actualWidthDescription) { |
||
| 132 | $rightTitle .= 'Image width is flexible'; |
||
| 133 | } else { |
||
| 134 | $rightTitle .= "Image should to be <strong>{$actualWidthDescription}</strong> wide"; |
||
| 135 | } |
||
| 136 | |||
| 137 | $rightTitle .= ' and '; |
||
| 138 | |||
| 139 | if ('flexible' === $actualHeightDescription) { |
||
| 140 | $rightTitle .= 'height is flexible'; |
||
| 141 | } else { |
||
| 142 | $rightTitle .= " <strong>{$actualHeightDescription}</strong> tall"; |
||
| 143 | } |
||
| 144 | |||
| 145 | $rightTitle .= '<br />'; |
||
| 146 | $maxSizeInKilobytes = PerfectCMSImages::max_size_in_kilobytes($name); |
||
| 147 | if (0 !== $maxSizeInKilobytes) { |
||
| 148 | $rightTitle .= 'Maximum file size: ' . round($maxSizeInKilobytes / 1024, 2) . ' megabyte.'; |
||
| 149 | $rightTitle .= '<br />'; |
||
| 150 | } |
||
| 151 | |||
| 152 | if ('' !== $recommendedFileType) { |
||
| 153 | if (strlen($recommendedFileType) < 5) { |
||
| 154 | $rightTitle .= 'The recommend file type (file extension) is <strong>' . $recommendedFileType . '</strong>.'; |
||
| 155 | } else { |
||
| 156 | $rightTitle .= '<strong>' . $recommendedFileType . '</strong>'; |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | return $rightTitle . '</span>'; |
||
| 161 | } |
||
| 162 | |||
| 163 | public static function use_retina(string $name): bool |
||
| 166 | } |
||
| 167 | |||
| 168 | public static function get_multiplier(bool $useRetina): int |
||
| 169 | { |
||
| 170 | $multiplier = 1; |
||
| 171 | if ($useRetina) { |
||
| 172 | $multiplier = Config::inst()->get(PerfectCMSImages::class, 'retina_multiplier'); |
||
| 173 | } |
||
| 174 | |||
| 175 | if (! $multiplier) { |
||
| 176 | $multiplier = 1; |
||
| 177 | } |
||
| 178 | |||
| 179 | return $multiplier; |
||
| 180 | } |
||
| 181 | |||
| 182 | public static function is_crop(string $name): bool |
||
| 183 | { |
||
| 184 | return self::get_one_value_for_image($name, 'crop', false); |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * @return int?string |
||
| 189 | */ |
||
| 190 | public static function get_width(string $name, bool $forceInteger = false) |
||
| 191 | { |
||
| 192 | $v = self::get_one_value_for_image($name, 'width', 0); |
||
| 193 | if ($forceInteger) { |
||
| 194 | $v = (int) $v - 0; |
||
| 195 | } |
||
| 196 | |||
| 197 | return $v; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @return int|string |
||
| 202 | */ |
||
| 203 | public static function get_height(string $name, bool $forceInteger = false) |
||
| 204 | { |
||
| 205 | $v = self::get_one_value_for_image($name, 'height', 0); |
||
| 206 | if ($forceInteger) { |
||
| 207 | $v = (int) $v - 0; |
||
| 208 | } |
||
| 209 | |||
| 210 | return $v; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @return int?string |
||
| 215 | */ |
||
| 216 | public static function get_mobile_width(string $name, bool $forceInteger = false) |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @return int|string |
||
| 228 | */ |
||
| 229 | public static function get_mobile_height(string $name, bool $forceInteger = false) |
||
| 237 | } |
||
| 238 | |||
| 239 | public static function get_folder(string $name): string |
||
| 242 | } |
||
| 243 | |||
| 244 | public static function move_to_right_folder(string $name): bool |
||
| 245 | { |
||
| 246 | return self::get_one_value_for_image($name, 'move_to_right_folder', true); |
||
| 247 | } |
||
| 248 | |||
| 249 | public static function loading_style(string $name) : string |
||
| 250 | { |
||
| 251 | return self::get_one_value_for_image($name, 'loading_style', 'lazy'); |
||
| 252 | } |
||
| 253 | |||
| 254 | public static function max_size_in_kilobytes(string $name): int |
||
| 262 | } |
||
| 263 | |||
| 264 | public static function get_file_type(string $name): string |
||
| 265 | { |
||
| 266 | return self::get_one_value_for_image($name, 'filetype', 'jpg'); |
||
| 267 | } |
||
| 268 | |||
| 269 | public static function get_enforce_size(string $name): bool |
||
| 270 | { |
||
| 271 | return self::get_one_value_for_image($name, 'enforce_size', false); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @return null|string |
||
| 276 | */ |
||
| 277 | public static function get_mobile_media_width(string $name) |
||
| 278 | { |
||
| 279 | return self::get_one_value_for_image( |
||
| 280 | $name, |
||
| 281 | 'mobile_media_max_width', |
||
| 282 | Config::inst()->get(PerfectCMSImages::class, 'mobile_media_max_width') |
||
| 283 | ); |
||
| 284 | } |
||
| 285 | |||
| 286 | public static function get_padding_bg_colour(string $name): string |
||
| 292 | ); |
||
| 293 | } |
||
| 294 | |||
| 295 | public static function image_info_available(string $name): bool |
||
| 296 | { |
||
| 297 | $sizes = self::get_all_values_for_images(); |
||
| 298 | //print_r($sizes);die(); |
||
| 299 | return isset($sizes[$name]); |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param string $default |
||
| 304 | * |
||
| 305 | * @return mixed |
||
| 306 | */ |
||
| 307 | protected static function get_one_value_for_image(string $name, string $key, ?string $default = '') |
||
| 317 | } |
||
| 318 | |||
| 319 | protected static function get_all_values_for_images(): array |
||
| 320 | { |
||
| 321 | return Config::inst()->get( |
||
| 325 | } |
||
| 326 | } |
||
| 327 |