Completed
Push — output_parsers_refactor ( ea70d9...668933 )
by Alessandro
02:32
created

Paraunit::getTempBaseDir()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0987

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 7
cts 9
cp 0.7778
rs 9.4285
cc 3
eloc 7
nc 3
nop 0
crap 3.0987
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 19
    public function __construct()
32
    {
33 19
        $this->timestamp = uniqid(date('Ymd-His'));
34 19
    }
35
36
    /**
37
     * @return ContainerBuilder
38
     */
39 18
    public static function buildContainer()
40
    {
41 18
        $containerBuilder = new ContainerBuilder();
42
43 18
        $loader = new YamlFileLoader($containerBuilder, new FileLocator(__DIR__ . '/../Resources/config/'));
44 18
        $loader->load('services.yml');
45 18
        $loader->load('parser.yml');
46 18
        $loader->load('configuration.yml');
47
48 18
        $containerBuilder->addCompilerPass(new RegisterListenersPass());
49
50 18
        $containerBuilder->setDefinition(
51 18
            'event_dispatcher',
52 18
            new Definition(
53 18
                'Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher',
54 18
                array(new Reference('service_container'))
55 18
            )
56 18
        );
57
58 18
        $containerBuilder->compile();
59
60 18
        return $containerBuilder;
61
    }
62
63
    /**
64
     * @return string
65
     */
66 17
    public function getTempDirForThisExecution()
67
    {
68 17
        $dir = self::getTempBaseDir() . DIRECTORY_SEPARATOR . $this->timestamp;
69 17
        self::mkdirIfNotExists($dir);
70 17
        self::mkdirIfNotExists($dir . DIRECTORY_SEPARATOR . 'logs');
71 17
        self::mkdirIfNotExists($dir . DIRECTORY_SEPARATOR . 'coverage');
72
73 17
        return $dir;
74
    }
75
76
    /**
77
     * @return string
78
     */
79 19
    public static function getTempBaseDir()
80 1
    {
81 19
        foreach (self::$tempDirs as $directory) {
82 19
            if (file_exists($directory)) {
83 19
                $baseDir = $directory . DIRECTORY_SEPARATOR . 'paraunit';
84 19
                self::mkdirIfNotExists($baseDir);
85
86 19
                return $baseDir;
87
            }
88
        }
89
90
        throw new \RuntimeException('Unable to create a temporary directory');
91
    }
92
93
    /**
94
     * @param string $path
95
     */
96 19
    private static function mkdirIfNotExists($path)
97
    {
98 19
        if ( ! file_exists($path)) {
99 18
            mkdir($path);
100 18
        }
101 19
    }
102
}
103