Completed
Pull Request — master (#93)
by Alessandro
02:47
created

TempDirectory::mkdirIfNotExists()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 3
Ratio 30 %

Code Coverage

Tests 4
CRAP Score 4.128

Importance

Changes 0
Metric Value
dl 3
loc 10
ccs 4
cts 5
cp 0.8
rs 9.2
c 0
b 0
f 0
cc 4
eloc 5
nc 3
nop 1
crap 4.128
1
<?php
2
declare(strict_types=1);
3
4
namespace Paraunit\File;
5
6
/**
7
 * Class TempDirectory
8
 * @package Paraunit\File
9
 */
10
class TempDirectory
11
{
12
    /** @var string[] */
13
    private static $tempDirs = [
14
        '/dev/shm',
15
    ];
16
17
    /** @var string */
18
    private static $timestamp;
19
20
    /**
21
     * Paraunit constructor.
22
     */
23 62
    public function __construct()
24
    {
25 62
        self::$timestamp = uniqid(date('Ymd-His'), true);
26
    }
27
28
    /**
29
     * @return string
30
     * @throws \RuntimeException If the temp dirs cannot be created
31
     */
32 54
    public function getTempDirForThisExecution(): string
33
    {
34 54
        $dir = self::getTempBaseDir() . DIRECTORY_SEPARATOR . self::$timestamp;
35 54
        self::mkdirIfNotExists($dir);
36 54
        self::mkdirIfNotExists($dir . DIRECTORY_SEPARATOR . 'config');
37 54
        self::mkdirIfNotExists($dir . DIRECTORY_SEPARATOR . 'logs');
38 54
        self::mkdirIfNotExists($dir . DIRECTORY_SEPARATOR . 'coverage');
39
40 54
        return $dir;
41
    }
42
43
    /**
44
     * @return string
45
     *
46
     * @throws \RuntimeException
47
     */
48 54
    public static function getTempBaseDir(): string
49
    {
50 54
        $dirs = self::$tempDirs;
51
        // Fallback to sys temp dir
52 54
        $dirs[] = sys_get_temp_dir();
53
54 54
        foreach ($dirs as $directory) {
55 54
            if (file_exists($directory)) {
56 54
                $baseDir = $directory . DIRECTORY_SEPARATOR . 'paraunit';
57
58
                try {
59 54
                    self::mkdirIfNotExists($baseDir);
60
61 54
                    return $baseDir;
62
                } catch (\RuntimeException $e) {
63
                    // ignore and try next dir
64
                }
65
            }
66
        }
67
68
        throw new \RuntimeException('Unable to create a temporary directory');
69
    }
70
71
    /**
72
     * @param string $path
73
     *
74
     * @throws \RuntimeException If the dir cannot be created
75
     */
76 54
    private static function mkdirIfNotExists(string $path)
77
    {
78 54
        if (file_exists($path)) {
79 54
            return;
80
        }
81
82 54 View Code Duplication
        if (!mkdir($path) && !is_dir($path)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
            throw new \RuntimeException('Unable to create temporary directory ' . $path);
84
        }
85
    }
86
}
87