| Total Complexity | 55 | 
| Total Lines | 249 | 
| Duplicated Lines | 0 % | 
| Changes | 5 | ||
| Bugs | 0 | Features | 0 | 
Complex classes like Image 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 Image, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace GeneaLabs\LaravelImagery;  | 
            ||
| 8 | class Image extends Model  | 
            ||
| 9 | { | 
            ||
| 10 | protected $originalHeight;  | 
            ||
| 11 | protected $originalWidth;  | 
            ||
| 12 | // protected $htmlAttributes;  | 
            ||
| 13 | protected $heightIsPercentage;  | 
            ||
| 14 | protected $widthIsPercentage;  | 
            ||
| 15 | // protected $source;  | 
            ||
| 16 | // protected $height;  | 
            ||
| 17 | // protected $width;  | 
            ||
| 18 | // protected $originalPath;  | 
            ||
| 19 | protected $alwaysPreserveAspectRatio;  | 
            ||
| 20 | protected $doNotCreateDerivativeImages;  | 
            ||
| 21 | protected $overrideScreenConstraint;  | 
            ||
| 22 | protected $screenConstraintMethod;  | 
            ||
| 23 | |||
| 24 | //TODO: this class needs serious refactoring!!!  | 
            ||
| 25 | public function __construct(  | 
            ||
| 26 | string $source,  | 
            ||
| 27 | string $width = null,  | 
            ||
| 28 | string $height = null,  | 
            ||
| 29 | Collection $htmlAttributes = null,  | 
            ||
| 30 | Collection $options = null  | 
            ||
| 31 |     ) { | 
            ||
| 32 | parent::__construct();  | 
            ||
| 33 | |||
| 34 | $this->createCacheFolderIfMissing();  | 
            ||
| 35 | |||
| 36 | $this->originalHeight = $height;  | 
            ||
| 37 | $this->originalWidth = $width;  | 
            ||
| 38 | $this->htmlAttributes = $htmlAttributes;  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 39 | $this->heightIsPercentage = str_contains($height, '%');  | 
            ||
| 40 | $this->widthIsPercentage = str_contains($width, '%');  | 
            ||
| 41 | $this->source = $source;  | 
            ||
| 42 | $this->image = (new ImageManager)->make($source);  | 
            ||
| 43 | $this->height = intval($height);  | 
            ||
| 44 | $this->width = intval($width);  | 
            ||
| 45 |         $this->originalPath = public_path(config('genealabs-laravel-imagery.storage-folder') . basename($source)); | 
            ||
| 46 |         $this->alwaysPreserveAspectRatio = $options->get('alwaysPreserveAspectRatio', true); | 
            ||
| 47 |         $this->doNotCreateDerivativeImages = $options->get('doNotCreateDerivativeImages', false); | 
            ||
| 48 |         $this->overrideScreenConstraint = $options->get('overrideScreenConstraint', false); | 
            ||
| 49 |         $this->screenConstraintMethod = $options->get('screenConstraintMethod', 'contain'); | 
            ||
| 50 | |||
| 51 |         if ($this->sourceIsUrl($source)) { | 
            ||
| 52 | $this->image->save($this->originalPath);  | 
            ||
| 53 | }  | 
            ||
| 54 | |||
| 55 | $this->resizeImage($this->width, $this->height, $this->alwaysPreserveAspectRatio);  | 
            ||
| 56 | |||
| 57 |         if (! $this->doNotCreateDerivativeImages) { | 
            ||
| 58 |             $job = (new RenderDerivativeImages($this->source))->onQueue('imagery'); | 
            ||
| 59 | dispatch($job);  | 
            ||
| 60 | }  | 
            ||
| 61 | }  | 
            ||
| 62 | |||
| 63 | protected function resizeImage(int $width, int $height, bool $alwaysPreserveAspect = false)  | 
            ||
| 64 |     { | 
            ||
| 65 |         if (! $height || ! $width) { | 
            ||
| 66 | $height = $height ?: $this->image->getHeight();  | 
            ||
| 67 | $width = $width ?: $this->image->getWidth();  | 
            ||
| 68 | }  | 
            ||
| 69 | |||
| 70 | $screenHeight = $_COOKIE['screenWidth'] ?? null;  | 
            ||
| 71 | $screenWidth = $_COOKIE['screenHeight'] ?? null;  | 
            ||
| 72 | $screenHeight = $screenWidth ? intval($screenWidth) : null;  | 
            ||
| 73 | $screenWidth = $screenWidth ? intval($screenWidth) : null;  | 
            ||
| 74 | $height = $this->determineHeight($height, $screenHeight);  | 
            ||
| 75 | $width = $this->determineWidth($width, $screenWidth);  | 
            ||
| 76 | |||
| 77 |         if (! $height && ! $width) { | 
            ||
| 78 | $height = $this->image->height();  | 
            ||
| 79 | $width = $this->image->width();  | 
            ||
| 80 | }  | 
            ||
| 81 | |||
| 82 | $maxHeight = $this->determineMaxHeight($height, $screenHeight, $screenWidth);  | 
            ||
| 83 | $maxWidth = $this->determineMaxWidth($width, $screenHeight, $screenWidth);  | 
            ||
| 84 | |||
| 85 |         $this->image->resize($maxWidth, $maxHeight, function ($constraint) use ($alwaysPreserveAspect) { | 
            ||
| 86 |             if ($alwaysPreserveAspect || ! $width || ! $height) { | 
            ||
| 87 | $constraint->aspectRatio();  | 
            ||
| 88 | }  | 
            ||
| 89 | |||
| 90 | $constraint->upsize();  | 
            ||
| 91 | });  | 
            ||
| 92 | |||
| 93 | $this->height = $this->image->height();  | 
            ||
| 94 | $this->width = $this->image->width();  | 
            ||
| 95 | $this->storeImage();  | 
            ||
| 96 | }  | 
            ||
| 97 | |||
| 98 | //TODO: refactor to have a single return type, instead of null or int  | 
            ||
| 99 | protected function determineMaxHeight($height, $screenHeight, $screenWidth)  | 
            ||
| 121 | }  | 
            ||
| 122 | |||
| 123 | //TODO: refactor to have a single return type, instead of null or int  | 
            ||
| 124 | protected function determineMaxWidth($width, $screenHeight, $screenWidth)  | 
            ||
| 125 |     { | 
            ||
| 126 |         if (! $screenHeight || ! $screenWidth) { | 
            ||
| 127 | return $width;  | 
            ||
| 128 | }  | 
            ||
| 129 | |||
| 130 | $maxWidth = $width ?: $this->image->width();  | 
            ||
| 131 | |||
| 132 |         if (! $this->overrideScreenConstraint) { | 
            ||
| 133 | $maxWidth = $screenWidth < $maxWidth ? $screenWidth : $maxWidth;  | 
            ||
| 134 | |||
| 135 |             if ($this->screenConstraintMethod === 'cover') { | 
            ||
| 136 | $imageToScreenHeight = $screenHeight / $this->image->height();  | 
            ||
| 137 | $imageToScreenWidth = $screenWidth / $this->image->width();  | 
            ||
| 138 |                 if ($imageToScreenHeight > $imageToScreenWidth) { | 
            ||
| 139 | $maxWidth = null;  | 
            ||
| 140 | }  | 
            ||
| 141 | }  | 
            ||
| 142 | }  | 
            ||
| 143 | |||
| 144 | return $maxWidth;  | 
            ||
| 145 | }  | 
            ||
| 146 | |||
| 147 | protected function determineHeight($height, $screenHeight) : int  | 
            ||
| 148 |     { | 
            ||
| 149 |         if ($screenHeight && $height && $this->heightIsPercentage) { | 
            ||
| 150 | return $screenHeight * ($height / 100);  | 
            ||
| 151 | }  | 
            ||
| 152 | |||
| 153 | return $height;  | 
            ||
| 154 | }  | 
            ||
| 155 | |||
| 156 | protected function determineWidth($width, $screenWidth) : int  | 
            ||
| 157 |     { | 
            ||
| 158 |         if ($screenWidth && $width && $this->widthIsPercentage) { | 
            ||
| 159 | return $screenWidth * ($width / 100);  | 
            ||
| 160 | }  | 
            ||
| 161 | |||
| 162 | return $width;  | 
            ||
| 163 | }  | 
            ||
| 164 | |||
| 165 | protected function sourceIsUrl() : bool  | 
            ||
| 166 |     { | 
            ||
| 167 |         return collect(parse_url($this->source))->has('scheme'); | 
            ||
| 168 | }  | 
            ||
| 169 | |||
| 170 | protected function storeImage()  | 
            ||
| 171 |     { | 
            ||
| 172 | $this->image  | 
            ||
| 173 |             ->save(public_path(config('genealabs-laravel-imagery.storage-folder') . $this->fileName)); | 
            ||
| 174 | }  | 
            ||
| 175 | |||
| 176 | public function getFileNameAttribute() : string  | 
            ||
| 177 |     { | 
            ||
| 178 | $pathParts = pathinfo($this->source);  | 
            ||
| 179 | $fileName = $pathParts['filename'];  | 
            ||
| 180 | $extension = $pathParts['extension'] ?? '';  | 
            ||
| 181 |         $extension = $extension ? ".{$extension}" : ''; | 
            ||
| 182 | |||
| 183 |         if ($this->width || $this->height) { | 
            ||
| 184 |             $fileName .= "_{$this->width}x{$this->height}"; | 
            ||
| 185 | }  | 
            ||
| 186 | |||
| 187 |         return "{$fileName}{$extension}"; | 
            ||
| 188 | }  | 
            ||
| 189 | |||
| 190 | public function getImgAttribute() : string  | 
            ||
| 191 |     { | 
            ||
| 192 |         $scriptUrl = mix('js/cookie.js', 'genealabs-laravel-imagery'); | 
            ||
| 193 | $attributes = '';  | 
            ||
| 194 | |||
| 195 |         $attributes = $this->htmlAttributes->map(function ($value, $attribute) use (&$attributes) { | 
            ||
| 196 |             return " {$attribute}=\"{$value}\""; | 
            ||
| 197 |         })->implode(''); | 
            ||
| 198 | |||
| 199 |         return "<img src=\"{$this->url}\" | 
            ||
| 200 |             width=\"{$this->originalWidth}\" | 
            ||
| 201 |             height=\"{$this->originalHeight}\"{{ $attributes }} | 
            ||
| 202 |         ><script src=\"{$scriptUrl}\"></script>"; | 
            ||
| 203 | }  | 
            ||
| 204 | |||
| 205 | public function getOriginalUrlAttribute() : string  | 
            ||
| 206 |     { | 
            ||
| 207 |         return asset(config('genealabs-laravel-imagery.storage-folder') . $this->fileName); | 
            ||
| 208 | }  | 
            ||
| 209 | |||
| 210 | public function getPathAttribute() : string  | 
            ||
| 211 |     { | 
            ||
| 212 |         return public_path(config('genealabs-laravel-imagery.storage-folder') . $this->fileName); | 
            ||
| 213 | }  | 
            ||
| 214 | |||
| 215 | public function getPictureAttribute() : string  | 
            ||
| 216 |     { | 
            ||
| 217 | //TODO: implement img tag attributes, move script to middleware injector  | 
            ||
| 218 |         $scriptUrl = mix('js/cookie.js', 'genealabs-laravel-imagery'); | 
            ||
| 219 | $sources = '';  | 
            ||
| 220 | |||
| 221 |         foreach (array_reverse(config('genealabs-laravel-imagery.size-presets')) as $sizePreset) { | 
            ||
| 222 | $image = (new Imagery)->conjure(  | 
            ||
| 223 | $this->source,  | 
            ||
| 224 | $sizePreset,  | 
            ||
| 225 | $sizePreset,  | 
            ||
| 226 | [],  | 
            ||
| 227 | ['doNotCreateDerivativeImages' => true]  | 
            ||
| 228 | );  | 
            ||
| 229 | |||
| 230 |             if ($sizePreset < $this->width || $sizePreset < $this->height) { | 
            ||
| 231 |                 $sources .= "<source srcset=\"{$image->url}\" media=\"(min-width: {$sizePreset}px)\">"; | 
            ||
| 232 | }  | 
            ||
| 233 | }  | 
            ||
| 234 | |||
| 235 | return "  | 
            ||
| 236 | <picture>  | 
            ||
| 237 |                 {$sources} | 
            ||
| 238 |                 <img src=\"{$this->url}\"> | 
            ||
| 239 | </picture>  | 
            ||
| 240 |             <script src=\"{$scriptUrl}\"></script> | 
            ||
| 241 | ";  | 
            ||
| 242 | }  | 
            ||
| 243 | |||
| 244 | public function getUrlAttribute() : string  | 
            ||
| 245 |     { | 
            ||
| 246 |         return asset(config('genealabs-laravel-imagery.storage-folder') . $this->fileName); | 
            ||
| 247 | }  | 
            ||
| 248 | |||
| 249 | protected function createCacheFolderIfMissing()  | 
            ||
| 257 | );  | 
            ||
| 258 | }  | 
            ||
| 259 | }  | 
            ||
| 260 | }  | 
            ||
| 261 |