| Total Complexity | 41 |
| Total Lines | 300 |
| Duplicated Lines | 0 % |
| Changes | 14 | ||
| Bugs | 0 | Features | 0 |
Complex classes like PerfectCmsImageDataExtension 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 PerfectCmsImageDataExtension, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class PerfectCmsImageDataExtension extends DataExtension |
||
| 21 | { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * background image for padded images... |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | private static $perfect_cms_images_background_padding_color = '#cccccc'; |
||
| 29 | |||
| 30 | /* |
||
| 31 | * details of the images |
||
| 32 | * - width: 3200 |
||
| 33 | * - height: 3200 |
||
| 34 | * - folder: "myfolder" |
||
| 35 | * - filetype: "try jpg" |
||
| 36 | * - enforce_size: false |
||
| 37 | * - folder: my-image-folder-a |
||
| 38 | * - filetype: "jpg or a png with a transparant background" |
||
| 39 | * - use_retina: true |
||
| 40 | * - padding_bg_colour: '#dddddd' |
||
| 41 | * - crop: true |
||
| 42 | * - move_to_right_folder: true |
||
| 43 | * - loading_style: 'eager' |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | private static $perfect_cms_images_image_definitions = []; |
||
| 47 | |||
| 48 | private static $casting = [ |
||
| 49 | 'PerfectCMSImageTag' => 'HTMLText', |
||
| 50 | ]; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param string $name PerfectCMSImages name |
||
| 54 | * @param bool $inline for use within existing image tag - optional |
||
| 55 | * @param string $alt alt tag for image -optional |
||
| 56 | * @param string $attributes additional attributes |
||
| 57 | * |
||
| 58 | * @return string (HTML) |
||
| 59 | */ |
||
| 60 | public function getPerfectCMSImageTag(string $name, $inline = false, ?string $alt = '', ?string $attributes = '') |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param string $name PerfectCMSImages name |
||
| 67 | * @param bool $inline for use within existing image tag - optional. can be TRUE, "TRUE" or 1 also... |
||
| 68 | * @param string $alt alt tag for image -optional |
||
| 69 | * @param string $attributes additional attributes |
||
| 70 | * |
||
| 71 | * @return string (HTML) |
||
| 72 | */ |
||
| 73 | public function PerfectCMSImageTag(string $name, $inline = false, ?string $alt = '', ?string $attributes = '') |
||
| 74 | { |
||
| 75 | $arrayData = $this->getPerfectCMSImageTagArrayData($name, $inline, $alt, $attributes); |
||
| 76 | $template = 'Includes/PerfectCMSImageTag'; |
||
| 77 | if (true === $inline || 1 === (int) $inline || 'true' === strtolower($inline)) { |
||
|
|
|||
| 78 | $template .= 'Inline'; |
||
| 79 | } |
||
| 80 | |||
| 81 | return DBField::create_field('HTMLText', $arrayData->renderWith($template)); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param string $name PerfectCMSImages name |
||
| 86 | * @param bool $inline for use within existing image tag - optional. can be TRUE, "TRUE" or 1 also... |
||
| 87 | * @param string $alt alt tag for image -optional |
||
| 88 | * @param string $attributes additional attributes |
||
| 89 | * |
||
| 90 | * @return ArrayData |
||
| 91 | */ |
||
| 92 | public function PerfectCMSImageTagArrayData(string $name, $inline = false, ?string $alt = '', ?string $attributes = '') |
||
| 93 | { |
||
| 94 | return $this->getPerfectCMSImageTagArrayData($name, $inline, $alt, $attributes); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @param string $name PerfectCMSImages name |
||
| 99 | * @param bool $inline for use within existing image tag - optional. can be TRUE, "TRUE" or 1 also... |
||
| 100 | * @param string $alt alt tag for image -optional |
||
| 101 | * @param string $attributes additional attributes |
||
| 102 | * |
||
| 103 | * @return ArrayData |
||
| 104 | */ |
||
| 105 | public function getPerfectCMSImageTagArrayData(string $name, $inline = false, ?string $alt = '', ?string $attributes = '') |
||
| 106 | { |
||
| 107 | $retinaLink = $this->PerfectCMSImageLinkRetina($name); |
||
| 108 | $nonRetinaLink = $this->PerfectCMSImageLinkNonRetina($name); |
||
| 109 | |||
| 110 | $retinaLinkWebP = $this->PerfectCMSImageLinkRetinaWebP($name); |
||
| 111 | $nonRetinaLinkWebP = $this->PerfectCMSImageLinkNonRetinaWebP($name); |
||
| 112 | |||
| 113 | $mobileRetinaLink = $this->PerfectCMSImageLinkRetinaForMobile($name); |
||
| 114 | $mobileNonRetinaLink = $this->PerfectCMSImageLinkNonRetinaForMobile($name); |
||
| 115 | |||
| 116 | $mobileRetinaLinkWebP = $this->PerfectCMSImageLinkRetinaWebPForMobile($name); |
||
| 117 | $mobileNonRetinaLinkWebP = $this->PerfectCMSImageLinkNonRetinaWebPForMobile($name); |
||
| 118 | |||
| 119 | $width = PerfectCMSImages::get_width($name, true); |
||
| 120 | $height = PerfectCMSImages::get_height($name, true); |
||
| 121 | $loadingStyle = PerfectCMSImages::loading_style($name); |
||
| 122 | $mobileMediaWidth = PerfectCMSImages::get_mobile_media_width($name); |
||
| 123 | |||
| 124 | if (! $alt) { |
||
| 125 | $alt = $this->getOwner()->Title; |
||
| 126 | } |
||
| 127 | |||
| 128 | return ArrayData::create( |
||
| 129 | [ |
||
| 130 | 'MobileMediaWidth' => $mobileMediaWidth, |
||
| 131 | 'Width' => $width, |
||
| 132 | 'Height' => $height, |
||
| 133 | 'Alt' => Convert::raw2att($alt), |
||
| 134 | 'MobileRetinaLink' => $mobileRetinaLink, |
||
| 135 | 'MobileNonRetinaLink' => $mobileNonRetinaLink, |
||
| 136 | 'MobileRetinaLinkWebP' => $mobileRetinaLinkWebP, |
||
| 137 | 'MobileNonRetinaLinkWebP' => $mobileNonRetinaLinkWebP, |
||
| 138 | 'RetinaLink' => $retinaLink, |
||
| 139 | 'NonRetinaLink' => $nonRetinaLink, |
||
| 140 | 'RetinaLinkWebP' => $retinaLinkWebP, |
||
| 141 | 'NonRetinaLinkWebP' => $nonRetinaLinkWebP, |
||
| 142 | 'LoadingStyle' => $loadingStyle, |
||
| 143 | 'Attributes' => DBField::create_field('HTMLText', $attributes), |
||
| 144 | ] |
||
| 145 | ); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param string $name of Image Field template |
||
| 150 | * |
||
| 151 | * @return string (link) |
||
| 152 | */ |
||
| 153 | public function PerfectCMSImageLinkNonRetina(string $name): string |
||
| 154 | { |
||
| 155 | return $this->PerfectCMSImageLink($name); |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param string $name of Image Field template |
||
| 160 | * |
||
| 161 | * @return string (link) |
||
| 162 | */ |
||
| 163 | public function PerfectCMSImageLinkRetina(string $name): string |
||
| 164 | { |
||
| 165 | return $this->PerfectCMSImageLink($name, true); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param string $name of Image Field template |
||
| 170 | * |
||
| 171 | * @return string (link) |
||
| 172 | */ |
||
| 173 | public function PerfectCMSImageLinkNonRetinaWebP(string $name): string |
||
| 174 | { |
||
| 175 | return $this->PerfectCMSImageLink($name, false, true); |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param string $name of Image Field template |
||
| 180 | * |
||
| 181 | * @return string (link) |
||
| 182 | */ |
||
| 183 | public function PerfectCMSImageLinkRetinaWebP(string $name): string |
||
| 184 | { |
||
| 185 | return $this->PerfectCMSImageLink($name, true, true); |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param string $name of Image Field template |
||
| 190 | * |
||
| 191 | * @return string (link) |
||
| 192 | */ |
||
| 193 | public function PerfectCMSImageLinkNonRetinaForMobile(string $name): string |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param string $name of Image Field template |
||
| 200 | * |
||
| 201 | * @return string (link) |
||
| 202 | */ |
||
| 203 | public function PerfectCMSImageLinkRetinaForMobile(string $name): string |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param string $name of Image Field template |
||
| 210 | * |
||
| 211 | * @return string (link) |
||
| 212 | */ |
||
| 213 | public function PerfectCMSImageLinkNonRetinaWebPForMobile(string $name): string |
||
| 214 | { |
||
| 215 | return $this->PerfectCMSImageLink($name, false, true, true); |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param string $name of Image Field template |
||
| 220 | * |
||
| 221 | * @return string (link) |
||
| 222 | */ |
||
| 223 | public function PerfectCMSImageLinkRetinaWebPForMobile(string $name): string |
||
| 224 | { |
||
| 225 | return $this->PerfectCMSImageLink($name, true, true, true); |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param string $link |
||
| 230 | * |
||
| 231 | * @return string (link) |
||
| 232 | */ |
||
| 233 | public function getPerfectCMSImageAbsoluteLink(string $link): string |
||
| 234 | { |
||
| 235 | return Director::absoluteURL($link); |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * returns image link (if any). |
||
| 240 | */ |
||
| 241 | public function PerfectCMSImageLink(string $name, ?bool $useRetina = false, ?bool $isWebP = false, ?bool $forMobile = false): string |
||
| 242 | { |
||
| 243 | /** @var null|Image $image */ |
||
| 244 | $image = $this->owner; |
||
| 245 | if ($image && $image->exists() && $image instanceof Image) { |
||
| 246 | //we are all good ... |
||
| 247 | } else { |
||
| 248 | $image = ImageManipulations::get_backup_image($name); |
||
| 249 | } |
||
| 250 | |||
| 251 | if ($image && $image->exists() && $image instanceof Image) { |
||
| 252 | // $backEndString = Image::get_backend(); |
||
| 253 | // $backend = Injector::inst()->get($backEndString); |
||
| 254 | $link = ImageManipulations::get_image_link($image, $name, $useRetina, $forMobile); |
||
| 255 | |||
| 256 | if ($isWebP) { |
||
| 257 | $link = ImageManipulations::web_p_link($link); |
||
| 258 | } |
||
| 259 | |||
| 260 | return '' !== $link ? ImageManipulations::add_fake_parts($image, $link) : ''; |
||
| 261 | } |
||
| 262 | |||
| 263 | // no image -> provide placeholder if in DEV MODE only!!! |
||
| 264 | if (Director::isDev()) { |
||
| 265 | return ImageManipulations::get_placeholder_image_tag($name); |
||
| 266 | } |
||
| 267 | |||
| 268 | return ''; |
||
| 269 | } |
||
| 270 | |||
| 271 | public function PerfectCMSImageFixFolder($name, ?string $folderName = ''): ?Folder |
||
| 272 | { |
||
| 273 | $folder = null; |
||
| 274 | if(PerfectCMSImages::move_to_right_folder($name) || $folderName) { |
||
| 275 | $image = $this->getOwner(); |
||
| 276 | if($image && $image->exists()) { |
||
| 277 | if(! $folderName) { |
||
| 278 | $folderName = PerfectCMSImages::get_folder($name); |
||
| 279 | } |
||
| 280 | $folder = Folder::find_or_make($folderName); |
||
| 281 | if(!$folder->exists()) { |
||
| 282 | $folder->write(); |
||
| 283 | } |
||
| 284 | if ($image->ParentID !== $folder->ID) { |
||
| 285 | $wasPublished = $image->isPublished(); |
||
| 286 | $image->ParentID = $folder->ID; |
||
| 287 | $image->write(); |
||
| 288 | if($wasPublished) { |
||
| 289 | $image->publishSingle(); |
||
| 290 | } |
||
| 291 | } |
||
| 292 | } else { |
||
| 293 | // user_error('could not find image'); |
||
| 294 | } |
||
| 295 | } |
||
| 296 | return $folder; |
||
| 297 | } |
||
| 298 | |||
| 299 | public function getThumbnail() { |
||
| 300 | if($this->owner->ID){ |
||
| 301 | if($this->owner->getExtension() == 'svg'){ |
||
| 302 | $obj= DBHTMLText::create(); |
||
| 303 | $obj->setValue(file_get_contents(BASE_PATH.$this->owner->Link())); |
||
| 304 | return $obj; |
||
| 305 | }else { |
||
| 306 | return $this->owner->CMSThumbnail(); |
||
| 307 | } |
||
| 308 | } else { |
||
| 309 | return $this->owner->CMSThumbnail(); |
||
| 310 | } |
||
| 311 | } |
||
| 312 | |||
| 313 | public function updatePreviewLink(&$link, $action) |
||
| 320 | } |
||
| 321 | |||
| 322 | } |
||
| 323 |