BackupFilesystem::locateCommand()   B
last analyzed

Complexity

Conditions 7
Paths 13

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.5706
c 0
b 0
f 0
cc 7
nc 13
nop 1
1
<?php
2
3
namespace Cornford\Backup;
4
5
use Cornford\Backup\Contracts\BackupFilesystemInterface;
6
7
class BackupFilesystem implements BackupFilesystemInterface
8
{
9
    private const OS_UNKNOWN = 1;
10
    private const OS_WIN = 2;
11
    private const OS_LINUX = 3;
12
    private const OS_OSX = 4;
13
14
    /**
15
     * Ignored files.
16
     *
17
     * @var array
18
     */
19
    public static $ignoredFiles = [
20
        '.gitignore',
21
        '.gitkeep'
22
    ];
23
24
    /**
25
     * Check path exists.
26
     *
27
     * @param string $path
28
     *
29
     * @return bool
30
     */
31
    public function checkPathExists($path): bool
32
    {
33
        if (!is_dir($path)) {
34
            return false;
35
        }
36
37
        return true;
38
    }
39
40
    /**
41
     * Check file exists.
42
     *
43
     * @param string $filepath
44
     *
45
     * @return bool
46
     */
47
    public function checkFileExists($filepath): bool
48
    {
49
        if (!$this->checkPathExists(dirname($filepath))) {
50
            return false;
51
        }
52
53
        if (!is_file($filepath)) {
54
            return false;
55
        }
56
57
        return true;
58
    }
59
60
    /**
61
     * Check if file is empty.
62
     *
63
     * @param string $filepath
64
     *
65
     * @return bool
66
     */
67
    public function checkFileEmpty($filepath): bool
68
    {
69
        return (filesize($filepath) == 0);
70
    }
71
72
    /**
73
     * Remove file.
74
     *
75
     * @param string $filepath
76
     *
77
     * @return void
78
     */
79
    public function removeFile($filepath): void
80
    {
81
        @unlink($filepath);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
82
    }
83
84
    /**
85
     * Write a compressed file.
86
     *
87
     * @param string $compressedFilepath
88
     * @param string $filepath
89
     *
90
     * @return void
91
     */
92
    public function writeCompressedFile($compressedFilepath, $filepath): void
93
    {
94
        $filePointer = fopen($compressedFilepath, 'w');
95
        fwrite($filePointer, gzencode(file_get_contents($filepath), 9));
96
        fclose($filePointer);
97
    }
98
99
    /**
100
     * Write an uncompressed file.
101
     *
102
     * @param string $uncompressedFilepath
103
     * @param string $filepath
104
     *
105
     * @return void
106
     */
107
    public function writeUncompressedFile($uncompressedFilepath, $filepath): void
108
    {
109
        $filePointer = fopen($uncompressedFilepath, 'w');
110
        fwrite($filePointer, gzdecode(file_get_contents($filepath, 9)));
111
        fclose($filePointer);
112
    }
113
114
    /**
115
     * Get the operating system.
116
     *
117
     * @return int
118
     */
119
    public function getOperatingSystem(): int
120
    {
121
        switch (true) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing stristr(PHP_OS, 'DAR') of type string to the boolean true. If you are specifically checking for a non-empty string, consider using the more explicit !== '' instead.
Loading history...
Bug Best Practice introduced by
It seems like you are loosely comparing stristr(PHP_OS, 'WIN') of type string to the boolean true. If you are specifically checking for a non-empty string, consider using the more explicit !== '' instead.
Loading history...
Bug Best Practice introduced by
It seems like you are loosely comparing stristr(PHP_OS, 'LINUX') of type string to the boolean true. If you are specifically checking for a non-empty string, consider using the more explicit !== '' instead.
Loading history...
122
            case stristr(PHP_OS, 'DAR'):
123
                return self::OS_OSX;
124
            case stristr(PHP_OS, 'WIN'):
125
                return self::OS_WIN;
126
            case stristr(PHP_OS, 'LINUX'):
127
                return self::OS_LINUX;
128
            default:
129
                return self::OS_UNKNOWN;
130
        }
131
    }
132
133
    /**
134
     * Locate command location.
135
     *
136
     * @param string $command
137
     *
138
     * @return string|null
139
     */
140
    public function locateCommand($command): ?string
141
    {
142
        switch ($this->getOperatingSystem()) {
143
            case self::OS_OSX:
144
            case self::OS_LINUX:
145
                exec(sprintf('/usr/bin/which %s', $command), $result, $returnCode);
146
                if (isset($result[0])) {
147
                    $result = $result[0];
148
                }
149
                break;
150
            case self::OS_WIN:
151
                exec(sprintf('where %s', $command), $result, $returnCode);
152
                if (isset($result[0])) {
153
                    $result = $result[0];
154
                }
155
                break;
156
            default:
157
                return null;
158
        }
159
160
        if (empty($result)) {
161
            return null;
162
        }
163
164
        return $result;
165
    }
166
167
    /**
168
     * Check a function exists.
169
     *
170
     * @param string $function
171
     *
172
     * @return bool
173
     */
174
    public function checkFunctionExists($function): bool
175
    {
176
        return function_exists($function);
177
    }
178
}
179