Total Complexity | 55 |
Total Lines | 235 |
Duplicated Lines | 19.15 % |
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 | //TODO: this class needs serious refactoring!!! |
||
11 | public function __construct( |
||
12 | string $source, |
||
13 | string $width = null, |
||
14 | string $height = null, |
||
15 | Collection $htmlAttributes = null, |
||
16 | Collection $options = null |
||
17 | ) { |
||
18 | parent::__construct(); |
||
19 | |||
20 | $this->createCacheFolderIfMissing(); |
||
21 | |||
22 | $this->originalHeight = $height; |
||
|
|||
23 | $this->originalWidth = $width; |
||
24 | $this->htmlAttributes = $htmlAttributes; |
||
25 | $this->heightIsPercentage = str_contains($height, '%'); |
||
26 | $this->widthIsPercentage = str_contains($width, '%'); |
||
27 | $this->source = $source; |
||
28 | $this->image = (new ImageManager)->make($source); |
||
29 | $this->height = intval($height); |
||
30 | $this->width = intval($width); |
||
31 | $this->originalPath = public_path(config('genealabs-laravel-imagery.storage-folder') . basename($source)); |
||
32 | $this->alwaysPreserveAspectRatio = $options->get('alwaysPreserveAspectRatio', true); |
||
33 | $this->doNotCreateDerivativeImages = $options->get('doNotCreateDerivativeImages', false); |
||
34 | $this->overrideScreenConstraint = $options->get('overrideScreenConstraint', false); |
||
35 | $this->screenConstraintMethod = $options->get('screenConstraintMethod', 'contain'); |
||
36 | |||
37 | if ($this->sourceIsUrl($source)) { |
||
38 | $this->image->save($this->originalPath); |
||
39 | } |
||
40 | |||
41 | $this->resizeImage($this->width, $this->height, $this->alwaysPreserveAspectRatio); |
||
42 | |||
43 | if (! $this->doNotCreateDerivativeImages) { |
||
44 | $job = (new RenderDerivativeImages($this->source))->onQueue('imagery'); |
||
45 | dispatch($job); |
||
46 | } |
||
47 | } |
||
48 | |||
49 | protected function resizeImage(int $width, int $height, bool $alwaysPreserveAspect = false) |
||
82 | } |
||
83 | |||
84 | //TODO: refactor to have a single return type, instead of null or int |
||
85 | View Code Duplication | protected function determineMaxHeight($height, $screenHeight, $screenWidth) |
|
107 | } |
||
108 | |||
109 | //TODO: refactor to have a single return type, instead of null or int |
||
110 | View Code Duplication | protected function determineMaxWidth($width, $screenHeight, $screenWidth) |
|
111 | { |
||
112 | if (! $screenHeight || ! $screenWidth) { |
||
113 | return $width; |
||
114 | } |
||
115 | |||
116 | $maxWidth = $width ?: $this->image->width(); |
||
117 | |||
118 | if (! $this->overrideScreenConstraint) { |
||
119 | $maxWidth = $screenWidth < $maxWidth ? $screenWidth : $maxWidth; |
||
120 | |||
121 | if ($this->screenConstraintMethod === 'cover') { |
||
122 | $imageToScreenHeight = $screenHeight / $this->image->height(); |
||
123 | $imageToScreenWidth = $screenWidth / $this->image->width(); |
||
124 | if ($imageToScreenHeight > $imageToScreenWidth) { |
||
125 | $maxWidth = null; |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | |||
130 | return $maxWidth; |
||
131 | } |
||
132 | |||
133 | protected function determineHeight($height, $screenHeight) : int |
||
134 | { |
||
135 | if ($screenHeight && $height && $this->heightIsPercentage) { |
||
136 | return $screenHeight * ($height / 100); |
||
137 | } |
||
138 | |||
139 | return $height; |
||
140 | } |
||
141 | |||
142 | protected function determineWidth($width, $screenWidth) : int |
||
143 | { |
||
144 | if ($screenWidth && $width && $this->widthIsPercentage) { |
||
145 | return $screenWidth * ($width / 100); |
||
146 | } |
||
147 | |||
148 | return $width; |
||
149 | } |
||
150 | |||
151 | protected function sourceIsUrl() : bool |
||
152 | { |
||
153 | return collect(parse_url($this->source))->has('scheme'); |
||
154 | } |
||
155 | |||
156 | protected function storeImage() |
||
157 | { |
||
158 | $this->image |
||
159 | ->save(public_path(config('genealabs-laravel-imagery.storage-folder') . $this->fileName)); |
||
160 | } |
||
161 | |||
162 | public function getFileNameAttribute() : string |
||
163 | { |
||
164 | $pathParts = pathinfo($this->source); |
||
165 | $fileName = $pathParts['filename']; |
||
166 | $extension = $pathParts['extension'] ?? ''; |
||
167 | $extension = $extension ? ".{$extension}" : ''; |
||
168 | |||
169 | if ($this->width || $this->height) { |
||
170 | $fileName .= "_{$this->width}x{$this->height}"; |
||
171 | } |
||
172 | |||
173 | return "{$fileName}{$extension}"; |
||
174 | } |
||
175 | |||
176 | public function getImgAttribute() : string |
||
177 | { |
||
178 | $scriptUrl = mix('js/cookie.js', 'genealabs-laravel-imagery'); |
||
179 | $attributes = ''; |
||
180 | |||
181 | $attributes = $this->htmlAttributes->map(function ($value, $attribute) use (&$attributes) { |
||
182 | return " {$attribute}=\"{$value}\""; |
||
183 | })->implode(''); |
||
184 | |||
185 | return "<img src=\"{$this->url}\" |
||
186 | width=\"{$this->originalWidth}\" |
||
187 | height=\"{$this->originalHeight}\"{{ $attributes }} |
||
188 | ><script src=\"{$scriptUrl}\"></script>"; |
||
189 | } |
||
190 | |||
191 | public function getOriginalUrlAttribute() : string |
||
192 | { |
||
193 | return asset(config('genealabs-laravel-imagery.storage-folder') . $this->fileName); |
||
194 | } |
||
195 | |||
196 | public function getPathAttribute() : string |
||
197 | { |
||
198 | return public_path(config('genealabs-laravel-imagery.storage-folder') . $this->fileName); |
||
199 | } |
||
200 | |||
201 | public function getPictureAttribute() : string |
||
227 | "; |
||
228 | } |
||
229 | |||
230 | public function getUrlAttribute() : string |
||
233 | } |
||
234 | |||
235 | protected function createCacheFolderIfMissing() |
||
243 | ); |
||
244 | } |
||
245 | } |
||
246 | } |
||
247 |