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

WatchHooksImplementationsAction   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 91.43%

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 89
ccs 32
cts 35
cp 0.9143
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getOptionsClass() 0 3 1
A setResources() 0 3 1
A __construct() 0 3 1
B process() 0 40 6
A getSubscribedEvents() 0 4 1
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\WatchHooksImplementations;
15
16
use Drupal\Core\Extension\ModuleHandler;
17
use Ekino\Drupal\Debug\Action\ActionWithOptionsInterface;
18
use Ekino\Drupal\Debug\Action\CompilerPassActionInterface;
19
use Ekino\Drupal\Debug\Action\EventSubscriberActionInterface;
20
use Ekino\Drupal\Debug\Cache\FileBackend;
21
use Ekino\Drupal\Debug\Cache\FileCache;
22
use Ekino\Drupal\Debug\Exception\NotSupportedException;
23
use Ekino\Drupal\Debug\Kernel\Event\AfterAttachSyntheticEvent;
24
use Ekino\Drupal\Debug\Kernel\Event\DebugKernelEvents;
25
use Symfony\Component\DependencyInjection\ContainerBuilder;
26
use Symfony\Component\DependencyInjection\Definition;
27
use Symfony\Component\DependencyInjection\Reference;
28
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
29
30
class WatchHooksImplementationsAction implements CompilerPassActionInterface, EventSubscriberActionInterface, ActionWithOptionsInterface
31
{
32
    /**
33
     * @var string
34
     */
35
    const RESOURCES_SERVICE_ID = 'ekino.drupal.debug.action.watch_hooks_implementations.resources';
36
37
    /**
38
     * @var WatchHooksImplementationsOptions
39
     */
40
    private $options;
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 1
    public static function getSubscribedEvents(): array
46
    {
47
        return array(
48 1
            DebugKernelEvents::AFTER_ATTACH_SYNTHETIC => 'setResources',
49
        );
50
    }
51
52
    /**
53
     * @param WatchHooksImplementationsOptions $options
54
     */
55 10
    public function __construct(WatchHooksImplementationsOptions $options)
56
    {
57 10
        $this->options = $options;
58 10
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 6
    public function process(ContainerBuilder $container): void
64
    {
65 6
        if (!$container->hasDefinition('module_handler')) {
66 1
            throw new NotSupportedException('The "module_handler" service should already be set in the container.');
67
        }
68
69 5
        $moduleHandlerDefinition = $container->getDefinition('module_handler');
70 5
        if (ModuleHandler::class !== $moduleHandlerDefinition->getClass()) {
71 1
            throw new NotSupportedException(\sprintf('The "module_handler" service class should be "%s".', ModuleHandler::class));
72
        }
73
74 4
        if (!$container->hasDefinition('event_dispatcher')) {
75 1
            throw new NotSupportedException('The "event_dispatcher" service should already be set in the container.');
76
        }
77
78 3
        $eventDispatcherDefinition = $container->getDefinition('event_dispatcher');
79 3
        $eventDispatcherClass = $eventDispatcherDefinition->getClass();
80 3
        if (!\is_string($eventDispatcherClass)) {
81 1
            throw new NotSupportedException('The "event_dispatcher" service class should be a string.');
82
        }
83
84 2
        if (!(new \ReflectionClass($eventDispatcherClass))->implementsInterface(EventDispatcherInterface::class)) {
85 1
            throw new NotSupportedException(\sprintf('The "event_dispatcher" service class should implement the "%s" interface.', EventDispatcherInterface::class));
86
        }
87
88 1
        $resourcesDefinition = new Definition();
89 1
        $resourcesDefinition->setSynthetic(true);
90 1
        $container->setDefinition(self::RESOURCES_SERVICE_ID, $resourcesDefinition);
91
92 1
        $fileBackendDefinition = new Definition(FileBackend::class, array(
93 1
            new Definition(FileCache::class, array(
94 1
                $this->options->getCacheFilePath(),
95 1
                new Reference(self::RESOURCES_SERVICE_ID),
96
            )),
97
        ));
98 1
        $fileBackendDefinition->addMethodCall('setEventDispatcher', array(
99 1
            new Reference('event_dispatcher'),
100
        ));
101
102 1
        $moduleHandlerDefinition->replaceArgument(2, $fileBackendDefinition);
103 1
    }
104
105
    /**
106
     * @param AfterAttachSyntheticEvent $event
107
     */
108
    public function setResources(AfterAttachSyntheticEvent $event): void
109
    {
110
        $event->getContainer()->set(self::RESOURCES_SERVICE_ID, $this->options->getFilteredResourcesCollection($event->getEnabledModules(), $event->getEnabledThemes()));
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 3
    public static function getOptionsClass(): string
117
    {
118 3
        return WatchHooksImplementationsOptions::class;
119
    }
120
}
121