Completed
Pull Request — master (#94)
by Alessandro
08:00
created

ParallelConfiguration::loadPostCompileSettings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 2
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Paraunit\Configuration;
5
6
use Paraunit\Printer\OutputFactory;
7
use Symfony\Component\Config\FileLocator;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
use Symfony\Component\DependencyInjection\Definition;
12
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
13
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
14
15
/**
16
 * Class Paraunit
17
 * @package Paraunit\Configuration
18
 */
19
class ParallelConfiguration
20
{
21
    /**
22
     * @param InputInterface $input
23
     * @param OutputInterface $output
24 57
     * @return ContainerBuilder
25
     * @throws \Symfony\Component\DependencyInjection\Exception\BadMethodCallException
26 57
     * @throws \Exception
27
     */
28 57
    public function buildContainer(InputInterface $input, OutputInterface $output): ContainerBuilder
29 57
    {
30 57
        $containerBuilder = new ContainerBuilder();
31
32 57
        $this->injectOutput($containerBuilder, $output);
33
        $this->loadYamlConfiguration($containerBuilder);
34 57
        $this->loadCommandLineOptions($containerBuilder, $input);
35
        $this->registerEventSubscribers($containerBuilder);
36 57
37
        $containerBuilder->compile();
38
39
        $this->loadPostCompileSettings($containerBuilder, $input);
40
41
        return $containerBuilder;
42
    }
43
44 57
    /**
45
     * @param ContainerBuilder $containerBuilder
46 57
     * @return YamlFileLoader
47 57
     * @throws \Exception
48 57
     */
49 57
    protected function loadYamlConfiguration(ContainerBuilder $containerBuilder): YamlFileLoader
50 57
    {
51 57
        $loader = new YamlFileLoader($containerBuilder, new FileLocator(__DIR__ . '/../Resources/config/'));
52 57
        $loader->load('configuration.yml');
53 57
        $loader->load('file.yml');
54 57
        $loader->load('parser.yml');
55 57
        $loader->load('printer.yml');
56
        $loader->load('process.yml');
57 57
        $loader->load('runner.yml');
58 57
        $loader->load('services.yml');
59 57
        $loader->load('test_result.yml');
60
        $loader->load('test_result_container.yml');
61
        $loader->load('test_result_format.yml');
62
63 57
        return $loader;
64
    }
65
66
    protected function registerEventSubscribers(ContainerBuilder $container)
67
    {
68
        $eventDispatcher = $container->getDefinition('event_dispatcher');
69
70 57
        foreach ($container->getDefinitions() as $definition) {
71
            if (is_subclass_of($definition->getClass(), EventSubscriberInterface::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Symfony\Component\Event...scriberInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
72 57
                $eventDispatcher->addMethodCall('addSubscriber', [$definition]);
73
            }
74 57
        }
75 57
    }
76 57
77 57
    protected function loadCommandLineOptions(ContainerBuilder $containerBuilder, InputInterface $input)
78 57
    {
79
        $containerBuilder->setParameter('paraunit.max_process_count', $input->getOption('parallel'));
80
        $containerBuilder->setParameter('paraunit.phpunit_config_filename', $input->getOption('configuration') ?? '.');
81 57
        $containerBuilder->setParameter('paraunit.testsuite', $input->getOption('testsuite'));
82
        $containerBuilder->setParameter('paraunit.string_filter', $input->getArgument('stringFilter'));
83 57
    }
84
85 57
    protected function loadPostCompileSettings(ContainerBuilder $container, InputInterface $input)
86 57
    {
87 57
    }
88 57
89 57
    private function injectOutput(ContainerBuilder $containerBuilder, OutputInterface $output)
90
    {
91 48
        OutputFactory::setOutput($output);
92
        $factoryClass = OutputFactory::class;
93 48
        $factoryMethod = 'getOutput';
94
95
        $definition = new Definition(OutputInterface::class);
96
        $definition->setFactory([$factoryClass, $factoryMethod]);
97
98
        $containerBuilder->setDefinition('output', $definition);
99
    }
100
}
101