| Total Complexity | 55 |
| Total Lines | 249 |
| Duplicated Lines | 18.07 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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) |
||
| 96 | } |
||
| 97 | |||
| 98 | //TODO: refactor to have a single return type, instead of null or int |
||
| 99 | View Code Duplication | protected function determineMaxHeight($height, $screenHeight, $screenWidth) |
|
| 121 | } |
||
| 122 | |||
| 123 | //TODO: refactor to have a single return type, instead of null or int |
||
| 124 | View Code Duplication | 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 |
||
| 241 | "; |
||
| 242 | } |
||
| 243 | |||
| 244 | public function getUrlAttribute() : string |
||
| 247 | } |
||
| 248 | |||
| 249 | protected function createCacheFolderIfMissing() |
||
| 257 | ); |
||
| 258 | } |
||
| 259 | } |
||
| 260 | } |
||
| 261 |