| Conditions | 10 |
| Paths | 11 |
| Total Lines | 63 |
| Code Lines | 41 |
| 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 |
||
| 134 | private function loginToGateway($profile) |
||
| 135 | { |
||
| 136 | $result = $this->httpClient->send( |
||
| 137 | build_url(config('p-connector.profiles.'.$profile.'.auth.login_path', config('p-connector.auth.login_path', 'login')), $profile), |
||
| 138 | config('p-connector.profiles.'.$profile.'.auth.credentials', config('p-connector.auth.credentials', [])), |
||
| 139 | strtoupper(config('p-connector.profiles.'.$profile.'.auth.login_http_method', config('p-connector.auth.login_http_method', 'POST'))), |
||
| 140 | $profile, |
||
| 141 | false, |
||
| 142 | config('p-connector.profiles.'.$profile.'.auth.headers', config('p-connector.auth.headers', [])), |
||
| 143 | ); |
||
| 144 | |||
| 145 | if ($result['status'] && in_array($result['response']['status_code'], config('p-connector.profiles.'.$profile.'.auth.success_login_code', config('p-connector.auth.success_login_code', [])))) { |
||
| 146 | $token = _get(json_decode($result['response']['body']), explode('.', config('p-connector.profiles.'.$profile.'.auth.token_path', config('p-connector.auth.token_path', 'token'))), ''); |
||
| 147 | if ('string' !== gettype($token)) { |
||
| 148 | throw new InvalidArgumentException('The returned token is not of type string (type: "'.gettype($token).'").'); |
||
| 149 | } |
||
| 150 | |||
| 151 | if (config('p-connector.profiles.'.$profile.'.session')) { |
||
| 152 | $data = [ |
||
| 153 | 'gateway_profile' => $profile, |
||
| 154 | 'token' => $token, |
||
| 155 | ]; |
||
| 156 | |||
| 157 | session()->put(['p-connector.session_'.$profile => $data]); |
||
| 158 | |||
| 159 | session()->save(); |
||
| 160 | } elseif (config('p-connector.session')) { |
||
| 161 | $data = [ |
||
| 162 | 'gateway_profile' => $profile, |
||
| 163 | 'token' => $token, |
||
| 164 | ]; |
||
| 165 | |||
| 166 | if (session()->has(config('p-connector.session_name'))) { |
||
| 167 | $index = -1; |
||
| 168 | foreach (session()->get(config('p-connector.session_name')) as $key => $value) { |
||
| 169 | if ($value['gateway_profile'] === $profile) { |
||
| 170 | $index = $key; |
||
| 171 | break; |
||
| 172 | } |
||
| 173 | } |
||
| 174 | if ($index != -1) { |
||
| 175 | session()->get(config('p-connector.session_name'))[$index] = $token; |
||
| 176 | } else { |
||
| 177 | session()->push(config('p-connector.session_name'), $data); |
||
| 178 | } |
||
| 179 | } else { |
||
| 180 | session()->put([ |
||
| 181 | config('p-connector.session_name') => [$data], |
||
| 182 | ]); |
||
| 183 | } |
||
| 184 | |||
| 185 | session()->save(); |
||
| 186 | } else { |
||
| 187 | app('db')->table(config('p-connector.table', 'p_connector'))->updateOrInsert( |
||
| 188 | ['gateway_profile' => $profile], |
||
| 189 | ['token' => $token, 'updated_at' => date('Y-m-d H:i:s')] |
||
| 190 | ); |
||
| 191 | } |
||
| 192 | |||
| 193 | return $token; |
||
| 194 | } |
||
| 195 | |||
| 196 | return null; |
||
| 197 | } |
||
| 199 |