| Conditions | 8 |
| Paths | 30 |
| Total Lines | 57 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 39 | public static function getUserProfileImageResourceUrl($remoteUserId, $size = 48) |
||
| 40 | { |
||
| 41 | $baseDir = Craft::$app->getPath()->getRuntimePath().DIRECTORY_SEPARATOR.'twitter'.DIRECTORY_SEPARATOR.'userimages'.DIRECTORY_SEPARATOR.$remoteUserId; |
||
| 42 | $originalDir = $baseDir.DIRECTORY_SEPARATOR.'original'; |
||
| 43 | $dir = $baseDir.DIRECTORY_SEPARATOR.$size; |
||
| 44 | $file = null; |
||
| 45 | |||
| 46 | if (is_dir($dir)) { |
||
| 47 | $files = FileHelper::findFiles($dir); |
||
| 48 | |||
| 49 | if (count($files) > 0) { |
||
| 50 | $file = $files[0]; |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | if (!$file) { |
||
| 55 | // Retrieve original image |
||
| 56 | $originalPath = null; |
||
| 57 | |||
| 58 | if (is_dir($originalDir)) { |
||
| 59 | $originalFiles = FileHelper::findFiles($originalDir); |
||
| 60 | |||
| 61 | if (count($originalFiles) > 0) { |
||
| 62 | $originalPath = $originalFiles[0]; |
||
| 63 | } |
||
| 64 | } |
||
| 65 | if (!$originalPath) { |
||
| 66 | $user = Plugin::getInstance()->getApi()->getUserById($remoteUserId); |
||
| 67 | |||
| 68 | $url = str_replace('_normal', '', $user['profile_image_url_https']); |
||
| 69 | $name = pathinfo($url, PATHINFO_BASENAME); |
||
| 70 | $originalPath = $originalDir.DIRECTORY_SEPARATOR.$name; |
||
|
|
|||
| 71 | |||
| 72 | FileHelper::createDirectory($originalDir); |
||
| 73 | $client = new \GuzzleHttp\Client(); |
||
| 74 | $response = $client->request('GET', $url, [ |
||
| 75 | 'sink' => $originalPath, |
||
| 76 | ]); |
||
| 77 | |||
| 78 | if ($response->getStatusCode() != 200) { |
||
| 79 | return null; |
||
| 80 | } |
||
| 81 | } else { |
||
| 82 | $name = pathinfo($originalPath, PATHINFO_BASENAME); |
||
| 83 | } |
||
| 84 | |||
| 85 | // Generate the thumb |
||
| 86 | $path = $dir.DIRECTORY_SEPARATOR.$name; |
||
| 87 | FileHelper::createDirectory($dir); |
||
| 88 | Craft::$app->getImages()->loadImage($originalPath, false, $size) |
||
| 89 | ->scaleToFit($size, $size) |
||
| 90 | ->saveAs($path); |
||
| 91 | } else { |
||
| 92 | $name = pathinfo($file, PATHINFO_BASENAME); |
||
| 93 | } |
||
| 94 | |||
| 95 | return Craft::$app->getAssetManager()->getPublishedUrl($dir, true)."/{$name}"; |
||
| 96 | } |
||
| 185 |