Completed
Push — master ( cfe763...4f0a48 )
by Vitaly
03:14
created

FileManager   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 61.9%

Importance

Changes 6
Bugs 2 Features 3
Metric Value
c 6
b 2
f 3
dl 0
loc 141
ccs 26
cts 42
cp 0.619
rs 10
wmc 14
lcom 0
cbo 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 4 1
A write() 0 9 2
A mkdir() 0 11 3
A exists() 0 4 1
A touch() 0 5 1
A remove() 0 15 4
A lastModified() 0 4 1
A scan() 0 21 1
1
<?php
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 10.07.16 at 15:40
5
 */
6
namespace samsonphp\resource;
7
8
/**
9
 * File system management class.
10
 * @package samsonphp\resource
11
 */
12
class FileManager implements FileManagerInterface
13
{
14
    /**
15
     * Wrapper for reading file.
16
     *
17
     * @param string $file Full path to file
18
     *
19
     * @return string Asset content
20
     */
21 1
    public function read($file)
22
    {
23 1
        return file_get_contents($file);
24
    }
25
26
    /**
27
     * Wrapper for writing file.
28
     *
29
     * @param string $asset   Full path to file
30
     *
31
     * @param string $content Asset content
32
     */
33 1
    public function write($asset, $content)
34
    {
35 1
        $path = dirname($asset);
36 1
        if (!file_exists($path)) {
37 1
            $this->mkdir($path);
38 1
        }
39
40 1
        file_put_contents($asset, $content);
41 1
    }
42
43
    /**
44
     * Create folder.
45
     *
46
     * @param string $path Full path to asset
47
     *
48
     * @throws \Exception
49
     */
50 1
    public function mkdir($path)
51
    {
52
        // Create cache path
53 1
        if (!$this->exists($path)) {
54
            try {
55 1
                mkdir($path, 0777, true);
56 1
            } catch (\Exception $e) {
57
                throw new \Exception($e->getMessage() . ' ' . $path);
58
            }
59 1
        }
60 1
    }
61
62
    /**
63
     * If path(file or folder) exists.
64
     *
65
     * @param string $path Path for validating existence
66
     *
67
     * @return bool True if path exists
68
     */
69 1
    public function exists($path)
70
    {
71 1
        return file_exists($path);
72
    }
73
74
    /**
75
     * Wrapper for touching file.
76
     *
77
     * @param string $asset     Full path to file
78
     * @param int    $timestamp Timestamp
79
     */
80
    public function touch($asset, $timestamp)
81
    {
82
        // Sync cached file with source file
83
        touch($asset, $timestamp);
84
    }
85
86
    /**
87
     * Remove path/file recursively.
88
     *
89
     * @param string $path Path to be removed
90
     */
91
    public function remove($path)
92
    {
93
        if (is_dir($path)) {
94
            // Get folder content
95
            foreach (glob($path . '*', GLOB_MARK) as $file) {
96
                // Recursion
97
                $this->remove($file);
98
            }
99
100
            // Remove folder after all internal sub-folders are clear
101
            rmdir($path);
102
        } elseif (is_file($path)) {
103
            unlink($path);
104
        }
105
    }
106
107
    /**
108
     * Get last file modification timestamp.
109
     *
110
     * @param string $file Path to file
111
     *
112
     * @return int File modification timestamp
113
     */
114
    public function lastModified($file)
115
    {
116
        return filemtime($file);
117
    }
118
119
    /**
120
     * Recursively scan collection of paths to find files with passed
121
     * extensions. Method is based on linux find command so this method
122
     * can face difficulties on other OS.
123
     *
124
     *
125
     * @param array $paths          Paths for files scanning
126
     * @param array $extensions     File extension filter
127
     * @param array $excludeFolders Path patterns for excluding
128
     *
129
     * @return array Found files
130
     */
131 1
    public function scan(array $paths, array $extensions, array $excludeFolders = [])
132
    {
133
        // Generate LINUX command to gather resources as this is 20 times faster
134 1
        $files = [];
135
136
        // Generate exclusion conditions
137
        $exclude = implode(' ', array_map(function ($value) {
138 1
            return '-not -path ' . $value . ' ';
139 1
        }, $excludeFolders));
140
141
        // Generate filters
142 1
        $filters = implode('-o ', array_map(function ($value) use ($exclude) {
143 1
            return '-name "*.' . $value . '" ' . $exclude;
144 1
        }, $extensions));
145
146
        // Scan path excluding folder patterns
147 1
        exec('find ' . implode(' ', $paths) . ' -type f ' . $filters, $files);
148
149
        // TODO: Why some paths have double slashes? Investigate speed of realpath, maybe // changing if quicker
150 1
        return array_map('realpath', $files);
151
    }
152
}
153