Completed
Push — master ( 139fb8...7c9eb0 )
by Thomas
10s
created

WatchRoutingDefinitionsAction::process()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 1
dl 0
loc 20
ccs 0
cts 12
cp 0
crap 20
rs 9.9
c 0
b 0
f 0
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
    public static function getSubscribedEvents(): array
35
    {
36
        return array(
37
            DebugKernelEvents::AFTER_REQUEST_PRE_HANDLE => 'process',
38
        );
39
    }
40
41
    /**
42
     * @param WatchRoutingDefinitionsOptions $options
43
     */
44 2
    public function __construct(WatchRoutingDefinitionsOptions $options)
45
    {
46 2
        $this->options = $options;
47 2
    }
48
49
    /**
50
     * @param AfterRequestPreHandleEvent $event
51
     */
52
    public function process(AfterRequestPreHandleEvent $event): void
53
    {
54
        $resourcesFreshnessChecker = new ResourcesFreshnessChecker($this->options->getCacheFilePath(), $this->options->getFilteredResourcesCollection($event->getEnabledModules(), $event->getEnabledThemes()));
55
        if ($resourcesFreshnessChecker->isFresh()) {
56
            return;
57
        }
58
59
        $container = $event->getContainer();
60
        if (!$container->has('router.builder')) {
61
            throw new NotSupportedException('The "router.builder" service should already be set in the container.');
62
        }
63
64
        $routerBuilder = $container->get('router.builder');
65
        if (!$routerBuilder instanceof RouteBuilderInterface) {
66
            throw new NotSupportedException(\sprintf('The "router.builder" service class should implement the "%s" interface', RouteBuilderInterface::class));
67
        }
68
69
        $routerBuilder->rebuild();
70
71
        $resourcesFreshnessChecker->commit();
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 2
    public static function getOptionsClass(): string
78
    {
79 2
        return WatchRoutingDefinitionsOptions::class;
80
    }
81
}
82