| Conditions | 12 |
| Paths | 8 |
| Total Lines | 43 |
| Code Lines | 22 |
| 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 |
||
| 94 | protected function doRequest() |
||
| 95 | { |
||
| 96 | $request = new Request('GET', 'addons.silverstripe.org/api/supported-addons'); |
||
| 97 | |||
| 98 | $failureMessage = 'Could not obtain information about supported addons. '; |
||
| 99 | |||
| 100 | try { |
||
| 101 | $response = $this->getGuzzleClient()->send($request, ['http_errors' => false]); |
||
| 102 | } catch (GuzzleException $exception) { |
||
| 103 | throw new RuntimeException($failureMessage); |
||
| 104 | } |
||
| 105 | |||
| 106 | if ($response->getStatusCode() !== 200) { |
||
| 107 | throw new RuntimeException($failureMessage . 'Error code ' . $response->getStatusCode()); |
||
| 108 | } |
||
| 109 | |||
| 110 | if (!in_array('application/json', $response->getHeader('Content-Type'))) { |
||
| 111 | throw new RuntimeException($failureMessage . 'Response is not JSON'); |
||
| 112 | } |
||
| 113 | |||
| 114 | $responseBody = Convert::json2array($response->getBody()->getContents()); |
||
| 115 | |||
| 116 | if (empty($responseBody)) { |
||
| 117 | throw new RuntimeException($failureMessage . 'Response could not be parsed'); |
||
| 118 | } |
||
| 119 | |||
| 120 | if (!isset($responseBody['success']) || !$responseBody['success'] || !isset($responseBody['addons'])) { |
||
| 121 | throw new RuntimeException($failureMessage . 'Response returned unsuccessfully'); |
||
| 122 | } |
||
| 123 | |||
| 124 | // Handle caching if requested |
||
| 125 | if ($cacheControl = $response->getHeader('Cache-Control')) { |
||
| 126 | // Combine separate header rows |
||
| 127 | $cacheControl = implode(', ', $cacheControl); |
||
| 128 | |||
| 129 | if (strpos($cacheControl, 'no-store') === false && |
||
| 130 | preg_match('/(?:max-age=)(\d+)/i', $cacheControl, $matches)) { |
||
| 131 | $duration = (int) $matches[1]; |
||
| 132 | $this->getCache()->set('addons', $responseBody['addons'], $duration); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | return $responseBody['addons'] ?: []; |
||
| 137 | } |
||
| 171 |