Path::getMode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php namespace KamranAhmed\Smasher;
2
3
use KamranAhmed\Smasher\Exceptions\InvalidPathException;
4
use KamranAhmed\Smasher\Exceptions\UnreadablePathException;
5
6
/**
7
 * Path
8
 *
9
 * Responsible for managing everything related to Paths
10
 */
11
class Path
12
{
13
    /**
14
     * @var string Path which is being processed
15
     */
16
    private $path;
17
18
    /**
19
     * Path constructor.
20
     *
21
     * @param string $path
22
     */
23 17
    public function __construct($path = '')
24
    {
25 17
        $this->path = $path;
26 17
    }
27
28
    /**
29
     * Validates whether the path exists or not
30
     *
31
     * @throws \KamranAhmed\Smasher\Exceptions\UnreadablePathException
32
     * @return bool
33
     */
34 9
    public function validate()
35
    {
36 9
        if (!is_readable($this->path)) {
37 2
            throw new UnreadablePathException("Unable to read the path" . $this->path);
38
        }
39
40 7
        return true;
41
    }
42
43
    /**
44
     * Writes content to the specified path
45
     *
46
     * @param $content
47
     * @throws \KamranAhmed\Smasher\Exceptions\InvalidPathException
48
     * @return int
49
     */
50 3
    public function saveFileContent($content)
51
    {
52 3
        if (!file_exists(dirname($this->path))) {
53
            mkdir(dirname($this->path), 0777, true);
54 3
        } elseif (is_dir($this->path)) {
55 1
            throw new InvalidPathException("You can only write content to file");
56
        }
57
58 2
        return file_put_contents($this->path, $content);
59
    }
60
61
    /**
62
     * Gets the path which is being operated
63
     * @return string
64
     */
65 2
    public function getPath()
66
    {
67 2
        return $this->path;
68
    }
69
70
    /**
71
     * Sets the path to operate on
72
     * @param $path
73
     */
74 9
    public function setPath($path)
75
    {
76 9
        $this->path = $path;
77 9
    }
78
79
    /**
80
     * Creates the path based upon the type of it
81
     * @param array $detail
82
     */
83 4
    public function createItem($detail = [])
84
    {
85
        // Default options
86 4
        $defaults = ['@type' => 'dir'];
87 4
        $detail   = array_merge($defaults, $detail);
88
89 4
        $old = umask(0);
90
91 4
        $type = $detail['@type'];
92
93 4
        if ($type === 'dir' && !file_exists($this->path)) {
94 2
            mkdir($this->path, 0777, true);
95 4
        } elseif ($type === 'file') {
96 2
            $content = $detail['@content'];
97
98 2
            $handle = fopen($this->path, "wb");
99 2
            fwrite($handle, $content);
100 2
            fclose($handle);
101
102 3
        } elseif ($type === 'link') {
103 1
            $target = $detail['@destination'];
104 1
            $link   = $this->path;
105
106 1
            symlink($target, $link);
107 1
        }
108
109 4
        umask($old);
110 4
    }
111
112
    /**
113
     * Gets the detail about the currently set path
114
     *
115
     * @return array
116
     */
117 6
    public function getDetail()
118
    {
119 6
        $pathDetail = [];
120
121 6
        $pathDetail['@name']          = $this->getName();
122 6
        $pathDetail['@path']          = $this->path;
123 6
        $pathDetail['@type']          = $this->getType();
124 6
        $pathDetail['@size']          = $this->getSize();
125 6
        $pathDetail['@mode']          = $this->getMode();
126 6
        $pathDetail['@owner']         = $this->getOwner();
127 6
        $pathDetail['@last_modified'] = $this->getLastModified();
128 6
        $pathDetail['@group']         = $this->getGroup();
129
130 6
        if ($pathDetail['@type'] === 'link') {
131
            // Save the destination of this symlink
132 1
            $pathDetail['@destination'] = $this->getRealPath();
133 6
        } elseif ($pathDetail['@type'] === 'file') {
134
            // If it was a file, put the contents
135 4
            $pathDetail['@content'] = $this->getFileContent();
136 4
        }
137
138 6
        return $pathDetail;
139
    }
140
141
    /**
142
     * Gets the name of the currently set path
143
     *
144
     * @return mixed
145
     */
146 6
    public function getName()
147
    {
148 6
        $parts = explode('/', $this->path);
149
150 6
        return array_pop($parts);
151
    }
152
153
    /**
154
     * Gets the type of path i.e. whether it is file, link or dir
155
     *
156
     * @return string
157
     */
158 6
    public function getType()
159
    {
160 6
        if (is_file($this->path)) {
161 4
            return 'file';
162 5
        } elseif (is_link($this->path)) {
163 1
            return 'link';
164 4
        } elseif (is_dir($this->path)) {
165 4
            return 'dir';
166
        }
167
168
        return "Unknown";
169
    }
170
171
    /**
172
     * Returns the size of the path in bytes
173
     *
174
     * @return int
175
     */
176 6
    public function getSize()
177
    {
178 6
        return filesize($this->path);
179
    }
180
181
    /**
182
     * Returns the permission that the path has
183
     *
184
     * @return string
185
     */
186 6
    public function getMode()
187
    {
188 6
        return substr(sprintf('%o', fileperms($this->path)), -4);
189
    }
190
191
    /**
192
     * Returns the detail of the path owner
193
     *
194
     * @return array
195
     */
196 6
    public function getOwner()
197
    {
198 6
        return posix_getpwuid(fileowner($this->path));
199
    }
200
201
    /**
202
     * Gets the last date when the path was modified
203
     *
204
     * @return string
205
     */
206 6
    public function getLastModified()
207
    {
208 6
        return gmdate('Y-m-d H:i:s', filemtime($this->path));
209
    }
210
211
    /**
212
     * Returns the detail of the path group
213
     *
214
     * @return array
215
     */
216 6
    public function getGroup()
217
    {
218 6
        return posix_getgrgid(filegroup($this->path));
219
    }
220
221
    /**
222
     * Gets the real path for the path
223
     *
224
     * @return string
225
     */
226 3
    public function getRealPath()
227
    {
228 3
        return realpath($this->path);
229
    }
230
231
    /**
232
     * If the set path is file, it returns the contents of the file
233
     *
234
     * @return string
235
     */
236 8
    public function getFileContent()
237
    {
238 8
        return file_get_contents($this->path);
239
    }
240
}
241