Behat3ZendFramework3Extension   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 5.56%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 112
ccs 2
cts 36
cp 0.0556
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 13 1
A getConfigKey() 0 4 1
A initialize() 0 4 1
A load() 0 7 1
A loadInitializer() 0 12 1
A mergeConfigurationFilePath() 0 6 1
A loadArgumentResolver() 0 13 1
A process() 0 4 1
1
<?php
2
declare(strict_types=1);
3
namespace VirCom\Behat3ZendFramework3Extension\ServiceContainer;
4
5
use VirCom\Behat3ZendFramework3Extension\Services\ZendFramework3Initializer;
6
use VirCom\Behat3ZendFramework3Extension\Context\Initializer\KernelAwareInitializer;
7
use VirCom\Behat3ZendFramework3Extension\Context\Argument\ArgumentResolver;
8
9
use Behat\Testwork\ServiceContainer\Extension;
10
use Behat\Testwork\ServiceContainer\ExtensionManager;
11
use Behat\Behat\Context\ServiceContainer\ContextExtension;
12
use Behat\Testwork\EventDispatcher\ServiceContainer\EventDispatcherExtension;
13
14
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Definition;
17
18
use Zend\Mvc\ApplicationInterface;
19
20
class Behat3ZendFramework3Extension implements
21
    Extension
22
{
23
    const SERVICE_CONTAINER_NAME = "behat3_zendframework3_extension";
24
    const APPLICATION_CONTAINER_NAME = "zendframework3.application";
25
    const ARGUMENT_RESOLVER_CONTAINER_NAME = "zendframework3.argument_resolver";
26
27
    const BASE_PATH_PARAMETER_NAME = "paths.base";
28
29
    const CONFIGURATION_PATH_ARGUMENT_NAME = "configuration_path";
30
    const CONFIGURATION_PATH_DEFAULT_ARGUMENT_VALUE = "/config/application.config.php";
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function configure(
36
        ArrayNodeDefinition $builder
37
    ) {
38
        $builder
39
            ->children()
40
                ->scalarNode(self::CONFIGURATION_PATH_ARGUMENT_NAME)
41
                    ->info("Zend Framework 3 configuration file path")
42
                    ->defaultValue(self::CONFIGURATION_PATH_DEFAULT_ARGUMENT_VALUE)
43
                    ->end()
44
                ->end()
45
            ->end()
46
        ->end();
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 1
    public function getConfigKey(): string
53
    {
54 1
        return self::SERVICE_CONTAINER_NAME;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function initialize(
61
        ExtensionManager $extensionManager
62
    ) {
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function load(
69
        ContainerBuilder $container,
70
        array $config
71
    ) {
72
        $this->loadInitializer($container, $config);
73
        $this->loadArgumentResolver($container);
74
    }
75
76
    /**
77
     * @param ContainerBuilder $container
78
     * @param array $config
79
     * @return void
80
     */
81
    protected function loadInitializer(
82
        ContainerBuilder $container,
83
        array $config
84
    ) {
85
        $configurationFilePath = $this->mergeConfigurationFilePath(
86
            $container->getParameter(self::BASE_PATH_PARAMETER_NAME),
87
            $config[self::CONFIGURATION_PATH_ARGUMENT_NAME]
88
        );
89
90
        $application = (new ZendFramework3Initializer($configurationFilePath))->boot();
91
        $container->set(self::APPLICATION_CONTAINER_NAME, $application);
92
    }
93
94
    /**
95
     * @param string $basePath
96
     * @param string $configurationFilePath
97
     * @return string
98
     */
99
    protected function mergeConfigurationFilePath(
100
        string $basePath,
101
        string $configurationFilePath
102
    ): string {
103
        return $basePath . $configurationFilePath;
104
    }
105
106
    /**
107
     * @param ContainerBuilder $container
108
     * @return void
109
     */
110
    protected function loadArgumentResolver(
111
        ContainerBuilder $container
112
    ) {
113
        $definition = new Definition(
114
            ArgumentResolver::class,
115
            [
116
                $container->get(self::APPLICATION_CONTAINER_NAME),
117
            ]
118
        );
119
120
        $definition->addTag(ContextExtension::ARGUMENT_RESOLVER_TAG, ['priority' => 0]);
121
        $container->setDefinition(self::ARGUMENT_RESOLVER_CONTAINER_NAME, $definition);
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function process(
128
        ContainerBuilder $container
129
    ) {
130
    }
131
}
132