Completed
Push — master ( 6ef701...eb43cb )
by Alessandro
13s
created

ParallelConfiguration::tagEventSubscribers()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 8.8571
c 2
b 0
f 0
cc 5
eloc 6
nc 4
nop 1
crap 5
1
<?php
2
declare(strict_types=1);
3
4
namespace Paraunit\Configuration;
5
6
use Paraunit\Configuration\DependencyInjection\ParallelContainerDefinition;
7
use Paraunit\Printer\DebugPrinter;
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\Reference;
13
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
14
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16
17
/**
18
 * Class Paraunit
19
 * @package Paraunit\Configuration
20
 */
21
class ParallelConfiguration
22
{
23
    const TAG_EVENT_SUBSCRIBER = 'paraunit.event_subscriber';
24
25
    /** @var ParallelContainerDefinition */
26
    protected $containerDefinition;
27
28 20
    public function __construct()
29
    {
30 20
        $this->containerDefinition = new ParallelContainerDefinition();
31
    }
32
33
    /**
34
     * @param InputInterface $input
35
     * @param OutputInterface $output
36
     * @return ContainerBuilder
37
     * @throws \Symfony\Component\DependencyInjection\Exception\BadMethodCallException
38
     * @throws \Exception
39
     */
40 63
    public function buildContainer(InputInterface $input, OutputInterface $output): ContainerBuilder
41
    {
42 63
        $containerBuilder = new ContainerBuilder();
43
44 63
        $this->injectOutput($containerBuilder, $output);
45 63
        $this->containerDefinition->configure($containerBuilder);
46 63
        $this->loadCommandLineOptions($containerBuilder, $input);
47 63
        $this->tagEventSubscribers($containerBuilder);
48
49 63
        $containerBuilder->compile();
50
51 63
        $this->loadPostCompileSettings($containerBuilder, $input);
52
53 63
        return $containerBuilder;
54
    }
55
56 63
    protected function tagEventSubscribers(ContainerBuilder $container)
57
    {
58 63
        foreach ($container->getDefinitions() as $definition) {
59 63
            if ($definition->isSynthetic() || $definition->isAbstract()) {
60 63
                continue;
61
            }
62
63 63
            if (array_key_exists(EventSubscriberInterface::class, class_implements($definition->getClass()))) {
64 63
                $definition->addTag(self::TAG_EVENT_SUBSCRIBER);
65
            }
66
        }
67
    }
68
69 63
    protected function loadCommandLineOptions(ContainerBuilder $containerBuilder, InputInterface $input)
70
    {
71 63
        $containerBuilder->setParameter('paraunit.max_process_count', $input->getOption('parallel'));
72 63
        $containerBuilder->setParameter('paraunit.phpunit_config_filename', $input->getOption('configuration') ?? '.');
73 63
        $containerBuilder->setParameter('paraunit.testsuite', $input->getOption('testsuite'));
74 63
        $containerBuilder->setParameter('paraunit.string_filter', $input->getArgument('stringFilter'));
75 63
        $containerBuilder->setParameter('paraunit.show_logo', $input->getOption('logo'));
76
77 63
        if ($input->getOption('debug')) {
78 3
            $this->enableDebugMode($containerBuilder);
79
        }
80
    }
81
82
    protected function loadPostCompileSettings(ContainerBuilder $container, InputInterface $input)
83
    {
84
    }
85
86 63
    private function injectOutput(ContainerBuilder $containerBuilder, OutputInterface $output)
87
    {
88 63
        $containerBuilder->register(OutputInterface::class)
89 63
            ->setSynthetic(true);
90
91 63
        $containerBuilder->set(OutputInterface::class, $output);
92
    }
93
94 3
    private function enableDebugMode(ContainerBuilder $containerBuilder)
95
    {
96 3
        $definition = new Definition(DebugPrinter::class, [new Reference(OutputInterface::class)]);
97 3
        $definition->addTag(self::TAG_EVENT_SUBSCRIBER);
98
99 3
        $containerBuilder->setDefinition(DebugPrinter::class, $definition);
100
    }
101
}
102