Completed
Push — master ( f62f21...f72c97 )
by Freek
05:09
created

FileSelection::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
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
        $allFiles = collect($paths)
95
            ->filter(function ($path) {
96
                return file_exists($path);
97
            })
98
99
            ->map(function ($file) {
100
                return realpath($file);
101
            })
102
103
            ->reduce(function (Collection $filePaths, $path) {
104
                if (is_dir($path)) {
105
                    return $filePaths->merge($this->getAllFilesFromDirectory($path));
106
                }
107
108
                return $filePaths->push($path);
109
            }, collect())
110
111
            ->unique()
112
            ->toArray();
113
114
        return $allFiles;
115
    }
116
117
    /**
118
     * Recursively get all the files within a given directory.
119
     *
120
     * @param string $directory
121
     *
122
     * @return array
123
     */
124
    protected function getAllFilesFromDirectory($directory)
125
    {
126
        $finder = (new Finder())
127
            ->ignoreDotFiles(false)
128
            ->ignoreVCS(false)
129
            ->files()
130
            ->in($directory);
131
132
        $filePaths = array_map(function (SplFileInfo $fileInfo) {
133
            return $fileInfo->getPathname();
134
        }, iterator_to_array($finder));
135
136
        $filePaths = array_values($filePaths);
137
138
        return $filePaths;
139
    }
140
}
141