RouteEventSubscriber::registerAnnotations()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Drupal\controller_annotations\EventSubscriber;
4
5
use Doctrine\Common\Annotations\AnnotationRegistry;
6
use Drupal\Core\Routing\RouteBuildEvent;
7
use Drupal\Core\Routing\RoutingEvents;
8
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader;
10
use Symfony\Component\Routing\Route;
11
12
class RouteEventSubscriber implements EventSubscriberInterface
13
{
14
    /**
15
     * @var AnnotationDirectoryLoader
16
     */
17
    private $annotationDirectoryLoader;
18
19
    /**
20
     * @var string
21
     */
22
    private $rootPath;
23
24
    /**
25
     * @param AnnotationDirectoryLoader $annotationDirectoryLoader
26
     * @param string $rootPath
27
     */
28 11
    public function __construct(AnnotationDirectoryLoader $annotationDirectoryLoader, string $rootPath)
29
    {
30 11
        $this->registerAnnotations();
31 11
        $this->annotationDirectoryLoader = $annotationDirectoryLoader;
32 11
        $this->rootPath = $rootPath;
33 11
    }
34
35
    /**
36
     * Configure the annotation registry to make routing annotations available
37
     */
38 11
    private function registerAnnotations()
39
    {
40 11
        AnnotationRegistry::registerLoader('class_exists');
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\Common\Annotati...istry::registerLoader() has been deprecated with message: this method is deprecated and will be removed in doctrine/annotations 2.0 autoloading should be deferred to the globally registered autoloader by then. For now, use @example AnnotationRegistry::registerLoader('class_exists')

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
41 11
    }
42
43
    /**
44
     * @param RouteBuildEvent $event
45
     * @throws \Exception
46
     */
47 11
    public function onRoutes(RouteBuildEvent $event)
48
    {
49
        /**
50
         * @var $route Route
51
         */
52 11
        foreach ($event->getRouteCollection() as $name => $route) {
53 10
            if ($route->hasOption('type')
54 10
                && $route->getOption('type') === 'annotation'
55
            ) {
56 9
                $routeCollection = $this->annotationDirectoryLoader->load($this->rootPath.$this->getRoutePath($route));
57 8
                $routeCollection->addPrefix($route->getPath());
58
59 8
                $event->getRouteCollection()->addCollection($routeCollection);
0 ignored issues
show
Documentation introduced by
$routeCollection is of type object<Symfony\Component\Routing\RouteCollection>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
60 9
                $event->getRouteCollection()->remove($name);
61
            }
62
        }
63 10
    }
64
65
    /**
66
     * @return array
67
     */
68 7
    public static function getSubscribedEvents()
69
    {
70
        return [
71 7
            RoutingEvents::DYNAMIC => [
72
                ['onRoutes', 0],
73
            ],
74
        ];
75
    }
76
77
    /**
78
     * @param Route $route
79
     * @return string
80
     * @throws \Exception
81
     */
82 9
    protected function getRoutePath(Route $route)
83
    {
84 9
        if ($route->hasOption('path')) {
85 1
            $path = $route->getOption('path');
86 8
        } elseif ($route->hasOption('module')) {
87 7
            $path = sprintf('/%s/src/Controller', drupal_get_path('module', $route->getOption('module')));
88
        } else {
89 1
            throw new \Exception(
90 1
                'Either the "resource" or "module"  option is required to load from annotations'
91
            );
92
        }
93
94 8
        return $path;
95
    }
96
}
97