Completed
Push — master ( 0545e6...9b96e2 )
by Luke
08:54
created

AbstractPlatform::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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 Symfony\Component\Filesystem\Filesystem;
14
15
/**
16
 * Abstract platform implementation.
17
 *
18
 * Utility methods for individual platforms.
19
 */
20
abstract class AbstractPlatform {
21
    /**
22
     * Temporary directories.
23
     *
24
     * @var string[]
25
     */
26
    protected $tempDirectories;
27
28
    /**
29
     * Temporary file/directory prefix.
30
     *
31
     * @var string
32
     */
33
    const TEMP_PREFIX = 'componentmgr-';
34
35
    /**
36
     * Initialiser.
37
     *
38
     * @param \Symfony\Component\Filesystem\Filesystem $filesystem
39
     */
40 5
    public function __construct(Filesystem $filesystem) {
41 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...
42 5
    }
43
44
    /**
45
     * Destructor.
46
     *
47
     * @return void
48
     */
49
    public function __destruct() {
50
        $this->removeTempDirectories();
51
    }
52
53
    /**
54
     * @override \ComponentManager\Platform\Platform
55
     */
56
    public function createTempDirectory() {
57
        $root      = sys_get_temp_dir();
58
        $directory = tempnam($root, static::TEMP_PREFIX);
59
60
        unlink($directory);
61
        mkdir($directory);
62
63
        $this->tempDirectories[] = $directory;
64
65
        return $directory;
66
    }
67
68
    /**
69
     * @override \ComponentManager\Platform\Platform
70
     */
71 4
    public function getDirectorySeparator() {
72 4
        return DIRECTORY_SEPARATOR;
73
    }
74
75
    /**
76
     * @override \ComponentManager\Platform\Platform
77
     */
78
    public function getWorkingDirectory() {
79
        return getcwd();
80
    }
81
82
    /**
83
     * @override \ComponentManager\Platform\Platform
84
     */
85 3
    public function joinPaths($parts) {
86 3
        return implode($this->getDirectorySeparator(), $parts);
87
    }
88
89
    /**
90
     * @override \ComponentManager\Platform\Platform
91
     */
92
    public function getPhpExecutable() {
93
        return PHP_BINARY;
94
    }
95
96
    /**
97
     * @override \ComponentManager\Platform\Platform
98
     */
99
    public function getPhpScript() {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
100
        return $_SERVER['argv'][0];
101
    }
102
103
    /**
104
     * @override \ComponentManager\Platform\Platform
105
     */
106
    public function removeTempDirectories() {
107
        $this->filesystem->remove($this->tempDirectories);
108
    }
109
}
110