Completed
Push — output_parsers_refactor ( d2cb12...a7c18a )
by Alessandro
03:46
created

Paraunit::buildContainer()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 1

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 25
ccs 18
cts 18
cp 1
rs 8.8571
cc 1
eloc 16
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Paraunit\Configuration;
4
5
use Symfony\Component\Config\FileLocator;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\DependencyInjection\Definition;
8
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
9
use Symfony\Component\DependencyInjection\Reference;
10
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
11
12
/**
13
 * Class Paraunit
14
 * @package Paraunit\Configuration
15
 */
16
class Paraunit
17
{
18
    const PARAUNIT_VERSION = '0.5.1';
19
20
    private static $tempDirs = array(
21
        '/dev/shm',
22
        '/temp',
23
    );
24
25
    /** @var  string */
26
    private $timestamp;
27
28
    /**
29
     * Paraunit constructor.
30
     */
31 27
    public function __construct()
32
    {
33 27
        $this->timestamp = uniqid(date('Ymd-His'));
34 27
    }
35
36
    /**
37
     * @return ContainerBuilder
38
     */
39 26
    public static function buildContainer()
40
    {
41 26
        $containerBuilder = new ContainerBuilder();
42
43 26
        $loader = new YamlFileLoader($containerBuilder, new FileLocator(__DIR__ . '/../Resources/config/'));
44 26
        $loader->load('configuration.yml');
45 26
        $loader->load('output_container.yml');
46 26
        $loader->load('parser.yml');
47 26
        $loader->load('printer.yml');
48 26
        $loader->load('services.yml');
49
50 26
        $containerBuilder->addCompilerPass(new RegisterListenersPass());
51
52 26
        $containerBuilder->setDefinition(
53 26
            'event_dispatcher',
54 26
            new Definition(
55 26
                'Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher',
56 26
                array(new Reference('service_container'))
57 26
            )
58 26
        );
59
60 26
        $containerBuilder->compile();
61
62 26
        return $containerBuilder;
63
    }
64
65
    /**
66
     * @return string
67
     */
68 27
    public function getTempDirForThisExecution()
69
    {
70 27
        $dir = self::getTempBaseDir() . DIRECTORY_SEPARATOR . $this->timestamp;
71 27
        self::mkdirIfNotExists($dir);
72 27
        self::mkdirIfNotExists($dir . DIRECTORY_SEPARATOR . 'logs');
73 27
        self::mkdirIfNotExists($dir . DIRECTORY_SEPARATOR . 'coverage');
74
75 27
        return $dir;
76
    }
77
78
    /**
79
     * @return string
80
     */
81 27
    public static function getTempBaseDir()
82
    {
83 27
        foreach (self::$tempDirs as $directory) {
84 27
            if (file_exists($directory)) {
85 27
                $baseDir = $directory . DIRECTORY_SEPARATOR . 'paraunit';
86 27
                self::mkdirIfNotExists($baseDir);
87
88 27
                return $baseDir;
89
            }
90
        }
91
92
        throw new \RuntimeException('Unable to create a temporary directory');
93
    }
94
95
    /**
96
     * @param string $path
97
     */
98 27
    private static function mkdirIfNotExists($path)
99
    {
100 27
        if ( ! file_exists($path)) {
101 27
            mkdir($path);
102 27
        }
103 27
    }
104
}
105