Completed
Pull Request — master (#62)
by Alessandro
05:54
created

ParallelConfiguration::buildContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.4285
cc 1
eloc 7
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Paraunit\Configuration;
4
5
use Symfony\Component\Config\FileLocator;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Definition;
9
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
10
use Symfony\Component\DependencyInjection\Reference;
11
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
12
13
/**
14
 * Class Paraunit
15
 * @package Paraunit\Configuration
16
 */
17
class ParallelConfiguration
18
{
19
    /**
20
     * @param InputInterface $input
21
     * @return ContainerBuilder
22
     * @throws \Exception
23
     */
24 41
    public function buildContainer(InputInterface $input)
25
    {
26 41
        $containerBuilder = new ContainerBuilder();
27
28 41
        $this->loadYamlConfiguration($containerBuilder);
29 41
        $this->registerEventDispatcher($containerBuilder);
30 41
        $this->loadCommandLineOptions($containerBuilder, $input);
31 41
        $containerBuilder->compile();
32
33 41
        return $containerBuilder;
34
    }
35
36
    /**
37
     * @param ContainerBuilder $containerBuilder
38
     * @return YamlFileLoader
39
     * @throws \Exception
40 41
     */
41
    protected function loadYamlConfiguration(ContainerBuilder $containerBuilder)
42 41
    {
43 41
        $loader = new YamlFileLoader($containerBuilder, new FileLocator(__DIR__ . '/../Resources/config/'));
44 41
        $loader->load('configuration.yml');
45 41
        $loader->load('file.yml');
46 41
        $loader->load('parser.yml');
47 41
        $loader->load('printer.yml');
48 41
        $loader->load('process.yml');
49 41
        $loader->load('services.yml');
50 41
        $loader->load('test_result.yml');
51 41
        $loader->load('test_result_container.yml');
52
        $loader->load('test_result_format.yml');
53 41
54
        $eventDispatcherPass = 'Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass';
55
        $deprecatedPass = 'Symfony\Component\HttpKernel\DependencyInjection\RegisterListenersPass';
56
        if (! class_exists($eventDispatcherPass)) {
57
            class_alias($deprecatedPass, $eventDispatcherPass);
58
        }
59 41
60
        return $loader;
61 41
    }
62 41
63
    /**
64 41
     * @param ContainerBuilder $containerBuilder
65 41
     * @throws \Symfony\Component\DependencyInjection\Exception\BadMethodCallException
66 41
     */
67 41
    private function registerEventDispatcher(ContainerBuilder $containerBuilder)
68 41
    {
69 41
        $containerBuilder->addCompilerPass(new RegisterListenersPass());
70 41
71 41
        $containerBuilder->setDefinition(
72
            'event_dispatcher',
73 41
            new Definition(
74
                'Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher',
75 41
                array(new Reference('service_container'))
76
            )
77
        );
78
    }
79
80
    protected function loadCommandLineOptions(ContainerBuilder $containerBuilder, InputInterface $input)
81
    {
82
        $containerBuilder->setParameter('paraunit.max_process_count', $input->getOption('parallel'));
83
    }
84
}
85