Failed Conditions
Push — feature/improve ( a8a968...727873 )
by Yo
02:31 queued 13s
created

KernelSubExtension::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 67
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 57
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 67
ccs 57
cts 57
cp 1
rs 9.2815
c 0
b 0
f 0
cc 1
eloc 45
nc 1
nop 2
crap 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Yoanm\Behat3SymfonyExtension\ServiceContainer\SubExtension;
3
4
use Behat\MinkExtension\ServiceContainer\MinkExtension;
5
use Behat\Testwork\EventDispatcher\ServiceContainer\EventDispatcherExtension;
6
use Behat\Testwork\ServiceContainer\Exception\ProcessingException;
7
use Behat\Testwork\ServiceContainer\ExtensionManager;
8
use Symfony\Component\BrowserKit\CookieJar;
9
use Symfony\Component\BrowserKit\History;
10
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
11
use Symfony\Component\DependencyInjection\ContainerBuilder;
12
use Symfony\Component\DependencyInjection\Definition;
13
use Symfony\Component\DependencyInjection\Reference;
14
use Yoanm\Behat3SymfonyExtension\Client\Client;
15
use Yoanm\Behat3SymfonyExtension\Context\Initializer\KernelAwareInitializer;
16
use Yoanm\Behat3SymfonyExtension\Context\Initializer\KernelHandlerAwareInitializer;
17
use Yoanm\Behat3SymfonyExtension\Dispatcher\BehatKernelEventDispatcher;
18
use Yoanm\Behat3SymfonyExtension\Factory\KernelFactory;
19
use Yoanm\Behat3SymfonyExtension\Handler\KernelHandler;
20
use Yoanm\Behat3SymfonyExtension\ServiceContainer\AbstractExtension;
21
use Yoanm\Behat3SymfonyExtension\ServiceContainer\DriverFactory\Behat3SymfonyFactory;
22
use Yoanm\Behat3SymfonyExtension\Subscriber\RebootKernelSubscriber;
23
24
class KernelSubExtension extends AbstractExtension
25
{
26
    /**
27
     * @inheritDoc
28
     */
29 3
    public function getConfigKey()
30
    {
31 3
        return 'kernel';
32
    }
33
34
    // @codeCoverageIgnoreStart
35
    // Not possible to cover this because ExtensionManager is a final class
36
    /**
37
     * @inheritDoc
38
     */
39
    public function initialize(ExtensionManager $extensionManager)
40
    {
41
        $minExtension = $extensionManager->getExtension('mink');
42
        if ($minExtension instanceof MinkExtension) {
43
            $minExtension->registerDriverFactory(new Behat3SymfonyFactory());
44
        }
45
    }
46
    // @codeCoverageIgnoreEnd
47
48
    // @codeCoverageIgnoreStart
49
    // Will be covered by FT
50
    /**
51
     * @inheritDoc
52
     */
53
    public function configure(ArrayNodeDefinition $builder)
54
    {
55
        $castToBool = function ($value) {
56
            $filtered = filter_var(
57
                $value,
58
                FILTER_VALIDATE_BOOLEAN,
59
                FILTER_NULL_ON_FAILURE
60
            );
61
62
            return (null === $filtered) ? (bool) $value : $filtered;
63
        };
64
        $builder
65
            ->addDefaultsIfNotSet()
66
            ->children()
67
                ->scalarNode('bootstrap')
68
                    ->defaultValue('app/autoload.php')
69
                ->end()
70
                ->scalarNode('path')
71
                    ->defaultValue('app/AppKernel.php')
72
                ->end()
73
                ->scalarNode('class')
74
                    ->defaultValue('AppKernel')
75
                ->end()
76
                ->scalarNode('env')
77
                    ->defaultValue('test')
78
                ->end()
79
                ->booleanNode('debug')
80
                    ->beforeNormalization()
81
                    ->always()
82
                        ->then($castToBool)
83
                    ->end()
84
                    ->defaultTrue()
85
                ->end()
86
                ->booleanNode('reboot')
87
                    ->info('If true symfony kernel will be rebooted after each scenario/example')
88
                    ->beforeNormalization()
89
                        ->always()
90
                        ->then($castToBool)
91
                    ->end()
92
                    ->defaultTrue()
93
                ->end()
94
            ->end();
95
    }
96
    // @codeCoverageIgnoreEnd
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 2
    public function load(ContainerBuilder $container, array $config)
102
    {
103 2
        $kernelConfig = $config[$this->getConfigKey()];
104
105 2
        $this->loadContainerParameter($container, $kernelConfig);
106 2
        $this->loadInitializer($container);
107 2
        $this->loadSubscriber($container, $kernelConfig);
108 2
        $this->createService(
109 2
            $container,
110 2
            'test.client',
111 2
            Client::class,
112
            [
113 2
                    new Reference(self::KERNEL_SERVICE_ID),
114 2
                    [],
115 2
                    new Reference($this->buildContainerId('test.client.history')),
116 2
                    new Reference($this->buildContainerId('test.client.cookiejar'))
117 2
                ]
118 2
        );
119 2
        $this->createService(
120 2
            $container,
121 2
            'test.client.history',
122
            History::class
123 2
        );
124 2
        $this->createService(
125 2
            $container,
126 2
            'test.client.cookiejar',
127
            CookieJar::class
128 2
        );
129 2
        $this->createService(
130 2
            $container,
131 2
            'handler.kernel',
132 2
            KernelHandler::class,
133
            [
134 2
                new Reference('event_dispatcher'),
135 2
                new Reference(self::KERNEL_SERVICE_ID),
136
            ]
137 2
        );
138 2
        $this->createService(
139 2
            $container,
140 2
            'dispatcher.kernel_event',
141 2
            BehatKernelEventDispatcher::class,
142 2
            [new Reference('event_dispatcher')]
143 2
        );
144
        // Load Kernel thanks to the factory
145 2
        $this->createService(
146 2
            $container,
147 2
            'kernel',
148 2
            $kernelConfig['class'],
149 2
            [],
150 2
            [],
151 2
            [],
152 2
            [new Reference($this->buildContainerId('factory.kernel')), 'load']
153 2
        );
154
155 2
        $this->createService(
156 2
            $container,
157 2
            'factory.kernel',
158 2
            KernelFactory::class,
159
            [
160 2
                new Reference($this->buildContainerId('dispatcher.kernel_event')),
161 2
                '%'.$this->buildContainerId('kernel.path').'%',
162 2
                '%'.$this->buildContainerId('kernel.class').'%',
163 2
                '%'.$this->buildContainerId('kernel.env').'%',
164 2
                '%'.$this->buildContainerId('kernel.debug').'%'
165 2
            ]
166 2
        );
167 2
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172 3
    public function process(ContainerBuilder $container)
173
    {
174 3
        $basePath = $container->getParameter('paths.base');
175 3
        $bootstrapPath = $container->getParameter($this->buildContainerId('kernel.bootstrap'));
176 3
        if ($bootstrapPath) {
177 2
            $bootstrapPathUnderBasePath = sprintf('%s/%s', $basePath, $bootstrapPath);
178 2
            if (file_exists($bootstrapPathUnderBasePath)) {
179 1
                $bootstrapPath = $bootstrapPathUnderBasePath;
180 1
            }
181 2
            if (file_exists($bootstrapPath)) {
182 1
                require_once($bootstrapPath);
183 1
            } else {
184 1
                throw new ProcessingException('Could not find bootstrap file !');
185
            }
186 1
        }
187
188
        // load kernel
189 2
        $kernelPath = $container->getParameter($this->buildContainerId('kernel.path'));
190 2
        $kernelPathUnderBasePath = sprintf('%s/%s', $basePath, $kernelPath);
191 2
        if (file_exists($kernelPathUnderBasePath)) {
192
            $kernelPath = $kernelPathUnderBasePath;
193
        }
194
195 2
        $container->getDefinition(self::KERNEL_SERVICE_ID)
196 2
            ->setFile($kernelPath);
197 2
    }
198
199
    /**
200
     * @param ContainerBuilder $container
201
     */
202 2
    protected function loadInitializer(ContainerBuilder $container)
203
    {
204 2
        $this->createService(
205 2
            $container,
206 2
            'initializer.kernel_aware',
207 2
            KernelAwareInitializer::class,
208 2
            [new Reference(self::KERNEL_SERVICE_ID)],
209 2
            ['context.initializer']
210 2
        );
211
212 2
        $this->createService(
213 2
            $container,
214 2
            'initializer.kernel_handler_aware',
215 2
            KernelHandlerAwareInitializer::class,
216 2
            [new Reference($this->buildContainerId('handler.kernel'))],
217 2
            ['context.initializer']
218 2
        );
219 2
    }
220
221
    /**
222
     * @param ContainerBuilder $container
223
     * @param $kernelConfig
224
     */
225 2
    protected function loadSubscriber(ContainerBuilder $container, $kernelConfig)
226
    {
227 2
        if (true === $kernelConfig['reboot']) {
228 1
            $this->createService(
229 1
                $container,
230 1
                'subscriber.reboot_kernel',
231 1
                RebootKernelSubscriber::class,
232 1
                [new Reference($this->buildContainerId('handler.kernel'))],
233 1
                [EventDispatcherExtension::SUBSCRIBER_TAG]
234 1
            );
235 1
        }
236 2
    }
237
238
    /**
239
     * @param ContainerBuilder $container
240
     * @param $kernelConfig
241
     */
242 2
    protected function loadContainerParameter(ContainerBuilder $container, $kernelConfig)
243
    {
244 2
        foreach ($kernelConfig as $key => $value) {
245 2
            $container->setParameter($this->buildContainerId(sprintf('kernel.%s', $key)), $value);
246 2
        }
247 2
    }
248
}
249