| Total Complexity | 44 |
| Total Lines | 949 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like BaseResponse 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 BaseResponse, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class BaseResponse |
||
| 24 | {
|
||
| 25 | |||
| 26 | /* |
||
| 27 | |-------------------------------------------------------------------------- |
||
| 28 | | Debug Parameters |
||
| 29 | |-------------------------------------------------------------------------- |
||
| 30 | | |
||
| 31 | | Parameters to aid with debugging |
||
| 32 | | |
||
| 33 | */ |
||
| 34 | /** |
||
| 35 | * Response from GuzzleHTTP |
||
| 36 | * @var Response |
||
| 37 | */ |
||
| 38 | private $response; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Request from GuzzleHTTP |
||
| 42 | * |
||
| 43 | * @var Request |
||
| 44 | */ |
||
| 45 | private $request; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Data returned from the API |
||
| 49 | * |
||
| 50 | * @var array |
||
| 51 | */ |
||
| 52 | private $rawData; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * MetaData returned from the API |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | private $rawMeta; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Options passed into Guzzle HTTP |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | private $requestOptions; |
||
| 65 | |||
| 66 | |||
| 67 | |||
| 68 | |||
| 69 | |||
| 70 | |||
| 71 | |||
| 72 | |||
| 73 | |||
| 74 | |||
| 75 | |||
| 76 | |||
| 77 | |||
| 78 | |||
| 79 | |||
| 80 | /* |
||
| 81 | |-------------------------------------------------------------------------- |
||
| 82 | | MetaData Parameters |
||
| 83 | |-------------------------------------------------------------------------- |
||
| 84 | | |
||
| 85 | | Parameters to do with the metadata |
||
| 86 | | |
||
| 87 | */ |
||
| 88 | /** |
||
| 89 | * Status code of the request |
||
| 90 | * |
||
| 91 | * @var int |
||
| 92 | */ |
||
| 93 | private $statusCode; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * X-Request-ID from the response |
||
| 97 | * @var string |
||
| 98 | */ |
||
| 99 | private $xRequestID; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * RunTime on the UnionCloud server |
||
| 103 | * @var double |
||
| 104 | */ |
||
| 105 | private $runTime; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Date in the HTTP Header |
||
| 109 | * |
||
| 110 | * @var Carbon |
||
| 111 | */ |
||
| 112 | private $responseDate; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Number of records per page |
||
| 116 | * |
||
| 117 | * @var int |
||
| 118 | */ |
||
| 119 | private $recordsPerPage; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Total pages returned |
||
| 123 | * @var int |
||
| 124 | */ |
||
| 125 | private $totalPages; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Total number of records returned |
||
| 129 | * @var int |
||
| 130 | */ |
||
| 131 | private $totalRecords; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Number of failed records |
||
| 135 | * |
||
| 136 | * @var int |
||
| 137 | */ |
||
| 138 | private $failedRecords; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Number of successful records |
||
| 142 | * |
||
| 143 | * @var int |
||
| 144 | */ |
||
| 145 | private $successfulRecords; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Is the request able to use the pagination functions? |
||
| 149 | * |
||
| 150 | * @var bool |
||
| 151 | */ |
||
| 152 | private $hasPagination = false; |
||
| 153 | |||
| 154 | |||
| 155 | |||
| 156 | /* |
||
| 157 | |-------------------------------------------------------------------------- |
||
| 158 | | Resource settings and container |
||
| 159 | |-------------------------------------------------------------------------- |
||
| 160 | | |
||
| 161 | | Parameters concerning resources and which classes can handle resources |
||
| 162 | | |
||
| 163 | */ |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Class to treat as the resource handler |
||
| 167 | * |
||
| 168 | * @var string |
||
| 169 | */ |
||
| 170 | private $resourceClass; |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Collection of resource classes representing UnionCloud objects |
||
| 174 | * |
||
| 175 | * @var ResourceCollection |
||
| 176 | */ |
||
| 177 | protected $resources; |
||
| 178 | |||
| 179 | |||
| 180 | |||
| 181 | |||
| 182 | |||
| 183 | |||
| 184 | |||
| 185 | |||
| 186 | |||
| 187 | |||
| 188 | |||
| 189 | |||
| 190 | |||
| 191 | /* |
||
| 192 | |-------------------------------------------------------------------------- |
||
| 193 | | Ready to parse a response |
||
| 194 | |-------------------------------------------------------------------------- |
||
| 195 | | |
||
| 196 | | Load up the details ready to parse a response |
||
| 197 | | |
||
| 198 | */ |
||
| 199 | |||
| 200 | /** |
||
| 201 | * BaseResponse constructor. |
||
| 202 | * |
||
| 203 | * Saves the resource class to use, response, request and request options. |
||
| 204 | * Also saves the response body |
||
| 205 | * |
||
| 206 | * @param Response $response Response from Guzzle |
||
| 207 | * @param Request $request Request from Guzzle |
||
| 208 | * @param array $requestOptions Guzzle HTTP Options |
||
| 209 | * @param string $resourceClass Class that implements IResource |
||
| 210 | * |
||
| 211 | * @throws IncorrectResponseTypeException |
||
| 212 | */ |
||
| 213 | public function __construct($response, $request, $requestOptions, $resourceClass) |
||
| 214 | {
|
||
| 215 | if (!$response instanceof Response) |
||
| 216 | {
|
||
| 217 | throw new IncorrectResponseTypeException('Request class didn\'t pass a response type into the response', 500);
|
||
| 218 | } |
||
| 219 | if (!$request instanceof Request) |
||
| 220 | {
|
||
| 221 | throw new IncorrectResponseTypeException('Request class didn\'t pass a request type into the response', 500);
|
||
| 222 | } |
||
| 223 | |||
| 224 | $this->resourceClass = $resourceClass; |
||
| 225 | $this->response = $response; |
||
| 226 | $this->request = $request; |
||
| 227 | $this->requestOptions = $requestOptions; |
||
| 228 | $this->saveResponseBody(); |
||
| 229 | |||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Saves the response body |
||
| 234 | * |
||
| 235 | * Split the response body into data and meta, and save it |
||
| 236 | * as properties of this class |
||
| 237 | * |
||
| 238 | * @return void |
||
| 239 | */ |
||
| 240 | private function saveResponseBody() |
||
| 241 | {
|
||
| 242 | $body = $this->response->getBody()->getContents(); |
||
| 243 | if (is_string($body)) |
||
| 244 | {
|
||
| 245 | $body = json_decode($body, true); |
||
| 246 | } |
||
| 247 | $this->rawData = $body['data']; |
||
| 248 | $this->rawMeta = $body['meta']; |
||
| 249 | |||
| 250 | } |
||
| 251 | |||
| 252 | |||
| 253 | |||
| 254 | |||
| 255 | |||
| 256 | /* |
||
| 257 | |-------------------------------------------------------------------------- |
||
| 258 | | Parse a Response |
||
| 259 | |-------------------------------------------------------------------------- |
||
| 260 | | |
||
| 261 | | Execute the parsing of a response |
||
| 262 | | |
||
| 263 | */ |
||
| 264 | |||
| 265 | ########################### Controller ############################ |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Entry into instructing the Response to parse the data |
||
| 269 | * |
||
| 270 | * @return void |
||
| 271 | */ |
||
| 272 | protected function parseResponse() |
||
| 273 | {
|
||
| 274 | // Process MetaData |
||
| 275 | $this->processMetadata(); |
||
| 276 | |||
| 277 | // Unfortunately, UnionCloud often returns 200 when an error occured (such as the requested resource wasn't found) |
||
| 278 | // We can look at the body of the response to extract any errors |
||
| 279 | $this->throwIfErrors(); |
||
| 280 | |||
| 281 | $this->resources = $this->parseResources(); |
||
| 282 | } |
||
| 283 | |||
| 284 | ########################### Parse the raw metadata ############################ |
||
| 285 | /** |
||
| 286 | * Process the metadata and save it. |
||
| 287 | */ |
||
| 288 | private function processMetadata() |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Gets a header from the Guzzle Response |
||
| 309 | * |
||
| 310 | * @param string $headerName |
||
| 311 | * |
||
| 312 | * @return mixed |
||
| 313 | */ |
||
| 314 | private function getHeaderFromResponse($headerName) |
||
| 315 | {
|
||
| 316 | return $this->response->getHeader($headerName)[0]; |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Check if the response is able to use the pagination functions |
||
| 321 | * |
||
| 322 | * @return bool |
||
| 323 | */ |
||
| 324 | public function responseContainsPagination() |
||
| 325 | {
|
||
| 326 | if ( |
||
| 327 | array_key_exists('records_per_page', $this->response->getHeaders())
|
||
| 328 | && |
||
| 329 | array_key_exists('total_records', $this->response->getHeaders())
|
||
| 330 | && |
||
| 331 | array_key_exists('total_pages', $this->response->getHeaders())
|
||
| 332 | ) |
||
| 333 | {
|
||
| 334 | return true; |
||
| 335 | } |
||
| 336 | return false; |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Get the raw meta data (i.e. the Summary) |
||
| 341 | * |
||
| 342 | * @return mixed |
||
| 343 | */ |
||
| 344 | public function getRawMeta() |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Throw errors if there were some from the response |
||
| 351 | * |
||
| 352 | * Guzzle catches any errors documented by the status code. Therefore, |
||
| 353 | * the only place to look for errors is in the response body. |
||
| 354 | * |
||
| 355 | * Since all API calls may return different errors, the rules defined |
||
| 356 | * below may not be universal. If you spot an error, creating a pull |
||
| 357 | * request with a fix would be hugely appreciated! |
||
| 358 | * |
||
| 359 | * Similarly, not all errors may be documented. If you spot an error from |
||
| 360 | * UnionCloud that slips through, a pull request would be awesome. |
||
| 361 | * |
||
| 362 | * @throws ResourceNotFoundException |
||
| 363 | * @throws BaseUnionCloudException |
||
| 364 | */ |
||
| 365 | private function throwIfErrors() |
||
| 366 | {
|
||
| 367 | |||
| 368 | // If no data was returned, no resource was found |
||
| 369 | if($this->getRawData() === null) |
||
| 370 | {
|
||
| 371 | throw new ResourceNotFoundException('The resource wasn\'t found', 404);
|
||
| 372 | } |
||
| 373 | |||
| 374 | $responseBody = (array_key_exists(0, $this->getRawData())?$this->getRawData()[0]:$this->getRawData()); |
||
| 375 | |||
| 376 | // Process the standard error code response. |
||
| 377 | |||
| 378 | if(array_key_exists('errors', $responseBody) || array_key_exists('error', $responseBody))
|
||
| 379 | {
|
||
| 380 | $errors = []; |
||
| 381 | // Get the errors |
||
| 382 | if(array_key_exists('errors', $responseBody))
|
||
| 383 | {
|
||
| 384 | $errors = $responseBody['errors']; |
||
| 385 | } elseif(array_key_exists('error', $responseBody))
|
||
| 386 | {
|
||
| 387 | $errors = $responseBody['error']; |
||
| 388 | } |
||
| 389 | |||
| 390 | // Standardize the errors. If the errors aren't in an enclosing array, put them in one so we can iterate through them |
||
| 391 | if(! array_key_exists(0, $errors)) |
||
| 392 | {
|
||
| 393 | $errors = [$errors]; |
||
| 394 | } |
||
| 395 | |||
| 396 | // Throw the error |
||
| 397 | // TODO allow all errors to be seen by the user |
||
| 398 | foreach($errors as $error) |
||
| 399 | {
|
||
| 400 | if(array_key_exists('error_code', $error) && array_key_exists('error_message', $error))
|
||
| 401 | {
|
||
| 402 | $this->throwCustomUnionCloudError($error); |
||
| 403 | } |
||
| 404 | } |
||
| 405 | |||
| 406 | |||
| 407 | // Default throw if error_code or error_message is missing |
||
| 408 | throw new BaseUnionCloudException('Couldn\'t parse the UnionCloud response: '.json_encode($this->getRawData()), 500);
|
||
| 409 | |||
| 410 | } |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Throws an error depending on the error returned from UnionCloud |
||
| 415 | * |
||
| 416 | * If the error code wasn't understood, throws a generic error with the |
||
| 417 | * raw information from UnionCloud |
||
| 418 | * |
||
| 419 | * @param array $error single error from UnionCloud |
||
| 420 | * |
||
| 421 | * @throws BaseUnionCloudException |
||
| 422 | */ |
||
| 423 | private function throwCustomUnionCloudError($error) |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Gets specific details about an error given the error code |
||
| 438 | * |
||
| 439 | * @param string $errorCode |
||
| 440 | * |
||
| 441 | * @return array|bool false if no error documented, or |
||
| 442 | * ['errorClass'=>'..Exception::class', 'code'=>'HTTP code', 'message' => 'Error Message' |
||
| 443 | */ |
||
| 444 | private function getUnionCloudErrorDetails($errorCode) |
||
| 775 | } |
||
| 776 | /** |
||
| 777 | * Interface to the resource. |
||
| 778 | * |
||
| 779 | * Will return a collection of all the resources populated with data |
||
| 780 | * |
||
| 781 | * @return ResourceCollection |
||
| 782 | */ |
||
| 783 | private function parseResources() |
||
| 784 | {
|
||
| 785 | $resources = new ResourceCollection(); |
||
| 786 | // TODO This will throw an error if data wasn't returned in an array |
||
| 787 | |||
| 788 | try {
|
||
| 789 | foreach ($this->getRawData() as $resource) {
|
||
| 790 | $parsedResource = $this->parseResource($resource); |
||
| 791 | $resources->addResource($parsedResource); |
||
| 792 | } |
||
| 793 | } catch(\Exception $e) {
|
||
| 794 | // See the above TODO |
||
| 795 | } |
||
| 796 | return $resources; |
||
| 797 | } |
||
| 798 | |||
| 799 | /** |
||
| 800 | * Parse a single resource, given an array of parameters |
||
| 801 | * |
||
| 802 | * @param $resource |
||
| 803 | * |
||
| 804 | * @return mixed |
||
| 805 | */ |
||
| 806 | private function parseResource($resource) |
||
| 812 | |||
| 813 | } |
||
| 814 | |||
| 815 | |||
| 816 | |||
| 817 | |||
| 818 | |||
| 819 | |||
| 820 | |||
| 821 | |||
| 822 | |||
| 823 | |||
| 824 | |||
| 825 | |||
| 826 | |||
| 827 | |||
| 828 | /* |
||
| 829 | |-------------------------------------------------------------------------- |
||
| 830 | | Get Details |
||
| 831 | |-------------------------------------------------------------------------- |
||
| 832 | | |
||
| 833 | | Execute the parsing of a response |
||
| 834 | | |
||
| 835 | */ |
||
| 836 | |||
| 837 | |||
| 838 | /** |
||
| 839 | * Get the date returned by the server |
||
| 840 | * |
||
| 841 | * @return Carbon |
||
| 842 | */ |
||
| 843 | public function getResponseDate() |
||
| 844 | {
|
||
| 845 | return $this->responseDate; |
||
| 846 | } |
||
| 847 | |||
| 848 | /** |
||
| 849 | * Get the time taken for the server to complete the task |
||
| 850 | * |
||
| 851 | * @return float |
||
| 852 | */ |
||
| 853 | public function getRunTime() |
||
| 854 | {
|
||
| 855 | return $this->runTime; |
||
| 856 | } |
||
| 857 | |||
| 858 | /** |
||
| 859 | * Get the X-Request-ID from the response |
||
| 860 | * |
||
| 861 | * @return string |
||
| 862 | */ |
||
| 863 | public function getXRequestID() |
||
| 864 | {
|
||
| 865 | return $this->xRequestID; |
||
| 866 | } |
||
| 867 | |||
| 868 | /** |
||
| 869 | * Get the Status Code of the request |
||
| 870 | * |
||
| 871 | * @return int |
||
| 872 | */ |
||
| 873 | public function getStatusCode() |
||
| 874 | {
|
||
| 875 | return $this->statusCode; |
||
| 876 | } |
||
| 877 | |||
| 878 | /** |
||
| 879 | * Get the data from the API call |
||
| 880 | * |
||
| 881 | * @return array |
||
| 882 | */ |
||
| 883 | public function getRawData() |
||
| 884 | {
|
||
| 885 | return $this->rawData; |
||
| 886 | } |
||
| 887 | |||
| 888 | |||
| 889 | /** |
||
| 890 | * Get the request |
||
| 891 | * |
||
| 892 | * @return Request |
||
| 893 | */ |
||
| 894 | public function getRawRequest() |
||
| 895 | {
|
||
| 896 | return $this->request; |
||
| 897 | } |
||
| 898 | |||
| 899 | /** |
||
| 900 | * Get the response |
||
| 901 | * |
||
| 902 | * @return Response |
||
| 903 | */ |
||
| 904 | public function getRawResponse() |
||
| 907 | } |
||
| 908 | |||
| 909 | /** |
||
| 910 | * Get the request options passed to Guzzle HTTP |
||
| 911 | * |
||
| 912 | * @return array |
||
| 913 | */ |
||
| 914 | public function getRequestOptions() |
||
| 915 | {
|
||
| 916 | return $this->requestOptions; |
||
| 917 | } |
||
| 918 | |||
| 919 | /** |
||
| 920 | * Get the total number of pages for a pagination request |
||
| 921 | * |
||
| 922 | * @return int |
||
| 923 | */ |
||
| 924 | public function getTotalPages() |
||
| 927 | } |
||
| 928 | |||
| 929 | |||
| 930 | |||
| 931 | |||
| 932 | |||
| 933 | |||
| 934 | |||
| 935 | |||
| 936 | |||
| 937 | |||
| 938 | |||
| 939 | |||
| 940 | |||
| 941 | |||
| 942 | /* |
||
| 943 | |-------------------------------------------------------------------------- |
||
| 944 | | Returning Resources |
||
| 945 | |-------------------------------------------------------------------------- |
||
| 946 | | |
||
| 947 | | Functions to return resources |
||
| 948 | | |
||
| 949 | */ |
||
| 950 | |||
| 951 | |||
| 952 | /** |
||
| 953 | * Remove options if debug isn't on |
||
| 954 | */ |
||
| 955 | public function removeDebugOptions() |
||
| 962 | } |
||
| 963 | |||
| 964 | /** |
||
| 965 | * Get the returned resources |
||
| 966 | * |
||
| 967 | * @return ResourceCollection |
||
| 968 | */ |
||
| 969 | public function get() |
||
| 974 | } |