| Conditions | 5 |
| Paths | 4 |
| Total Lines | 51 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 29 | public function __invoke(ServerRequestInterface $request): ResponseInterface |
||
| 30 | { |
||
| 31 | if (!\in_array($request->getMethod(), ['GET', 'HEAD', 'OPTIONS'])) { |
||
| 32 | return new Response(405); |
||
| 33 | } |
||
| 34 | |||
| 35 | $path = $request->getUri()->getPath(); |
||
| 36 | $filters = \explode('/', \trim(\strtr($path, [self::PATH => '']), '/'), 2); |
||
| 37 | $subscriber = nullify($filters[0]); |
||
| 38 | $topic = nullify($filters[1] ?? null); |
||
| 39 | |||
| 40 | $token = $this->authenticator->authenticate($request); |
||
| 41 | |||
| 42 | if (null === $token) { |
||
| 43 | throw new AccessDeniedHttpException('You must be authenticated to access the Subscription API.'); |
||
| 44 | } |
||
| 45 | |||
| 46 | $claim = (array) $token->getClaim('mercure'); |
||
| 47 | $allowedTopics = $claim['subscribe'] ?? []; |
||
| 48 | $deniedTopics = $claim['subscribe_exclude'] ?? []; |
||
| 49 | $matchAllowedTopics = TopicMatcher::matchesTopicSelectors($path, $allowedTopics); |
||
| 50 | $matchDeniedTopics = TopicMatcher::matchesTopicSelectors($path, $deniedTopics); |
||
| 51 | if (!$matchAllowedTopics || $matchDeniedTopics) { |
||
| 52 | throw new AccessDeniedHttpException('You are not authorized to display these subscriptions.'); |
||
| 53 | } |
||
| 54 | |||
| 55 | $stream = new ThroughStream(); |
||
| 56 | $this->hub->hook( |
||
| 57 | function () use ($stream, $path, $subscriber, $topic) { |
||
| 58 | $this->hub->getActiveSubscriptions($subscriber, $topic) |
||
| 59 | ->then( |
||
| 60 | function (iterable $subscriptions) use ($stream, $path) { |
||
| 61 | $result = [ |
||
| 62 | '@context' => 'https://mercure.rocks/', |
||
| 63 | 'id' => $path, |
||
| 64 | 'type' => 'Subscriptions', |
||
| 65 | 'subscriptions' => \iterable_to_array($subscriptions), |
||
| 66 | ]; |
||
| 67 | $stream->write(\json_encode($result, \JSON_THROW_ON_ERROR)); |
||
| 68 | $stream->end(); |
||
| 69 | $stream->close(); |
||
| 70 | } |
||
| 71 | ); |
||
| 72 | } |
||
| 73 | ); |
||
| 74 | |||
| 75 | $headers = [ |
||
| 76 | 'Content-Type' => 'application/ld+json', |
||
| 77 | ]; |
||
| 78 | |||
| 79 | return new Response(200, $headers, $stream); |
||
| 80 | } |
||
| 87 |