Completed
Push — master ( 496988...59dfa7 )
by Freek
06:45
created

FileSelection::checkForWildcardPaths()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 6
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 10
rs 9.4285
1
<?php
2
3
namespace Spatie\Backup\Tasks\Backup;
4
5
use Illuminate\Support\Collection;
6
use Symfony\Component\Finder\Finder;
7
use Symfony\Component\Finder\SplFileInfo;
8
9
class FileSelection
10
{
11
    /** @var array */
12
    protected $includeFilesAndDirectories = [];
13
14
    /** @var array */
15
    protected $excludeFilesAndDirectories = [];
16
17
    /**
18
     * @param array|string $includeFilesAndDirectories
19
     *
20
     * @return \Spatie\Backup\Tasks\Backup\FileSelection
21
     */
22
    public static function create($includeFilesAndDirectories = [])
23
    {
24
        return new static($includeFilesAndDirectories);
25
    }
26
27
    /**
28
     * @param array|string $includeFilesAndDirectories
29
     */
30
    public function __construct($includeFilesAndDirectories)
31
    {
32
        if (!is_array($includeFilesAndDirectories)) {
33
            $includeFilesAndDirectories = [$includeFilesAndDirectories];
34
        }
35
36
        $this->includeFilesAndDirectories = $includeFilesAndDirectories;
37
    }
38
39
    /**
40
     * Do not included the given files and directories.
41
     *
42
     * @param array|string $excludeFilesAndDirectories
43
     *
44
     * @return \Spatie\Backup\Tasks\Backup\FileSelection
45
     */
46
    public function excludeFilesFrom($excludeFilesAndDirectories)
47
    {
48
        if (!is_array($excludeFilesAndDirectories)) {
49
            $excludeFilesAndDirectories = [$excludeFilesAndDirectories];
50
        }
51
52
        $this->excludeFilesAndDirectories = $excludeFilesAndDirectories;
53
54
        return $this;
55
    }
56
57
    /**
58
     * @return array
59
     */
60
    public function getSelectedFiles()
61
    {
62
        if (count($this->includeFilesAndDirectories) === 0) {
63
            return [];
64
        }
65
66
        $filesToBeIncluded = $this->getAllFilesFromPaths($this->includeFilesAndDirectories);
67
68
        if (count($this->excludeFilesAndDirectories) === 0) {
69
            return $filesToBeIncluded;
70
        }
71
72
        $filesToBeExcluded = $this->getAllFilesFromPaths($this->excludeFilesAndDirectories);
73
74
        $selectedFiles = collect($filesToBeIncluded)
75
            ->filter(function ($file) use ($filesToBeExcluded) {
76
                return !in_array($file, $filesToBeExcluded);
77
            })
78
            ->toArray();
79
80
        $selectedFiles = array_values($selectedFiles);
81
82
        return $selectedFiles;
83
    }
84
85
    /**
86
     * Make a unique array of all files from a given array of files and directories.
87
     *
88
     * @param array $paths
89
     *
90
     * @return array
91
     */
92
    protected function getAllFilesFromPaths(array $paths)
93
    {
94
        $paths = $this->checkForWildcardPaths($paths);
95
96
        $allFiles = collect($paths)
97
            ->filter(function ($path) {
98
                return file_exists($path);
99
            })
100
101
            ->map(function ($file) {
102
                return realpath($file);
103
            })
104
105
            ->reduce(function (Collection $filePaths, $path) {
106
                if (is_dir($path)) {
107
                    return $filePaths->merge($this->getAllFilesFromDirectory($path));
108
                }
109
110
                return $filePaths->push($path);
111
            }, collect())
112
113
            ->unique()
114
            ->toArray();
115
116
        return $allFiles;
117
    }
118
119
    /**
120
     * Recursively get all the files within a given directory.
121
     *
122
     * @param string $directory
123
     *
124
     * @return array
125
     */
126
    protected function getAllFilesFromDirectory($directory)
127
    {
128
        $finder = (new Finder())
129
            ->ignoreDotFiles(false)
130
            ->ignoreVCS(false)
131
            ->files()
132
            ->in($directory);
133
134
        $filePaths = array_map(function (SplFileInfo $fileInfo) {
135
            return $fileInfo->getPathname();
136
        }, iterator_to_array($finder));
137
138
        $filePaths = array_values($filePaths);
139
140
        return $filePaths;
141
    }
142
143
    /**
144
     * Check all paths in array for a wildcard (*) and build a new array from the results.
145
     *
146
     * @param $paths
147
     *
148
     * @return array
149
     */
150
    private function checkForWildcardPaths($paths)
151
    {
152
        $paths_new = [];
153
        foreach ($paths as $path) {
154
            $paths_new[] = glob($path);
155
        }
156
        $paths_new = array_flatten($paths_new);
157
158
        return $paths_new;
159
    }
160
}
161