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

addEventSubscriberActionsToEventDispatcher()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
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 Ekino\Drupal\Debug\ActionMetadata\ActionMetadataManager;
17
use Ekino\Drupal\Debug\ActionMetadata\Model\ActionMetadata;
18
use Ekino\Drupal\Debug\ActionMetadata\Model\ActionWithOptionsMetadata;
19
use Ekino\Drupal\Debug\Configuration\ConfigurationManager;
20
use Ekino\Drupal\Debug\Option\OptionsInterface;
21
use Ekino\Drupal\Debug\Option\OptionsStack;
22
use Symfony\Component\DependencyInjection\ContainerBuilder;
23
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
24
25
class ActionRegistrar
26
{
27
    /**
28
     * @var EventSubscriberActionInterface[]
29
     */
30
    private $eventSubscriberActions;
31
32
    /**
33
     * @var CompilerPassActionInterface[]
34
     */
35
    private $compilerPassActions;
36
37 2
    public function __construct(
38
        string $appRoot,
39
        ActionMetadataManager $actionMetadataManager,
40
        ConfigurationManager $configurationManager,
41
        OptionsStack $optionsStack
42
    ) {
43 2
        $this->eventSubscriberActions = array();
44 2
        $this->compilerPassActions = array();
45
46 2
        foreach ($this->getActions($appRoot, $actionMetadataManager, $configurationManager, $optionsStack) as $action) {
47 2
            if ($action instanceof EventSubscriberActionInterface) {
48 2
                $this->eventSubscriberActions[] = $action;
49
            }
50
51 2
            if ($action instanceof CompilerPassActionInterface) {
52 2
                $this->compilerPassActions[] = $action;
53
            }
54
        }
55 2
    }
56
57
    /**
58
     * @param EventDispatcherInterface $eventDispatcher
59
     */
60 1
    public function addEventSubscriberActionsToEventDispatcher(EventDispatcherInterface $eventDispatcher): void
61
    {
62 1
        foreach ($this->eventSubscriberActions as $eventSubscriberAction) {
63 1
            $eventDispatcher->addSubscriber($eventSubscriberAction);
64
        }
65 1
    }
66
67
    /**
68
     * @param ContainerBuilder $containerBuilder
69
     */
70 1
    public function addCompilerPassActionsToContainerBuilder(ContainerBuilder $containerBuilder): void
71
    {
72 1
        foreach ($this->compilerPassActions as $compilerPassAction) {
73 1
            $containerBuilder->addCompilerPass($compilerPassAction);
74
        }
75 1
    }
76
77
    /**
78
     * @param string                $appRoot
79
     * @param OptionsStack          $optionsStack
80
     * @param ActionMetadataManager $actionMetadataManager
81
     * @param ConfigurationManager  $configurationManager
82
     *
83
     * @return ActionInterface[]
84
     */
85 2
    private function getActions(
86
        string $appRoot,
87
        ActionMetadataManager $actionMetadataManager,
88
        ConfigurationManager $configurationManager,
89
        OptionsStack $optionsStack
90
    ): array {
91 2
        $actions = array();
92
93
        /** @var ActionMetadata $actionMetadata */
94 2
        foreach ($actionMetadataManager->all() as $shortName => $actionMetadata) {
95 2
            $actionConfiguration = $configurationManager->getActionConfiguration($shortName);
96
97 2
            if (!$actionConfiguration->isEnabled()) {
98
                continue;
99
            }
100
101 2
            $args = array();
102 2
            if ($actionMetadata instanceof ActionWithOptionsMetadata) {
103 2
                $optionsClass = $actionMetadata->getOptionsClass();
104
105 2
                $options = $optionsStack->get($optionsClass);
106 2
                if (!$options instanceof OptionsInterface) {
107 2
                    $options = $optionsClass::getOptions($appRoot, $actionConfiguration);
108
                }
109
110 2
                $args[] = $options;
111
            }
112
113
            /** @var ActionInterface $action */
114 2
            $action = $actionMetadata->getReflectionClass()->newInstanceArgs($args);
115 2
            $actions[] = $action;
116
        }
117
118 2
        return $actions;
119
    }
120
}
121