Test Failed
Pull Request — master (#3)
by MediaCT
03:44
created

Filesystem::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Copyright MediaCT. All rights reserved.
4
 * https://www.mediact.nl
5
 */
6
7
namespace Mediact\CodingStandard\PhpStorm;
8
9
use RecursiveDirectoryIterator;
10
use RecursiveIteratorIterator;
11
use RuntimeException;
12
use SplFileInfo;
13
14
class Filesystem implements FilesystemInterface
15
{
16
    /**
17
     * @var string
18
     */
19
    private $root;
20
21
    /**
22
     * Constructor.
23
     *
24
     * @param string $root
25
     */
26
    public function __construct($root)
27
    {
28
        $this->root = rtrim($root, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
29
    }
30
31
    /**
32
     * Check whether a file exists.
33
     *
34
     * @param string $path
35
     *
36
     * @return bool
37
     */
38
    public function has($path)
39
    {
40
        return file_exists($this->getPath($path));
41
    }
42
43
    /**
44
     * Read a path.
45
     *
46
     * @param string $path
47
     *
48
     * @return string
49
     *
50
     * @throws RuntimeException When the path is not readable.
51
     */
52
    public function read($path)
53
    {
54
        $path = $this->getPath($path);
55
        if (!is_readable($path) || !is_file($path)) {
56
            throw new RuntimeException($path . ' is not readable file');
57
        }
58
        return file_get_contents($path);
59
    }
60
61
    /**
62
     * Write contents to a path.
63
     *
64
     * @param string $path
65
     * @param string $contents
66
     *
67
     * @return bool
68
     *
69
     * @throws RuntimeException When the path is not writable.
70
     */
71
    public function put($path, $contents)
72
    {
73
        $directory = dirname($path);
74
        $this->createDir($directory);
75
        $path = $this->getPath($path);
76
        if (!file_exists($path) && !is_writable(dirname($path))) {
77
            throw new RuntimeException(dirname($path) . ' is not writable');
78
        }
79
        if (file_exists($path) && !is_writable($path)) {
80
            throw new RuntimeException($path . ' is not writable');
81
        }
82
        return file_put_contents($path, $contents);
0 ignored issues
show
Bug Best Practice introduced by
The expression return file_put_contents($path, $contents) returns the type integer which is incompatible with the documented return type boolean.
Loading history...
83
    }
84
85
    /**
86
     * Create a directory if it does not exist.
87
     *
88
     * @param string $path
89
     *
90
     * @return bool
91
     *
92
     * @throws RuntimeException When the directory can not be created.
93
     */
94
    public function createDir($path)
95
    {
96
        $directory = $this->getPath($path);
97
        if (!is_dir($directory)) {
98
            if (file_exists($directory)) {
99
                throw new RuntimeException(
100
                    $directory . ' is not a directory.'
101
                );
102
            }
103
            if (!mkdir($directory, 0777, true)) {
104
                throw new RuntimeException(
105
                    $directory . ' can not be created.'
106
                );
107
            }
108
        }
109
        return true;
110
    }
111
112
    /**
113
     * List contents of a directory.
114
     *
115
     * @param string $path
116
     *
117
     * @return array
118
     *
119
     * @throws RuntimeException When the path is not a directory.
120
     */
121
    public function listFiles($path = '')
122
    {
123
        $directory = $this->getPath($path);
124
        if (!is_dir($directory)) {
125
            throw new RuntimeException(
126
                $directory . ' is not a directory.'
127
            );
128
        }
129
130
        $iterator = new RecursiveIteratorIterator(
131
            new RecursiveDirectoryIterator(
132
                $this->getPath($path),
133
                RecursiveDirectoryIterator::SKIP_DOTS
134
            ),
135
            RecursiveIteratorIterator::SELF_FIRST
136
        );
137
138
        $files = [];
139
140
        /** @var SplFileInfo $fileInfo */
141
        foreach ($iterator as $fileInfo) {
142
            if ($fileInfo->isDir()) {
143
                continue;
144
            }
145
            $files[] = preg_replace(
146
                sprintf('/^%s/', preg_quote($this->root, '/')),
147
                '',
148
                $fileInfo->getPathName()
149
            );
150
        }
151
152
        return $files;
153
    }
154
155
    /**
156
     * Get the full path.
157
     *
158
     * @param string $path
159
     *
160
     * @return string
161
     */
162
    private function getPath($path)
163
    {
164
        return $this->root .
165
            ltrim(
166
                preg_replace(
167
                    sprintf('|%s{2,}|', preg_quote(DIRECTORY_SEPARATOR)),
168
                    DIRECTORY_SEPARATOR,
169
                    $path
170
                ),
171
                DIRECTORY_SEPARATOR
172
            );
173
    }
174
}
175