| Conditions | 6 |
| Paths | 7 |
| Total Lines | 51 |
| Code Lines | 33 |
| 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 |
||
| 37 | public function getWebfinger(Request $request) |
||
| 38 | { |
||
| 39 | $resource = $request->getQueryParameter('resource'); |
||
| 40 | if (null === $resource) { |
||
| 41 | throw new HttpException('resource parameter missing', 400); |
||
| 42 | } |
||
| 43 | if (0 !== strpos($resource, 'acct:')) { |
||
| 44 | throw new HttpException('unsupported resource type', 400); |
||
| 45 | } |
||
| 46 | $userAddress = substr($resource, 5); |
||
| 47 | $atPos = strpos($userAddress, '@'); |
||
| 48 | if (false === $atPos) { |
||
| 49 | throw new HttpException('invalid user address', 400); |
||
| 50 | } |
||
| 51 | $user = substr($userAddress, 0, $atPos); |
||
| 52 | |||
| 53 | $webFingerData = [ |
||
| 54 | 'links' => [ |
||
| 55 | [ |
||
| 56 | 'href' => sprintf('%s%s', $request->getRootUri(), $user), |
||
| 57 | 'properties' => [ |
||
| 58 | 'http://remotestorage.io/spec/version' => 'draft-dejong-remotestorage-05', |
||
| 59 | 'http://remotestorage.io/spec/web-authoring' => null, |
||
| 60 | 'http://tools.ietf.org/html/rfc6749#section-4.2' => sprintf('%s_oauth/authorize?login_hint=%s', $request->getRootUri(), $user), |
||
| 61 | 'http://tools.ietf.org/html/rfc6750#section-2.3' => 'true', |
||
| 62 | 'http://tools.ietf.org/html/rfc7233' => 'development' !== $this->serverMode ? 'GET' : null, |
||
| 63 | ], |
||
| 64 | 'rel' => 'http://tools.ietf.org/id/draft-dejong-remotestorage', |
||
| 65 | ], |
||
| 66 | // legacy -03 WebFinger response |
||
| 67 | [ |
||
| 68 | 'href' => sprintf('%s%s', $request->getRootUri(), $user), |
||
| 69 | 'properties' => [ |
||
| 70 | 'http://remotestorage.io/spec/version' => 'draft-dejong-remotestorage-03', |
||
| 71 | 'http://tools.ietf.org/html/rfc2616#section-14.16' => 'development' !== $this->serverMode ? 'GET' : false, |
||
| 72 | 'http://tools.ietf.org/html/rfc6749#section-4.2' => sprintf('%s_oauth/authorize?login_hint=%s', $request->getRootUri(), $user), |
||
| 73 | 'http://tools.ietf.org/html/rfc6750#section-2.3' => true, |
||
| 74 | ], |
||
| 75 | 'rel' => 'remotestorage', |
||
| 76 | ], |
||
| 77 | ], |
||
| 78 | ]; |
||
| 79 | |||
| 80 | $response = new Response(200, 'application/jrd+json'); |
||
| 81 | $response->addHeader('Access-Control-Allow-Origin', '*'); |
||
| 82 | $response->setBody( |
||
| 83 | json_encode($webFingerData) |
||
| 84 | ); |
||
| 85 | |||
| 86 | return $response; |
||
| 87 | } |
||
| 88 | } |
||
| 89 |