Completed
Push — output_parsers_refactor ( 5337aa...a0f5ee )
by Alessandro
03:14
created

TempDirectory::mkdirIfNotExists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Paraunit\File;
4
5
/**
6
 * Class TempDirectory
7
 * @package Paraunit\File
8
 */
9
class TempDirectory
10
{
11
    private static $tempDirs = array(
12
        '/dev/shm',
13
        '/temp',
14
    );
15
16
    /** @var  string */
17
    private $timestamp;
18
19
    /**
20
     * Paraunit constructor.
21
     */
22 28
    public function __construct()
23
    {
24 28
        $this->timestamp = uniqid(date('Ymd-His'));
25 28
    }
26
27
    /**
28
     * @return string
29
     */
30 28
    public function getTempDirForThisExecution()
31
    {
32 28
        $dir = self::getTempBaseDir() . DIRECTORY_SEPARATOR . $this->timestamp;
33 28
        self::mkdirIfNotExists($dir);
34 28
        self::mkdirIfNotExists($dir . DIRECTORY_SEPARATOR . 'logs');
35 28
        self::mkdirIfNotExists($dir . DIRECTORY_SEPARATOR . 'coverage');
36
37 28
        return $dir;
38
    }
39
40
    /**
41
     * @return string
42
     */
43 28
    public static function getTempBaseDir()
44
    {
45 28
        foreach (self::$tempDirs as $directory) {
46 28
            if (file_exists($directory)) {
47 28
                $baseDir = $directory . DIRECTORY_SEPARATOR . 'paraunit';
48 28
                self::mkdirIfNotExists($baseDir);
49
50 28
                return $baseDir;
51
            }
52
        }
53
54
        throw new \RuntimeException('Unable to create a temporary directory');
55
    }
56
57
    /**
58
     * @param string $path
59
     */
60 28
    private static function mkdirIfNotExists($path)
61
    {
62 28
        if ( ! file_exists($path)) {
63 28
            mkdir($path);
64 28
        }
65 28
    }
66
}
67