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