GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

DFToolsServicesSubscriber   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 26
dl 0
loc 62
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 5 1
A allowCORSResponse() 0 14 3
A allowOPTIONSRoute() 0 9 3
A setCORSHeaders() 0 5 1
1
<?php
2
namespace Drupal\df_tools_services\EventSubscriber;
3
4
use Drupal\Core\Routing\RouteBuildEvent;
5
use Drupal\Core\Routing\RoutingEvents;
6
use Symfony\Component\HttpFoundation\Response;
7
use Symfony\Component\HttpKernel\KernelEvents;
8
use Symfony\Component\HttpKernel\Event\ResponseEvent;
9
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10
11
class DFToolsServicesSubscriber implements EventSubscriberInterface {
12
13
  /**
14
   * Forces CORS enablement for all responses.
15
   *
16
   * @param ResponseEvent $event
17
   */
18
  public function allowCORSResponse(ResponseEvent $event) {
19
    $response = $event->getResponse();
20
    $request = $event->getRequest();
21
22
    // Forcibly allow CORS for the purposes of the headless demo.
23
    if ($request->getMethod() == 'OPTIONS') {
24
      $response = new Response();
25
      $this->setCORSHeaders($response);
26
      $response->send();
27
      exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
28
    }
29
30
    if ($response) {
0 ignored issues
show
introduced by
$response is of type Symfony\Component\HttpFoundation\Response, thus it always evaluated to true.
Loading history...
31
      $this->setCORSHeaders($response);
32
    }
33
  }
34
35
  /**
36
   * Allows OPTIONS as an acceptable method for all Drupal routes.
37
   *
38
   * @param RouteBuildEvent $event
39
   */
40
  public function allowOPTIONSRoute(RouteBuildEvent $event) {
41
    $collection = $event->getRouteCollection();
42
    $routes = $collection->all();
43
    foreach ($routes as $name => $route) {
44
      $methods = $route->getMethods();
45
      if (!in_array('OPTIONS', $methods)) {
46
        $methods[] = 'OPTIONS';
47
        $route->setMethods($methods);
48
        $collection->add($name, $route);
49
      }
50
    }
51
  }
52
53
  /**
54
   * Sets headers required to enable CORS.
55
   *
56
   * @param Response $response
57
   */
58
  protected function setCORSHeaders($response) {
59
    $response->headers->set('Access-Control-Allow-Origin', '*');
60
    $response->headers->set('Access-Control-Allow-Methods', 'GET,POST,PUT,PATCH,DELETE,OPTIONS');
61
    $response->headers->set('Access-Control-Allow-Headers', 'Authorization, Origin, Accept, Content-Type, X-CSRF-Token');
62
    $response->headers->set('Access-Control-Allow-Credentials', 'true');
63
  }
64
65
  /**
66
   * {@inheritdoc}
67
   */
68
  public static function getSubscribedEvents() {
69
    $events = [];
70
    $events[KernelEvents::RESPONSE][] = ['allowCORSResponse'];
71
    $events[RoutingEvents::ALTER][] = ['allowOPTIONSRoute'];
72
    return $events;
73
  }
74
75
}
76