Finder::read()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 2
eloc 9
nc 2
nop 1
1
<?php
2
3
namespace Vinelab\Cdn;
4
5
use Illuminate\Support\Collection;
6
use Symfony\Component\Console\Output\ConsoleOutput;
7
use Symfony\Component\Finder\Finder as SymfonyFinder;
8
use Vinelab\Cdn\Contracts\AssetInterface;
9
use Vinelab\Cdn\Contracts\FinderInterface;
10
11
/**
12
 * Class Finder.
13
 *
14
 * @category Finder Helper
15
 *
16
 * @author  Mahmoud Zalt <[email protected]>
17
 */
18
class Finder extends SymfonyFinder implements FinderInterface
19
{
20
    /**
21
     * @var \Symfony\Component\Console\Output\ConsoleOutput
22
     */
23
    protected $console;
24
25
    /**
26
     * @param ConsoleOutput $console
27
     */
28
    public function __construct(ConsoleOutput $console)
29
    {
30
        $this->console = $console;
31
32
        parent::__construct();
33
    }
34
35
    /**
36
     * return a collection of arrays of assets paths found
37
     * in the included directories, except all ignored
38
     * (directories, patterns, extensions and files).
39
     *
40
     * @param AssetInterface $asset_holder
41
     *
42
     * @return Collection
43
     */
44
    public function read(AssetInterface $asset_holder)
45
    {
46
        /*
47
         * add the included directories and files
48
         */
49
        $this->includeThis($asset_holder);
50
        /*
51
         * exclude the ignored directories and files
52
         */
53
        $this->excludeThis($asset_holder);
54
55
        // user terminal message
56
        $this->console->writeln('<fg=yellow>Files to upload:</fg=yellow>');
57
58
        // get all allowed 'for upload' files objects (assets) and store them in an array
59
        $assets = [];
60
        foreach ($this->files() as $file) {
61
            // user terminal message
62
            $this->console->writeln('<fg=cyan>'.'Path: '.$file->getRealpath().'</fg=cyan>');
63
64
            $assets[] = $file;
65
        }
66
67
        return new Collection($assets);
68
    }
69
70
    /**
71
     * Add the included directories and files.
72
     *
73
     * @param AssetInterface $asset_holder
74
     */
75
    private function includeThis(AssetInterface $asset_holder)
76
    {
77
78
        // include the included directories
79
        $this->in($asset_holder->getIncludedDirectories());
80
81
        // include files with this extensions
82
        foreach ($asset_holder->getIncludedExtensions() as $extension) {
83
            $this->name('*'.$extension);
84
        }
85
86
        // include patterns
87
        foreach ($asset_holder->getIncludedPatterns() as $pattern) {
88
            $this->name($pattern);
89
        }
90
91
        // exclude ignored directories
92
        $this->exclude($asset_holder->getExcludedDirectories());
93
    }
94
95
    /**
96
     * exclude the ignored directories and files.
97
     *
98
     * @param AssetInterface $asset_holder
99
     */
100
    private function excludeThis(AssetInterface $asset_holder)
101
    {
102
        // add or ignore hidden directories
103
        $this->ignoreDotFiles($asset_holder->getExcludeHidden());
104
105
        // exclude ignored files
106
        foreach ($asset_holder->getExcludedFiles() as $name) {
107
            $this->notName($name);
108
        }
109
110
        // exclude files (if exist) with this extensions
111
        $excluded_extensions = $asset_holder->getExcludedExtensions();
112
        if (!empty($excluded_extensions)) {
113
            foreach ($asset_holder->getExcludedExtensions() as $extension) {
114
                $this->notName('*'.$extension);
115
            }
116
        }
117
118
        // exclude the regex pattern
119
        foreach ($asset_holder->getExcludedPatterns() as $pattern) {
120
            $this->notName($pattern);
121
        }
122
    }
123
}
124