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

TempDirectory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 90.91%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 58
ccs 20
cts 22
cp 0.9091
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getTempDirForThisExecution() 0 9 1
A getTempBaseDir() 0 13 3
A mkdirIfNotExists() 0 6 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