| Conditions | 6 |
| Paths | 6 |
| Total Lines | 66 |
| Code Lines | 44 |
| 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 |
||
| 33 | public function __invoke(ServerRequestInterface $request): ResponseInterface |
||
| 34 | { |
||
| 35 | if (!\in_array($request->getMethod(), ['GET', 'HEAD', 'OPTIONS'])) { |
||
| 36 | return new Response(405); |
||
| 37 | } |
||
| 38 | |||
| 39 | $path = $request->getUri()->getPath(); |
||
| 40 | $filters = \explode('/', \trim(\strtr($path, [self::PATH => '']), '/'), 2); |
||
| 41 | $topic = nullify($filters[0]); |
||
| 42 | $subscriber = nullify($filters[1] ?? null); |
||
| 43 | |||
| 44 | $token = $this->authenticator->authenticate($request); |
||
| 45 | |||
| 46 | if (null === $token) { |
||
| 47 | throw new AccessDeniedHttpException('You must be authenticated to access the Subscription API.'); |
||
| 48 | } |
||
| 49 | |||
| 50 | $claim = (array) $token->getClaim('mercure'); |
||
| 51 | $allowedTopics = $claim['subscribe'] ?? []; |
||
| 52 | $deniedTopics = $claim['subscribe_exclude'] ?? []; |
||
| 53 | $topicSelector = $path === self::PATH ? self::TOPIC_SELECTOR : $path; |
||
| 54 | $matchAllowedTopics = TopicMatcher::matchesTopicSelectors($topicSelector, $allowedTopics); |
||
| 55 | $matchDeniedTopics = TopicMatcher::matchesTopicSelectors($topicSelector, $deniedTopics); |
||
| 56 | if (!$matchAllowedTopics || $matchDeniedTopics) { |
||
| 57 | throw new AccessDeniedHttpException('You are not authorized to display these subscriptions.'); |
||
| 58 | } |
||
| 59 | |||
| 60 | $stream = new ThroughStream(); |
||
| 61 | $this->hub->hook( |
||
| 62 | function () use ($stream, $path, $subscriber, $topic, $allowedTopics, $deniedTopics) { |
||
| 63 | $this->hub->getLastEventID()->then( |
||
| 64 | function (?string $lastEventId) use ( |
||
| 65 | $topic, |
||
| 66 | $subscriber, |
||
| 67 | $stream, |
||
| 68 | $path, |
||
| 69 | $allowedTopics, |
||
| 70 | $deniedTopics |
||
| 71 | ) { |
||
| 72 | $this->hub->getActiveSubscriptions($topic, $subscriber) |
||
| 73 | ->then( |
||
| 74 | function (iterable $subscriptions) use ( |
||
| 75 | $stream, |
||
| 76 | $path, |
||
| 77 | $allowedTopics, |
||
| 78 | $deniedTopics, |
||
| 79 | $lastEventId |
||
| 80 | ) { |
||
| 81 | $subscriptions = $this->filterSubscriptions( |
||
| 82 | $subscriptions, |
||
| 83 | $allowedTopics, |
||
| 84 | $deniedTopics |
||
| 85 | ); |
||
| 86 | $this->sendResult($path, $lastEventId, $subscriptions, $stream); |
||
| 87 | } |
||
| 88 | ); |
||
| 89 | } |
||
| 90 | ); |
||
| 91 | } |
||
| 92 | ); |
||
| 93 | |||
| 94 | $headers = [ |
||
| 95 | 'Content-Type' => 'application/ld+json', |
||
| 96 | ]; |
||
| 97 | |||
| 98 | return new Response(200, $headers, $stream); |
||
| 99 | } |
||
| 146 |