Total Complexity | 40 |
Total Lines | 297 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 0 |
Complex classes like ImageTransform 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 ImageTransform, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class ImageTransform |
||
35 | { |
||
36 | // Constants |
||
37 | // ========================================================================= |
||
38 | |||
39 | public const SOCIAL_TRANSFORM_QUALITY = 82; |
||
40 | |||
41 | public const ALLOWED_SOCIAL_MIME_TYPES = [ |
||
42 | 'image/jpeg', |
||
43 | 'image/png', |
||
44 | 'image/webp', |
||
45 | 'image/gif', |
||
46 | ]; |
||
47 | |||
48 | public const DEFAULT_SOCIAL_FORMAT = 'jpg'; |
||
49 | |||
50 | // Static Public Properties |
||
51 | // ========================================================================= |
||
52 | |||
53 | /** |
||
54 | * @var bool |
||
55 | */ |
||
56 | public static $pendingImageTransforms = false; |
||
57 | |||
58 | // Static Private Properties |
||
59 | // ========================================================================= |
||
60 | |||
61 | private static $transforms = [ |
||
62 | 'base' => [ |
||
63 | 'format' => null, |
||
64 | 'quality' => self::SOCIAL_TRANSFORM_QUALITY, |
||
65 | 'width' => 1200, |
||
66 | 'height' => 630, |
||
67 | 'mode' => 'crop', |
||
68 | ], |
||
69 | 'facebook' => [ |
||
70 | 'width' => 1200, |
||
71 | 'height' => 630, |
||
72 | ], |
||
73 | 'twitter-summary' => [ |
||
74 | 'width' => 800, |
||
75 | 'height' => 800, |
||
76 | ], |
||
77 | 'twitter-large' => [ |
||
78 | 'width' => 800, |
||
79 | 'height' => 418, |
||
80 | ], |
||
81 | 'schema-logo' => [ |
||
82 | 'format' => 'png', |
||
83 | 'width' => 600, |
||
84 | 'height' => 60, |
||
85 | 'mode' => 'fit', |
||
86 | ], |
||
87 | ]; |
||
88 | |||
89 | private static $cachedAssetsElements = []; |
||
90 | |||
91 | // Static Methods |
||
92 | // ========================================================================= |
||
93 | |||
94 | /** |
||
95 | * Transform the $asset for social media sites in $transformName and |
||
96 | * optional $siteId |
||
97 | * |
||
98 | * @param int|Asset $asset the Asset or Asset ID |
||
99 | * @param string $transformName the name of the transform to apply |
||
100 | * @param int|null $siteId |
||
101 | * @param string $transformMode |
||
102 | * |
||
103 | * @return string URL to the transformed image |
||
104 | */ |
||
105 | public static function socialTransform( |
||
106 | $asset, |
||
107 | $transformName = '', |
||
108 | $siteId = null, |
||
109 | $transformMode = null, |
||
110 | ): string { |
||
111 | $url = ''; |
||
112 | $transform = self::createSocialTransform($transformName); |
||
113 | // Let them override the mode |
||
114 | if (empty($transformMode)) { |
||
115 | $transformMode = $transform->mode ?? 'crop'; |
||
116 | } |
||
117 | if ($transform !== null) { |
||
118 | $transform->mode = $transformMode; |
||
119 | } |
||
120 | $asset = self::assetFromAssetOrIdOrQuery($asset, $siteId); |
||
121 | if (($asset !== null) && ($asset instanceof Asset)) { |
||
122 | // Make sure the format is an allowed format, otherwise explicitly change it |
||
123 | $mimeType = $asset->getMimeType(); |
||
124 | if ($transform !== null && !in_array($mimeType, self::ALLOWED_SOCIAL_MIME_TYPES, false)) { |
||
125 | $transform->format = self::DEFAULT_SOCIAL_FORMAT; |
||
126 | } |
||
127 | // Generate a transformed image |
||
128 | $assets = Craft::$app->getAssets(); |
||
129 | try { |
||
130 | $volume = $asset->getVolume(); |
||
131 | } catch (InvalidConfigException $e) { |
||
132 | $volume = null; |
||
133 | } |
||
134 | // If we're not in local dev, tell it to generate the transform immediately so that |
||
135 | // urls like `actions/assets/generate-transform` don't get cached |
||
136 | $generateNow = Seomatic::$environment === EnvironmentHelper::SEOMATIC_DEV_ENV ? null : true; |
||
137 | if ($volume->getFs() instanceof Local) { |
||
138 | // Preflight to ensure that the source asset actually exists to avoid Craft hanging |
||
139 | if (!$volume->fileExists($asset->getPath())) { |
||
140 | $generateNow = false; |
||
141 | } |
||
142 | } else { |
||
143 | // If this is not a local volume, avoid a potentially long round-trip by |
||
144 | // being paranoid, and defaulting to not generating the image now |
||
145 | // if we're in local dev |
||
146 | if (Seomatic::$environment === EnvironmentHelper::SEOMATIC_DEV_ENV) { |
||
147 | $generateNow = false; |
||
148 | } |
||
149 | } |
||
150 | try { |
||
151 | $url = $asset->getUrl($transform, $generateNow); |
||
152 | } catch (InvalidConfigException $e) { |
||
153 | $url = null; |
||
154 | } |
||
155 | if ($url === null) { |
||
156 | $url = ''; |
||
157 | } |
||
158 | // If we have a url, add an `mtime` param to cache bust |
||
159 | if (!empty($url) && empty(parse_url($url, PHP_URL_QUERY))) { |
||
160 | $now = new DateTime(); |
||
161 | $newestChange = max($asset->dateModified, $asset->dateUpdated); |
||
162 | $url = UrlHelper::url($url, [ |
||
163 | 'mtime' => $newestChange->getTimestamp(), |
||
164 | ]); |
||
165 | } |
||
166 | } |
||
167 | // Check to see if the $url contains a pending image transform |
||
168 | if (!empty($url) && StringHelper::contains($url, 'assets/generate-transform')) { |
||
169 | self::$pendingImageTransforms = true; |
||
170 | } |
||
171 | |||
172 | return $url; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @param int|Asset $asset the Asset or Asset ID |
||
177 | * @param string $transformName the name of the transform to apply |
||
178 | * @param int|null $siteId |
||
179 | * @param string $transformMode |
||
180 | * |
||
181 | * @return string width of the transformed image |
||
182 | */ |
||
183 | public static function socialTransformWidth( |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * @param int|Asset $asset the Asset or Asset ID |
||
209 | * @param string $transformName the name of the transform to apply |
||
210 | * @param int|null $siteId |
||
211 | * @param string $transformMode |
||
212 | * |
||
213 | * @return string width of the transformed image |
||
214 | */ |
||
215 | public static function socialTransformHeight( |
||
216 | $asset, |
||
217 | $transformName = '', |
||
218 | $siteId = null, |
||
219 | $transformMode = null, |
||
220 | ): string { |
||
221 | $height = ''; |
||
222 | $transform = self::createSocialTransform($transformName); |
||
223 | // Let them override the mode |
||
224 | if ($transform !== null) { |
||
225 | $transform->mode = $transformMode ?? $transform->mode; |
||
226 | } |
||
227 | $asset = self::assetFromAssetOrIdOrQuery($asset, $siteId); |
||
228 | if ($asset instanceof Asset) { |
||
229 | $height = $asset->getHeight($transform); |
||
230 | if ($height === null) { |
||
231 | $height = ''; |
||
232 | } |
||
233 | $height = (string)$height; |
||
234 | } |
||
235 | |||
236 | return $height; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Return an array of Asset elements from an array of element IDs |
||
241 | * |
||
242 | * @param array|string $assetIds |
||
243 | * @param int|null $siteId |
||
244 | * |
||
245 | * @return array |
||
246 | */ |
||
247 | public static function assetElementsFromIds($assetIds, $siteId = null): array |
||
248 | { |
||
249 | $elements = Craft::$app->getElements(); |
||
250 | $assets = []; |
||
251 | if (!empty($assetIds)) { |
||
252 | if (is_array($assetIds)) { |
||
253 | foreach ($assetIds as $assetId) { |
||
254 | if (!empty($assetId)) { |
||
255 | $assets[] = $elements->getElementById((int)$assetId, Asset::class, $siteId); |
||
256 | } |
||
257 | } |
||
258 | } else { |
||
259 | $assetId = $assetIds; |
||
260 | $assets[] = $elements->getElementById((int)$assetId, Asset::class, $siteId); |
||
261 | } |
||
262 | } |
||
263 | |||
264 | return array_filter($assets); |
||
265 | } |
||
266 | |||
267 | // Protected Static Methods |
||
268 | // ========================================================================= |
||
269 | |||
270 | /** |
||
271 | * Return an asset from either an id or an asset |
||
272 | * |
||
273 | * @param int|array|ElementCollection|Asset|ElementQuery $asset the Asset or Asset ID or ElementQuery |
||
274 | * @param int|null $siteId |
||
275 | * |
||
276 | * @return Asset|array|ElementInterface|null |
||
277 | */ |
||
278 | protected static function assetFromAssetOrIdOrQuery($asset, $siteId = null) |
||
279 | { |
||
280 | if (empty($asset)) { |
||
281 | return null; |
||
282 | } |
||
283 | // If it's an array (eager loaded Element query), return the first element |
||
284 | if (is_array($asset)) { |
||
285 | return reset($asset); |
||
286 | } |
||
287 | // If it's an asset already, just return it |
||
288 | if ($asset instanceof Asset) { |
||
289 | return $asset; |
||
290 | } |
||
291 | // If it is a Collection, resolve that to an asset |
||
292 | if ($asset instanceof ElementCollection) { |
||
293 | return $asset->first(); |
||
294 | } |
||
295 | // If it is an ElementQuery, resolve that to an asset |
||
296 | if ($asset instanceof ElementQuery) { |
||
297 | return $asset->one(); |
||
298 | } |
||
299 | |||
300 | $resolvedAssetId = (int)$asset; |
||
301 | $resolvedSiteId = $siteId ?? 0; |
||
302 | if (isset(self::$cachedAssetsElements[$resolvedAssetId][$resolvedSiteId])) { |
||
303 | return self::$cachedAssetsElements[$resolvedAssetId][$resolvedSiteId]; |
||
304 | } |
||
305 | |||
306 | $asset = Craft::$app->getAssets()->getAssetById($resolvedAssetId, $siteId); |
||
307 | self::$cachedAssetsElements[$resolvedAssetId][$resolvedSiteId] = $asset; |
||
308 | |||
309 | return $asset; |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * Create a transform from the passed in $transformName |
||
314 | * |
||
315 | * @param string $transformName the name of the transform to apply |
||
316 | * |
||
317 | * @return ImageTransformModel|null |
||
318 | */ |
||
319 | protected static function createSocialTransform($transformName = 'base') |
||
331 | } |
||
332 | } |
||
333 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths