Conditions | 19 |
Paths | 258 |
Total Lines | 99 |
Code Lines | 55 |
Lines | 0 |
Ratio | 0 % |
Changes | 9 | ||
Bugs | 1 | 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 |
||
60 | public function getResponse(string $model, string $calledMethod, ?array $methodProperties, bool $auth = true): array |
||
61 | { |
||
62 | $url = $this->url.'/'.$model.'/'.$calledMethod; |
||
63 | $body = []; |
||
64 | $info = ''; |
||
65 | |||
66 | $body['modelName'] = $model; |
||
67 | $body['calledMethod'] = $calledMethod; |
||
68 | $body['methodProperties'] = $methodProperties; |
||
69 | |||
70 | if ($auth) { |
||
71 | $body['apiKey'] = $this->api; |
||
72 | } |
||
73 | |||
74 | $response = Http::timeout(config('novaposhta.http_response_timeout', 3)) |
||
75 | ->retry(config('novaposhta.http_retry_max_time', 2), config('novaposhta.http_retry_delay', 200)) |
||
76 | ->withHeaders([ |
||
77 | 'Accept' => 'application/json', |
||
78 | 'Content-Type' => 'application/json', |
||
79 | ]) |
||
80 | ->post($url, $body); |
||
81 | |||
82 | if ($response->failed()) { |
||
83 | return [ |
||
84 | 'success' => false, |
||
85 | 'result' => null, |
||
86 | 'info' => trans('novaposhta::novaposhta.error_data'), |
||
87 | ]; |
||
88 | } |
||
89 | |||
90 | $answer = $response->json(); |
||
91 | if ($auth === false && isset($answer[0])) { |
||
92 | /** |
||
93 | * Костыль для Новой Почты. |
||
94 | * Спасибо Вам большое, что нормально не выдаете ответ :). |
||
95 | */ |
||
96 | $answer = $answer[0]; |
||
97 | } |
||
98 | |||
99 | if (! isset($answer['success']) || ! isset($answer['data']) || empty($answer['data'])) { |
||
100 | /** |
||
101 | * Что-то не так в ответе. |
||
102 | */ |
||
103 | $info = trans('novaposhta::novaposhta.error_answer'); |
||
|
|||
104 | $success = false; |
||
105 | $result = null; |
||
106 | } else { |
||
107 | $success = $answer['success']; |
||
108 | $result = $answer['data']; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Ошибки, либо уведомления. |
||
113 | */ |
||
114 | $info = []; |
||
115 | if (isset($answer['warnings']) && $answer['warnings']) { |
||
116 | $info['warnings'] = $answer['warnings']; |
||
117 | |||
118 | if ($answer['errors']) { |
||
119 | $info['errors'] = $answer['errors']; |
||
120 | if ($answer['errorCodes']) { |
||
121 | foreach ($answer['errorCodes'] as $err) { |
||
122 | $info['StatusCode'] = $err; |
||
123 | $info['StatusLocale'] = __('novaposhta::novaposhta.statusCode.'.$err); |
||
124 | } |
||
125 | } |
||
126 | } |
||
127 | } |
||
128 | |||
129 | if (! $info && isset($answer['info']) && $answer['info']) { |
||
130 | $info['info'] = $answer['info']; |
||
131 | } |
||
132 | |||
133 | $return = [ |
||
134 | 'success' => $success, |
||
135 | 'result' => $result, |
||
136 | 'info' => $info, |
||
137 | ]; |
||
138 | |||
139 | if ($this->dev) { |
||
140 | /** |
||
141 | * Test and Dev. |
||
142 | */ |
||
143 | Log::debug('= = = = = = = = = = = = = = = = = = = ='); |
||
144 | Log::debug($model.' / '.$calledMethod.' // apiKey: '.$auth); |
||
145 | Log::debug('--------------------'); |
||
146 | |||
147 | if ($methodProperties) { |
||
148 | try { |
||
149 | Log::notice(json_encode($methodProperties)); |
||
150 | } catch (Exception $e) { |
||
151 | Log::notice('method json_encode error'); |
||
152 | } |
||
153 | } |
||
154 | |||
155 | $return['dev'] = $answer; |
||
156 | } |
||
157 | |||
158 | return $return; |
||
159 | } |
||
161 |