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

TempDirectory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 3.9 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 3
loc 77
ccs 21
cts 24
cp 0.875
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getTempDirForThisExecution() 0 10 1
B getTempBaseDir() 0 22 4
A mkdirIfNotExists() 3 10 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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