Completed
Push — master ( 52b242...112fdc )
by Thomas
17s queued 11s
created

ActionMetadataManager   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 85
c 1
b 0
f 0
dl 0
loc 145
ccs 16
cts 17
cp 0.9412
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 9 2
A getInstance() 0 7 2
A isCoreAction() 0 3 1
A all() 0 3 1
A __construct() 0 4 2
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\ActionMetadata;
15
16
use Ekino\Drupal\Debug\Action\DisableCSSAggregation\DisableCSSAggregationAction;
17
use Ekino\Drupal\Debug\Action\DisableDynamicPageCache\DisableDynamicPageCacheAction;
18
use Ekino\Drupal\Debug\Action\DisableInternalPageCache\DisableInternalPageCacheAction;
19
use Ekino\Drupal\Debug\Action\DisableJSAggregation\DisableJSAggregationAction;
20
use Ekino\Drupal\Debug\Action\DisableRenderCache\DisableRenderCacheAction;
21
use Ekino\Drupal\Debug\Action\DisableTwigCache\DisableTwigCacheAction;
22
use Ekino\Drupal\Debug\Action\DisplayDumpLocation\DisplayDumpLocationAction;
23
use Ekino\Drupal\Debug\Action\DisplayPrettyExceptions\DisplayPrettyExceptionsAction;
24
use Ekino\Drupal\Debug\Action\DisplayPrettyExceptions\DisplayPrettyExceptionsOptions;
25
use Ekino\Drupal\Debug\Action\DisplayPrettyExceptionsASAP\DisplayPrettyExceptionsASAPAction;
26
use Ekino\Drupal\Debug\Action\DisplayPrettyExceptionsASAP\DisplayPrettyExceptionsASAPOptions;
27
use Ekino\Drupal\Debug\Action\EnableDebugClassLoader\EnableDebugClassLoaderAction;
28
use Ekino\Drupal\Debug\Action\EnableTwigDebug\EnableTwigDebugAction;
29
use Ekino\Drupal\Debug\Action\EnableTwigStrictVariables\EnableTwigStrictVariablesAction;
30
use Ekino\Drupal\Debug\Action\ThrowErrorsAsExceptions\ThrowErrorsAsExceptionsAction;
31
use Ekino\Drupal\Debug\Action\ThrowErrorsAsExceptions\ThrowErrorsAsExceptionsOptions;
32
use Ekino\Drupal\Debug\Action\WatchContainerDefinitions\WatchContainerDefinitionsAction;
33
use Ekino\Drupal\Debug\Action\WatchContainerDefinitions\WatchContainerDefinitionsOptions;
34
use Ekino\Drupal\Debug\Action\WatchModulesHooksImplementations\WatchModulesHooksImplementationsAction;
35
use Ekino\Drupal\Debug\Action\WatchModulesHooksImplementations\WatchModulesHooksImplementationsOptions;
36
use Ekino\Drupal\Debug\Action\WatchRoutingDefinitions\WatchRoutingDefinitionsAction;
37
use Ekino\Drupal\Debug\Action\WatchRoutingDefinitions\WatchRoutingDefinitionsOptions;
38
use Ekino\Drupal\Debug\ActionMetadata\Model\ActionMetadata;
39
use Ekino\Drupal\Debug\ActionMetadata\Model\ActionWithOptionsMetadata;
40
41
class ActionMetadataManager
42
{
43
    private const CORE_ACTIONS = array(
44
        'disable_css_aggregation' => array(
45
            ActionMetadata::class,
46
            DisableCSSAggregationAction::class,
47
            array(),
48
        ),
49
        'disable_dynamic_page_cache' => array(
50
            ActionMetadata::class,
51
            DisableDynamicPageCacheAction::class,
52
            array(),
53
        ),
54
        'disable_internal_page_cache' => array(
55
            ActionMetadata::class,
56
            DisableInternalPageCacheAction::class,
57
            array(),
58
        ),
59
        'disable_js_aggregation' => array(
60
            ActionMetadata::class,
61
            DisableJSAggregationAction::class,
62
            array(),
63
        ),
64
        'disable_render_cache' => array(
65
            ActionMetadata::class,
66
            DisableRenderCacheAction::class,
67
            array(),
68
        ),
69
        'disable_twig_cache' => array(
70
            ActionMetadata::class,
71
            DisableTwigCacheAction::class,
72
            array(),
73
        ),
74
        'display_dump_location' => array(
75
            ActionMetadata::class,
76
            DisplayDumpLocationAction::class,
77
            array(),
78
        ),
79
        'display_pretty_exceptions' => array(
80
            ActionWithOptionsMetadata::class,
81
            DisplayPrettyExceptionsAction::class,
82
            array(
83
                DisplayPrettyExceptionsOptions::class,
84
            ),
85
        ),
86
        'display_pretty_exceptions_asap' => array(
87
            ActionWithOptionsMetadata::class,
88
            DisplayPrettyExceptionsASAPAction::class,
89
            array(
90
                DisplayPrettyExceptionsASAPOptions::class,
91
            ),
92
        ),
93
        'enable_debug_class_loader' => array(
94
            ActionMetadata::class,
95
            EnableDebugClassLoaderAction::class,
96
            array(),
97
        ),
98
        'enable_twig_debug' => array(
99
            ActionMetadata::class,
100
            EnableTwigDebugAction::class,
101
            array(),
102
        ),
103
        'enable_twig_strict_variables' => array(
104
            ActionMetadata::class,
105
            EnableTwigStrictVariablesAction::class,
106
            array(),
107
        ),
108
        'throw_errors_as_exceptions' => array(
109
            ActionWithOptionsMetadata::class,
110
            ThrowErrorsAsExceptionsAction::class,
111
            array(
112
                ThrowErrorsAsExceptionsOptions::class,
113
            ),
114
        ),
115
        'watch_container_definitions' => array(
116
            ActionWithOptionsMetadata::class,
117
            WatchContainerDefinitionsAction::class,
118
            array(
119
                WatchContainerDefinitionsOptions::class,
120
            ),
121
        ),
122
        'watch_modules_hooks_implementations' => array(
123
            ActionWithOptionsMetadata::class,
124
            WatchModulesHooksImplementationsAction::class,
125
            array(
126
                WatchModulesHooksImplementationsOptions::class,
127
            ),
128
        ),
129
        'watch_routing_definitions' => array(
130
            ActionWithOptionsMetadata::class,
131
            WatchRoutingDefinitionsAction::class,
132
            array(
133
                WatchRoutingDefinitionsOptions::class,
134
            ),
135
        ),
136
    );
137
138
    /**
139
     * @var ActionMetadata[]
140
     */
141
    private $actionsMetadata;
142
143
    private static $instance;
144
145
    /**
146
     * @internal
147
     */
148 31
    public function __construct()
149
    {
150 31
        foreach (self::CORE_ACTIONS as $shortName => list($actionMetadataClass, $actionClass, $args)) {
151 31
            $this->add(new $actionMetadataClass(new \ReflectionClass($actionClass), $shortName, ...$args));
152
        }
153 31
    }
154
155 31
    public static function getInstance(): self
156
    {
157 31
        if (!self::$instance instanceof self) {
158 31
            self::$instance = new self();
159
        }
160
161 31
        return self::$instance;
162
    }
163
164
    /**
165
     * @return ActionMetadata[]
166
     */
167 19
    public function all(): array
168
    {
169 19
        return $this->actionsMetadata;
170
    }
171
172 31
    public function add(ActionMetadata $actionMetadata): self
173
    {
174 31
        if (isset($this->actionsMetadata[$shortName = $actionMetadata->getShortName()])) {
175
            throw new \RuntimeException();
176
        }
177
178 31
        $this->actionsMetadata[$shortName] = $actionMetadata;
179
180 31
        return $this;
181
    }
182
183 19
    public function isCoreAction(string $shortName): bool
184
    {
185 19
        return isset(self::CORE_ACTIONS[$shortName]);
186
    }
187
}
188