| Total Complexity | 42 |
| Total Lines | 306 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| 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 | * @var string |
||
| 20 | */ |
||
| 21 | private static $htaccess_content = <<<EOT |
||
| 22 | <IfModule mod_rewrite.c> |
||
| 23 | RewriteEngine On |
||
| 24 | RewriteBase / |
||
| 25 | |||
| 26 | RewriteCond %{REQUEST_FILENAME} !-f |
||
| 27 | RewriteCond %{REQUEST_FILENAME} !-d |
||
| 28 | RewriteRule ^(.+)\.(v[A-Za-z0-9]+)\.(js|css|png|jpg|gif|svg|webp)$ $1.$3 [L] |
||
| 29 | </IfModule> |
||
| 30 | |||
| 31 | EOT; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * background image for padded images... |
||
| 35 | * |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | private static $perfect_cms_images_background_padding_color = '#cccccc'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * used to set the max width of the media value for mobile images, |
||
| 42 | * eg <source srcset="small.jpg, small2x.jpg 2x" media="(max-width: 600px)"> |
||
| 43 | * |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | private static $mobile_media_max_width = '600px'; |
||
| 47 | |||
| 48 | /*** |
||
| 49 | * details of the images |
||
| 50 | * - width: 3200 |
||
| 51 | * - height: 3200 |
||
| 52 | * - folder: "myfolder" |
||
| 53 | * - filetype: "try jpg" |
||
| 54 | * - enforce_size: false |
||
| 55 | * - folder: my-image-folder-a |
||
| 56 | * - filetype: "jpg or a png with a transparant background" |
||
| 57 | * - use_retina: true |
||
| 58 | * - padding_bg_colour: '#dddddd' |
||
| 59 | * - crop: true |
||
| 60 | * |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | private static $perfect_cms_images_image_definitions = []; |
||
| 64 | |||
| 65 | /*** |
||
| 66 | * Images Titles will be appended to the links only |
||
| 67 | * if the ClassName of the Image is in this array |
||
| 68 | * @var array |
||
| 69 | * |
||
| 70 | */ |
||
| 71 | private static $perfect_cms_images_append_title_to_image_links_classes = [ |
||
| 72 | Image::class, |
||
| 73 | ]; |
||
| 74 | |||
| 75 | private static $retina_multiplier = 2; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * force resample, put htaccess content if it does not exists. |
||
| 79 | */ |
||
| 80 | public static function flush() |
||
| 93 | } |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | public static function get_description_for_cms(string $name): string |
||
| 161 | } |
||
| 162 | |||
| 163 | |||
| 164 | public static function use_retina(string $name): bool |
||
| 165 | { |
||
| 166 | return self::get_one_value_for_image($name, 'use_retina', true); |
||
| 167 | } |
||
| 168 | |||
| 169 | |||
| 170 | public static function get_multiplier(bool $useRetina): int |
||
| 171 | { |
||
| 172 | $multiplier = 1; |
||
| 173 | if ($useRetina) { |
||
| 174 | $multiplier = Config::inst()->get(PerfectCMSImages::class, 'retina_multiplier'); |
||
| 175 | } |
||
| 176 | if (! $multiplier) { |
||
| 177 | $multiplier = 1; |
||
| 178 | } |
||
| 179 | return $multiplier; |
||
| 180 | } |
||
| 181 | |||
| 182 | |||
| 183 | public static function is_crop(string $name): bool |
||
| 184 | { |
||
| 185 | return self::get_one_value_for_image($name, 'crop', false); |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @return int?string |
||
| 190 | */ |
||
| 191 | public static function get_width(string $name, bool $forceInteger = false) |
||
| 192 | { |
||
| 193 | $v = self::get_one_value_for_image($name, 'width', 0); |
||
| 194 | if ($forceInteger) { |
||
| 195 | $v = (int) $v - 0; |
||
| 196 | } |
||
| 197 | |||
| 198 | return $v; |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @return int|string |
||
| 203 | */ |
||
| 204 | public static function get_height(string $name, bool $forceInteger = false) |
||
| 205 | { |
||
| 206 | $v = self::get_one_value_for_image($name, 'height', 0); |
||
| 207 | if ($forceInteger) { |
||
| 208 | $v = (int) $v - 0; |
||
| 209 | } |
||
| 210 | |||
| 211 | return $v; |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @return int?string |
||
| 216 | */ |
||
| 217 | public static function get_mobile_width(string $name, bool $forceInteger = false) |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @return int|string |
||
| 229 | */ |
||
| 230 | public static function get_mobile_height(string $name, bool $forceInteger = false) |
||
| 231 | { |
||
| 232 | $v = self::get_one_value_for_image($name, 'mobile_height', 0); |
||
| 233 | if ($forceInteger) { |
||
| 234 | $v = (int) $v - 0; |
||
| 235 | } |
||
| 236 | |||
| 237 | return $v; |
||
| 238 | } |
||
| 239 | |||
| 240 | |||
| 241 | public static function get_folder(string $name): string |
||
| 242 | { |
||
| 243 | return self::get_one_value_for_image($name, 'folder', 'other-images'); |
||
| 244 | } |
||
| 245 | |||
| 246 | |||
| 247 | public static function max_size_in_kilobytes(string $name): int |
||
| 254 | } |
||
| 255 | |||
| 256 | |||
| 257 | public static function get_file_type(string $name): string |
||
| 258 | { |
||
| 259 | return self::get_one_value_for_image($name, 'filetype', 'jpg'); |
||
| 260 | } |
||
| 261 | |||
| 262 | |||
| 263 | public static function get_enforce_size(string $name): bool |
||
| 264 | { |
||
| 265 | return self::get_one_value_for_image($name, 'enforce_size', false); |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @return string|null |
||
| 270 | */ |
||
| 271 | public static function get_mobile_media_width(string $name) |
||
| 272 | { |
||
| 273 | return self::get_one_value_for_image( |
||
| 274 | $name, |
||
| 275 | 'mobile_media_max_width', |
||
| 276 | Config::inst()->get(PerfectCMSImages::class, 'mobile_media_max_width') |
||
| 277 | ); |
||
| 278 | } |
||
| 279 | |||
| 280 | |||
| 281 | public static function get_padding_bg_colour(string $name): string |
||
| 287 | ); |
||
| 288 | } |
||
| 289 | |||
| 290 | |||
| 291 | protected static function image_info_available(string $name): bool |
||
| 292 | { |
||
| 293 | $sizes = self::get_all_values_for_images(); |
||
| 294 | //print_r($sizes);die(); |
||
| 295 | return isset($sizes[$name]); |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param string $default |
||
| 300 | * |
||
| 301 | * @return mixed |
||
| 302 | */ |
||
| 303 | protected static function get_one_value_for_image(string $name, string $key, ?string $default = '') |
||
| 312 | } |
||
| 313 | |||
| 314 | |||
| 315 | protected static function get_all_values_for_images(): array |
||
| 316 | { |
||
| 317 | return Config::inst()->get( |
||
| 318 | PerfectCmsImageDataExtension::class, |
||
| 321 | } |
||
| 322 | } |
||
| 323 |