Conditions | 9 |
Paths | 50 |
Total Lines | 51 |
Code Lines | 32 |
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 |
||
70 | private function query($name, $requestMethod = 'GET', $parameters = []) |
||
71 | { |
||
72 | $this->log()->info("Starting to perform query - resource: {$name} method: {$requestMethod}".(empty($parameters ?: " parameters: ".implode($parameters)))); |
||
73 | |||
74 | $client = $this->buildClient(); |
||
75 | $options = $this->buildRequest($requestMethod)->options($parameters); |
||
76 | $url = $this->buildRequest()->URL($name); |
||
77 | |||
78 | try { |
||
79 | $request = $client->request($requestMethod, $url, $options); |
||
80 | $header = $request->getHeaders(); |
||
81 | if ($request->getStatusCode() >= 200 and $request->getStatusCode() <= 299) { |
||
82 | |||
83 | $this->log()->info('Received '.$request->getStatusCode()); |
||
84 | |||
85 | // Build object to return |
||
86 | $result = []; |
||
87 | |||
88 | // Include pagination details |
||
89 | if ($header['Pagination'] !== null) { |
||
90 | $result['pagination'] = json_decode($header['Pagination'][0], true); |
||
91 | } |
||
92 | |||
93 | if (!empty($parameters)) { |
||
94 | $result['parameters'] = $parameters; |
||
95 | } |
||
96 | $result['data'] = json_decode($request->getBody()->getContents(), true); |
||
97 | |||
98 | return $result; |
||
99 | } else { |
||
100 | // TODO - Error Handling |
||
101 | $this->log()->error('An error occurred, received a '.$request->getStatusCode()); |
||
102 | $this->log()->transferLogs(); |
||
103 | $this->throwRunTimeException('An error occurred, received a '.$request->getStatusCode()); |
||
104 | return null; |
||
105 | } |
||
106 | } |
||
107 | catch (\Exception $exception) { |
||
108 | if ($exception instanceof ClientException) { |
||
109 | if ($exception->getCode() == 404) { |
||
110 | $this->log()->info('Received '.$exception->getCode()); |
||
111 | $result['error']['code'] = 404; |
||
112 | $result['error']['message'] = "No Results Found"; |
||
113 | return $result; |
||
114 | } |
||
115 | } |
||
116 | // TODO - Error Handling |
||
117 | $this->log()->error('An error occurred, received a '.$exception->getCode().' '.$exception->getMessage()); |
||
118 | $this->log()->transferLogs(); |
||
119 | $this->throwRunTimeException('An error occurred, received a '.$exception->getCode()); |
||
120 | return null; |
||
121 | } |
||
174 | } |