Completed
Push — master ( 9b96e2...70e41f )
by Luke
04:58
created

AbstractPlatform::removeTempDirectory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
ccs 0
cts 9
cp 0
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 * Moodle component manager.
5
 *
6
 * @author Luke Carrier <[email protected]>
7
 * @copyright 2016 Luke Carrier
8
 * @license GPL-3.0+
9
 */
10
11
namespace ComponentManager\Platform;
12
13
use ComponentManager\Exception\PlatformException;
14
use Symfony\Component\Filesystem\Filesystem;
15
16
/**
17
 * Abstract platform implementation.
18
 *
19
 * Utility methods for individual platforms.
20
 */
21
abstract class AbstractPlatform implements Platform {
22
    /**
23
     * Temporary file/directory prefix.
24
     *
25
     * @var string
26
     */
27
    const TEMP_PREFIX = 'componentmgr-';
28
29
    /**
30
     * Temporary directories.
31
     *
32
     * @var string[]
33
     */
34
    protected $tempDirectories;
35
36
    /**
37
     * Initialiser.
38
     *
39
     * @param \Symfony\Component\Filesystem\Filesystem $filesystem
40
     */
41 5
    public function __construct(Filesystem $filesystem) {
42 5
        $this->filesystem = $filesystem;
0 ignored issues
show
Bug introduced by
The property filesystem does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
43 5
    }
44
45
    /**
46
     * @override \ComponentManager\Platform\Platform
47
     */
48
    public function createTempDirectory() {
49
        $root      = sys_get_temp_dir();
50
        $directory = tempnam($root, static::TEMP_PREFIX);
51
52
        unlink($directory);
53
        mkdir($directory);
54
55
        $this->tempDirectories[] = $directory;
56
57
        return $directory;
58
    }
59
60
    /**
61
     * @override \ComponentManager\Platform\Platform
62
     */
63 4
    public function getDirectorySeparator() {
64 4
        return DIRECTORY_SEPARATOR;
65
    }
66
67
    /**
68
     * @override \ComponentManager\Platform\Platform
69
     */
70
    public function getWorkingDirectory() {
71
        return getcwd();
72
    }
73
74
    /**
75
     * @override \ComponentManager\Platform\Platform
76
     */
77 3
    public function joinPaths($parts) {
78 3
        return implode($this->getDirectorySeparator(), $parts);
79
    }
80
81
    /**
82
     * @override \ComponentManager\Platform\Platform
83
     */
84
    public function getPhpExecutable() {
85
        return PHP_BINARY;
86
    }
87
88
    /**
89
     * @override \ComponentManager\Platform\Platform
90
     */
91
    public function getPhpScript() {
92
        return $_SERVER['argv'][0];
93
    }
94
95
    /**
96
     * @override \ComponentManager\Platform\Platform
97
     */
98
    public function removeTempDirectories() {
99
        $this->filesystem->remove($this->tempDirectories);
100
    }
101
102
    /**
103
     * @override \ComponentManager\Platform\Platform
104
     */
105
    public function removeTempDirectory($directory) {
106
        $index = array_search($directory, $this->tempDirectories, true);
107
108
        if ($index === false) {
109
            throw new PlatformException(
110
                    sprintf('Directory "%s" is not a known temporary directory', $directory),
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 80 characters; contains 93 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
111
                    PlatformException::CODE_UNKNOWN_TEMP_DIRECTORY);
112
        }
113
114
        $this->filesystem->remove($directory);
115
        unset($this->tempDirectories[$index]);
116
    }
117
}
118