Completed
Push — master ( 7c9eb0...a92430 )
by Thomas
13s
created

WatchRoutingDefinitionsAction   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 56
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getOptionsClass() 0 3 1
A getSubscribedEvents() 0 4 1
A process() 0 20 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the ekino Drupal Debug project.
7
 *
8
 * (c) ekino
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Ekino\Drupal\Debug\Action\WatchRoutingDefinitions;
15
16
use Drupal\Core\Routing\RouteBuilderInterface;
17
use Ekino\Drupal\Debug\Action\ActionWithOptionsInterface;
18
use Ekino\Drupal\Debug\Action\EventSubscriberActionInterface;
19
use Ekino\Drupal\Debug\Exception\NotSupportedException;
20
use Ekino\Drupal\Debug\Kernel\Event\AfterRequestPreHandleEvent;
21
use Ekino\Drupal\Debug\Kernel\Event\DebugKernelEvents;
22
use Ekino\Drupal\Debug\Resource\ResourcesFreshnessChecker;
23
24
class WatchRoutingDefinitionsAction implements EventSubscriberActionInterface, ActionWithOptionsInterface
25
{
26
    /**
27
     * @var WatchRoutingDefinitionsOptions
28
     */
29
    private $options;
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 1
    public static function getSubscribedEvents(): array
35
    {
36
        return array(
37 1
            DebugKernelEvents::AFTER_REQUEST_PRE_HANDLE => 'process',
38
        );
39
    }
40
41
    /**
42
     * @param WatchRoutingDefinitionsOptions $options
43
     */
44 8
    public function __construct(WatchRoutingDefinitionsOptions $options)
45
    {
46 8
        $this->options = $options;
47 8
    }
48
49
    /**
50
     * @param AfterRequestPreHandleEvent $event
51
     */
52 4
    public function process(AfterRequestPreHandleEvent $event): void
53
    {
54 4
        $resourcesFreshnessChecker = new ResourcesFreshnessChecker($this->options->getCacheFilePath(), $this->options->getFilteredResourcesCollection($event->getEnabledModules(), $event->getEnabledThemes()));
55 4
        if ($resourcesFreshnessChecker->isFresh()) {
56 2
            return;
57
        }
58
59 3
        $container = $event->getContainer();
60 3
        if (!$container->has('router.builder')) {
61 1
            throw new NotSupportedException('The "router.builder" service should already be set in the container.');
62
        }
63
64 2
        $routerBuilder = $container->get('router.builder');
65 2
        if (!$routerBuilder instanceof RouteBuilderInterface) {
66 1
            throw new NotSupportedException(\sprintf('The "router.builder" service class should implement the "%s" interface.', RouteBuilderInterface::class));
67
        }
68
69 1
        $routerBuilder->rebuild();
70
71 1
        $resourcesFreshnessChecker->commit();
72 1
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 3
    public static function getOptionsClass(): string
78
    {
79 3
        return WatchRoutingDefinitionsOptions::class;
80
    }
81
}
82