Completed
Pull Request — master (#161)
by Sebastian
03:49
created

FileSelection::getAllFilesFromPaths()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 1
Metric Value
cc 2
eloc 12
c 4
b 0
f 1
nc 1
nop 1
dl 0
loc 18
rs 9.4285

1 Method

Rating   Name   Duplication   Size   Complexity  
A FileSelection::includedFiles() 0 6 1
1
<?php
2
3
namespace Spatie\Backup\Tasks\Backup;
4
5
use Symfony\Component\Finder\Finder;
6
7
class FileSelection
8
{
9
    /** @var \Illuminate\Support\Collection */
10
    protected $includeFilesAndDirectories;
11
12
    /** @var \Illuminate\Support\Collection */
13
    protected $excludeFilesAndDirectories;
14
15
    /** @var bool */
16
    protected $shouldFollowLinks = false;
17
18
    /**
19
     * @param array|string $includeFilesAndDirectories
20
     *
21
     * @return \Spatie\Backup\Tasks\Backup\FileSelection
22
     */
23
    public static function create($includeFilesAndDirectories = [])
24
    {
25
        return new static($includeFilesAndDirectories);
26
    }
27
28
    /**
29
     * @param array|string $includeFilesAndDirectories
30
     */
31
    public function __construct($includeFilesAndDirectories)
32
    {
33
        $this->includeFilesAndDirectories = $this->createPathCollection($includeFilesAndDirectories);
34
35
        $this->excludeFilesAndDirectories = collect();
36
    }
37
38
    /**
39
     * Do not included the given files and directories.
40
     *
41
     * @param array|string $excludeFilesAndDirectories
42
     *
43
     * @return \Spatie\Backup\Tasks\Backup\FileSelection
44
     */
45
    public function excludeFilesFrom($excludeFilesAndDirectories)
46
    {
47
        $this->excludeFilesAndDirectories = $this->createPathCollection($excludeFilesAndDirectories);
48
49
        return $this;
50
    }
51
52
    /**
53
     * Enable or disable the following of symlinks.
54
     *
55
     * @param bool $shouldFollowLinks
56
     *
57
     * @return \Spatie\Backup\Tasks\Backup\FileSelection
58
     */
59
    public function shouldFollowLinks($shouldFollowLinks)
60
    {
61
        $this->shouldFollowLinks = $shouldFollowLinks;
62
63
        return $this;
64
    }
65
66
    /**
67
     * @return Generator|string
68
     */
69
    public function getSelectedFiles()
70
    {
71
        if ($this->includeFilesAndDirectories->isEmpty()) {
72
            return;
73
        }
74
75
        $finder = (new Finder())
76
            ->ignoreDotFiles(false)
77
            ->ignoreVCS(false)
78
            ->files();
79
80
        if ($this->shouldFollowLinks) {
81
            $finder->followLinks();
82
        }
83
84
        $finder->in($this->includedDirectories());
85
86
        foreach ($this->includedFiles() as $includedFile) {
87
            yield $includedFile;
88
        }
89
90
        foreach ($finder->getIterator() as $file) {
91
92
            if ($this->shouldExclude($file)) {
93
                continue;
94
            }
95
96
            yield $file->getPathname();
97
        }
98
    }
99
100
    /**
101
     * @return array
102
     */
103
    protected function includedFiles()
104
    {
105
        return $this->includeFilesAndDirectories->filter(function ($path) {
106
            return is_file($path);
107
        })->toArray();
108
    }
109
110
    /**
111
     * @return array
112
     */
113
    protected function includedDirectories()
114
    {
115
        return $this->includeFilesAndDirectories->reject(function ($path) {
116
            return is_file($path);
117
        })->toArray();
118
    }
119
120
    /**
121
     * @param string $path
122
     *
123
     * @return bool
124
     */
125
    protected function shouldExclude($path)
126
    {
127
        return $this->excludeFilesAndDirectories->contains(function ($key, $excludedPath) use ($path) {
128
            return starts_with($path, $excludedPath);
129
        });
130
    }
131
132
    /**
133
     * @param $paths
134
     *
135
     * @return static
136
     */
137
    protected function createPathCollection($paths)
138
    {
139
        return collect($paths)
140
            ->reject(function ($path) {
141
                return $path == '';
142
            })
143
            ->flatMap(function ($path) {
144
                return glob($path);
145
            })
146
            ->map(function ($path) {
147
                return realpath($path);
148
            })
149
            ->reject(function ($path) {
150
                return $path === false;
151
            });
152
    }
153
}
154