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

overrideSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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;
15
16
use Drupal\Core\Cache\NullBackendFactory;
17
use Ekino\Drupal\Debug\Helper\SettingsHelper;
18
use Ekino\Drupal\Debug\Kernel\Event\AfterContainerInitializationEvent;
19
use Ekino\Drupal\Debug\Kernel\Event\DebugKernelEvents;
20
21
abstract class AbstractDisableDrupalCacheAction implements EventSubscriberActionInterface
22
{
23
    /**
24
     * @var string
25
     */
26
    const NULL_BACKEND_FACTORY_SERVICE_ID = 'ekino.drupal.debug.action.abstract_disable_cache.null_backend_factory';
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 1
    public static function getSubscribedEvents(): array
32
    {
33
        return array(
34 1
            DebugKernelEvents::AFTER_SETTINGS_INITIALIZATION => 'overrideSettings',
35 1
            DebugKernelEvents::AFTER_CONTAINER_INITIALIZATION => 'setNullBackend',
36
        );
37
    }
38
39 1
    public function overrideSettings(): void
40
    {
41 1
        (new SettingsHelper())->override(\sprintf('[cache][bins][%s]', $this->getBin()), self::NULL_BACKEND_FACTORY_SERVICE_ID);
42 1
    }
43
44
    /**
45
     * @param AfterContainerInitializationEvent $event
46
     */
47 2
    public function setNullBackend(AfterContainerInitializationEvent $event): void
48
    {
49 2
        $container = $event->getContainer();
50 2
        if ($container->has(self::NULL_BACKEND_FACTORY_SERVICE_ID)) {
51 1
            return;
52
        }
53
54 1
        $container->set(self::NULL_BACKEND_FACTORY_SERVICE_ID, new NullBackendFactory());
55 1
    }
56
57
    /**
58
     * @return string
59
     */
60
    abstract protected function getBin(): string;
61
}
62