| Conditions | 16 |
| Paths | 73 |
| Total Lines | 82 |
| Code Lines | 50 |
| 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 |
||
| 90 | public function login(?FormInterface $form = null) |
||
| 91 | { |
||
| 92 | if (!$form) { |
||
| 93 | throw new \RuntimeException('This provider require a form'); |
||
| 94 | } |
||
| 95 | |||
| 96 | $url = $this->config['login']['url'] ?? null; |
||
| 97 | if ($this->router->getRouteCollection()->get($url)) { |
||
| 98 | $url = $this->router->generate($url, [], Router::ABSOLUTE_URL); |
||
| 99 | } |
||
| 100 | |||
| 101 | $opts = [ |
||
| 102 | CURLOPT_CONNECTTIMEOUT => 10, |
||
| 103 | CURLOPT_RETURNTRANSFER => true, |
||
| 104 | CURLOPT_HEADER => true, |
||
| 105 | CURLOPT_TIMEOUT => 60, |
||
| 106 | CURLOPT_HTTPHEADER => [], |
||
| 107 | ]; |
||
| 108 | |||
| 109 | $paramsIn = $this->config['login']['parameters_in'] ?? 'form'; |
||
| 110 | if ('header' === $paramsIn) { |
||
| 111 | $opts[CURLOPT_HEADER] = true; |
||
| 112 | $headers = []; |
||
| 113 | foreach ($form->getData() as $key => $value) { |
||
| 114 | $headers[] = sprintf('%s: %s', $key, $value); |
||
| 115 | } |
||
| 116 | $opts[CURLOPT_HTTPHEADER] = array_merge(["Accept: application/json"], $headers); |
||
| 117 | } elseif ('query' === $paramsIn) { |
||
| 118 | $url .= '?'.http_build_query($form->getData(), null, '&'); |
||
| 119 | } else { |
||
| 120 | $opts[CURLOPT_POSTFIELDS] = http_build_query($form->getData(), null, '&'); |
||
| 121 | $opts[CURLOPT_POST] = true; |
||
| 122 | } |
||
| 123 | |||
| 124 | $opts[CURLOPT_URL] = $url; |
||
| 125 | |||
| 126 | $ch = curl_init(); |
||
| 127 | curl_setopt_array($ch, $opts); |
||
| 128 | $result = curl_exec($ch); |
||
| 129 | |||
| 130 | // Split the HTTP response into header and body. |
||
| 131 | try { |
||
| 132 | list($headers, $body) = explode("\r\n\r\n", $result); |
||
| 133 | $headers = explode("\r\n", $headers); |
||
| 134 | } catch (\ErrorException $exception) { |
||
| 135 | throw new AuthenticationFailedException('Authentication Failed'); |
||
| 136 | } |
||
| 137 | |||
| 138 | // We catch HTTP/1.1 4xx or HTTP/1.1 5xx error response. |
||
| 139 | if (strpos($headers[0], 'HTTP/1.1 4') !== false || strpos($headers[0], 'HTTP/1.1 5') !== false) { |
||
| 140 | $result = [ |
||
| 141 | 'code' => 0, |
||
| 142 | 'message' => '', |
||
| 143 | ]; |
||
| 144 | |||
| 145 | if (preg_match('/^HTTP\/1.1 ([0-9]{3,3}) (.*)$/', $headers[0], $matches)) { |
||
| 146 | $result['code'] = $matches[1]; |
||
| 147 | $result['message'] = $matches[2]; |
||
| 148 | } |
||
| 149 | |||
| 150 | // In case retrun with WWW-Authenticate replace the description. |
||
| 151 | foreach ($headers as $header) { |
||
| 152 | if (preg_match("/^WWW-Authenticate:.*error='(.*)'/", $header, $matches)) { |
||
| 153 | $result['message'] = $matches[1]; |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | throw new AuthenticationFailedException($result['message'] ?: 'Authentication Failed', $result['code'] ?: 401); |
||
| 158 | } |
||
| 159 | |||
| 160 | $tokenPath = $this->config['login']['response_token_path'] ?? 'token'; |
||
| 161 | $token = $body; |
||
| 162 | |||
| 163 | if ($tokenPath) { |
||
| 164 | $token = search($tokenPath, @json_decode($body)); |
||
| 165 | } |
||
| 166 | |||
| 167 | if (!$token) { |
||
| 168 | throw new AuthenticationFailedException('Authentication Failed'); |
||
| 169 | } |
||
| 170 | |||
| 171 | $this->session->set(self::SESSION_PATH, $token); |
||
| 172 | } |
||
| 211 |