| Conditions | 8 |
| Paths | 4 |
| Total Lines | 68 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 53 | public static function fetch(string $pathOrUrl, callable $callback = null, string $cacheKeySuffix = '') |
||
| 54 | { |
||
| 55 | $pathOrUrl = (string)Craft::parseEnv($pathOrUrl); |
||
| 56 | // Create the dependency tags |
||
| 57 | $dependency = new TagDependency([ |
||
| 58 | 'tags' => [ |
||
| 59 | self::CACHE_TAG . $cacheKeySuffix, |
||
| 60 | self::CACHE_TAG . $cacheKeySuffix . $pathOrUrl, |
||
| 61 | ], |
||
| 62 | ]); |
||
| 63 | // If this is a file path such as for the `manifest.json`, add a FileDependency so it's cache bust if the file changes |
||
| 64 | if (!UrlHelper::isAbsoluteUrl($pathOrUrl)) { |
||
| 65 | $dependency = new ChainedDependency([ |
||
| 66 | 'dependencies' => [ |
||
| 67 | new FileDependency([ |
||
| 68 | 'fileName' => $pathOrUrl |
||
| 69 | ]), |
||
| 70 | $dependency |
||
| 71 | ] |
||
| 72 | ]); |
||
| 73 | } |
||
| 74 | // Set the cache duration based on devMode |
||
| 75 | $cacheDuration = Craft::$app->getConfig()->getGeneral()->devMode |
||
| 76 | ? self::DEVMODE_CACHE_DURATION |
||
| 77 | : null; |
||
| 78 | // Get the result from the cache, or parse the file |
||
| 79 | $cache = Craft::$app->getCache(); |
||
| 80 | return $cache->getOrSet( |
||
| 81 | self::CACHE_KEY . $cacheKeySuffix . $pathOrUrl, |
||
| 82 | function () use ($pathOrUrl, $callback) { |
||
| 83 | $contents = null; |
||
| 84 | $result = null; |
||
| 85 | if (UrlHelper::isAbsoluteUrl($pathOrUrl)) { |
||
| 86 | // See if we can connect to the server |
||
| 87 | $clientOptions = [ |
||
| 88 | RequestOptions::HTTP_ERRORS => false, |
||
| 89 | RequestOptions::CONNECT_TIMEOUT => 3, |
||
| 90 | RequestOptions::VERIFY => false, |
||
| 91 | RequestOptions::TIMEOUT => 5, |
||
| 92 | ]; |
||
| 93 | $client = new Client($clientOptions); |
||
| 94 | try { |
||
| 95 | $response = $client->request('GET', $pathOrUrl, [ |
||
| 96 | RequestOptions::HEADERS => [ |
||
| 97 | 'User-Agent' => self::USER_AGENT_STRING, |
||
| 98 | 'Accept' => '*/*', |
||
| 99 | ], |
||
| 100 | ]); |
||
| 101 | if ($response->getStatusCode() === 200) { |
||
| 102 | $contents = $response->getBody()->getContents(); |
||
| 103 | } |
||
| 104 | } catch (Throwable $e) { |
||
| 105 | Craft::error($e, __METHOD__); |
||
| 106 | } |
||
| 107 | } else { |
||
| 108 | $contents = @file_get_contents($pathOrUrl); |
||
| 109 | } |
||
| 110 | if ($contents) { |
||
| 111 | $result = $contents; |
||
| 112 | if ($callback) { |
||
| 113 | $result = $callback($result); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | return $result; |
||
| 118 | }, |
||
| 119 | $cacheDuration, |
||
| 120 | $dependency |
||
| 121 | ); |
||
| 124 |