Conditions | 10 |
Paths | 25 |
Total Lines | 77 |
Code Lines | 46 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
89 | public static function getVideoThumbnail($gatewayHandle, $videoId, $size): ?string |
||
90 | { |
||
91 | $baseDir = Craft::$app->getPath()->getRuntimePath() . DIRECTORY_SEPARATOR . 'videos' . DIRECTORY_SEPARATOR . 'thumbnails' . DIRECTORY_SEPARATOR . $gatewayHandle . DIRECTORY_SEPARATOR . $videoId; |
||
92 | $originalDir = $baseDir . DIRECTORY_SEPARATOR . 'original'; |
||
93 | $dir = $baseDir . DIRECTORY_SEPARATOR . $size; |
||
94 | |||
95 | $file = self::getThumbnailFile($dir); |
||
96 | |||
97 | if (!$file) { |
||
98 | // Retrieve original image |
||
99 | $originalPath = null; |
||
100 | |||
101 | if (is_dir($originalDir)) { |
||
102 | $originalFiles = FileHelper::findFiles($originalDir); |
||
103 | |||
104 | if ($originalFiles !== []) { |
||
105 | $originalPath = $originalFiles[0]; |
||
106 | } |
||
107 | } |
||
108 | |||
109 | if (!$originalPath) { |
||
110 | try { |
||
111 | $video = Plugin::$plugin->getVideos()->getVideoById($gatewayHandle, $videoId); |
||
112 | } catch (ApiResponseException $apiResponseException) { |
||
113 | Craft::info('Couldn’t get video thumbnail:' . "\r\n" |
||
114 | . 'Message: ' . "\r\n" . $apiResponseException->getMessage() . "\r\n" |
||
115 | . 'Trace: ' . "\r\n" . $apiResponseException->getTraceAsString(), __METHOD__); |
||
116 | return null; |
||
117 | } |
||
118 | |||
119 | $url = $video->thumbnailSource; |
||
120 | |||
121 | $name = pathinfo($url, PATHINFO_BASENAME); |
||
122 | $originalPath = $originalDir . DIRECTORY_SEPARATOR . $name; |
||
|
|||
123 | |||
124 | FileHelper::createDirectory($originalDir); |
||
125 | $client = new \GuzzleHttp\Client(); |
||
126 | $response = $client->request('GET', $url, [ |
||
127 | 'sink' => $originalPath, |
||
128 | ]); |
||
129 | |||
130 | // Make sure the original file has an extension |
||
131 | $mimeByExt = FileHelper::getMimeTypeByExtension($originalPath); |
||
132 | |||
133 | if (!$mimeByExt) { |
||
134 | // Add the extension to the filename if it doesn’t have one |
||
135 | $mime = FileHelper::getMimeType($originalPath); |
||
136 | $ext = FileHelper::getExtensionByMimeType($mime); |
||
137 | |||
138 | if ($ext !== '' && $ext !== '0') { |
||
139 | $name .= '.' . $ext; |
||
140 | $targetPath = $originalDir . DIRECTORY_SEPARATOR . $name; |
||
141 | |||
142 | rename($originalPath, $targetPath); |
||
143 | |||
144 | $originalPath = $targetPath; |
||
145 | } |
||
146 | } |
||
147 | |||
148 | if ($response->getStatusCode() !== 200) { |
||
149 | return null; |
||
150 | } |
||
151 | } else { |
||
152 | $name = pathinfo($originalPath, PATHINFO_BASENAME); |
||
153 | } |
||
154 | |||
155 | // Generate the thumb |
||
156 | $path = $dir . DIRECTORY_SEPARATOR . $name; |
||
157 | FileHelper::createDirectory($dir); |
||
158 | Craft::$app->getImages()->loadImage($originalPath, false, $size) |
||
159 | ->scaleToFit($size, $size) |
||
160 | ->saveAs(parse_url($path, PHP_URL_PATH)); |
||
161 | } else { |
||
162 | $name = pathinfo($file, PATHINFO_BASENAME); |
||
163 | } |
||
164 | |||
165 | return Craft::$app->getAssetManager()->getPublishedUrl($dir, true) . sprintf('/%s', $name); |
||
166 | } |
||
229 |