Completed
Pull Request — master (#94)
by Alessandro
04:33
created

TempDirectory::getTempBaseDir()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.128

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
ccs 8
cts 10
cp 0.8
rs 8.9197
cc 4
eloc 11
nc 4
nop 0
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 60
    public function __construct()
24
    {
25 60
        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 52
    public function getTempDirForThisExecution(): string
33
    {
34 52
        $dir = self::getTempBaseDir() . DIRECTORY_SEPARATOR . self::$timestamp;
35 52
        self::mkdirIfNotExists($dir);
36 52
        self::mkdirIfNotExists($dir . DIRECTORY_SEPARATOR . 'config');
37 52
        self::mkdirIfNotExists($dir . DIRECTORY_SEPARATOR . 'logs');
38 52
        self::mkdirIfNotExists($dir . DIRECTORY_SEPARATOR . 'coverage');
39
40 52
        return $dir;
41
    }
42
43
    /**
44
     * @return string
45
     *
46
     * @throws \RuntimeException
47
     */
48 52
    public static function getTempBaseDir(): string
49
    {
50 52
        $dirs = self::$tempDirs;
51
        // Fallback to sys temp dir
52 52
        $dirs[] = sys_get_temp_dir();
53
54 52
        foreach ($dirs as $directory) {
55 52
            if (file_exists($directory)) {
56 52
                $baseDir = $directory . DIRECTORY_SEPARATOR . 'paraunit';
57
58
                try {
59 52
                    self::mkdirIfNotExists($baseDir);
60
61 52
                    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 52
    private static function mkdirIfNotExists(string $path)
77
    {
78 52
        if (file_exists($path)) {
79 52
            return;
80
        }
81
82 52 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