Find::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Console\Console\Commands;
6
7
use Exception;
8
use Illuminate\Console\Command;
9
use Symfony\Component\Finder\Finder;
10
use Illuminate\Filesystem\Filesystem;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Input\StringInput;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
class Find extends Command
18
{
19
    /**
20
     * The console command name.
21
     *
22
     * @var string
23
     */
24
    protected $name = 'find';
25
26
    /**
27
     * The console command description.
28
     *
29
     * @var string
30
     */
31
    protected $description = 'search for files in a directory hierarchy';
32
33
    /**
34
     * $finder.
35
     *
36
     * @var \Symfony\Component\Finder\Finder
37
     */
38
    protected $finder;
39
40
    /**
41
     * $files.
42
     *
43
     * @var \Illuminate\Filesystem\Filesystem
44
     */
45
    protected $files;
46
47
    /**
48
     * __construct.
49
     *
50
     * @param \Symfony\Component\Finder\Finder  $finder
51
     * @param \Illuminate\Filesystem\Filesystem $files
52
     */
53
    public function __construct(Finder $finder, Filesystem $files)
54
    {
55
        parent::__construct();
56
57
        $this->finder = $finder;
58
        $this->files = $files;
59
    }
60
61
    /**
62
     * run.
63
     *
64
     * @param \Symfony\Component\Console\Input\InputInterface   $input
65
     * @param \Symfony\Component\Console\Output\OutputInterface $output
66
     *
67
     * @return int
68
     */
69
    public function run(InputInterface $input, OutputInterface $output): int
70
    {
71
        $command = (string) $input;
72
        $command = strtr($command, [
73
            ' -name' => ' -N',
74
            ' -type' => ' -T',
75
            ' -maxdepth' => ' -M',
76
            ' -delete' => ' -d true',
77
        ]);
78
79
        return parent::run(new StringInput($command), $output);
80
    }
81
82
    /**
83
     * fire.
84
     */
85
    public function handle(): void
86
    {
87
        $path = $this->argument('path');
88
        $name = $this->option('name');
89
        $type = $this->option('type');
90
        $maxDepth = $this->option('maxdepth');
91
        $delete = filter_var($this->option('delete'), FILTER_VALIDATE_BOOLEAN);
92
93
        $root = function_exists('base_path') === true ? base_path() : getcwd();
94
        $path = rtrim($root, '/').'/'.$path;
95
96
        $this->finder->in($path);
97
98
        if ($name !== null) {
99
            $this->finder->name($name);
100
        }
101
102
        switch ($type) {
103
            case 'd':
104
                $this->finder->directories();
105
                break;
106
            case 'f':
107
                $this->finder->files();
108
                break;
109
        }
110
111
        if (is_null($maxDepth) === false) {
112
            if ($maxDepth === '0') {
113
                $this->line($path);
114
115
                return;
116
            }
117
            $this->finder->depth('<'.$maxDepth);
118
        }
119
120
        foreach ($this->finder->getIterator() as $file) {
121
            $realPath = $file->getRealpath();
122
            if ($delete === true && $this->files->exists($realPath) === true) {
123
                $removed = false;
124
125
                try {
126
                    if ($this->files->isDirectory($realPath) === true) {
127
                        $removed = $this->files->deleteDirectory($realPath);
128
                    } else {
129
                        $removed = $this->files->delete($realPath);
130
                    }
131
                } catch (Exception $e) {
132
                    $removed = false;
133
                }
134
                $removed === true ? $this->info('removed '.$realPath) : $this->error('removed '.$realPath.' fail');
135
            } else {
136
                $this->line($realPath);
137
            }
138
        }
139
    }
140
141
    /**
142
     * Get the console command arguments.
143
     *
144
     * @return array
145
     */
146
    protected function getArguments(): array
147
    {
148
        return [
149
            ['path', InputArgument::REQUIRED, 'path'],
150
        ];
151
    }
152
153
    /**
154
     * Get the console command options.
155
     *
156
     * @return array
157
     */
158
    protected function getOptions(): array
159
    {
160
        return [
161
            ['type', 'T', InputOption::VALUE_OPTIONAL, 'File is of type c: [f, d]'],
162
            ['name', 'N', InputOption::VALUE_OPTIONAL, 'Base of file name (the path with the leading directories removed) matches shell pattern pattern.  The metacharacters (`*\', `?\', and `[]\') match a `.\' at  the  start of  the  base name (this is a change in findutils-4.2.2; see section STANDARDS CONFORMANCE below).  To ignore a directory and the files under it, use -prune; see an example in the description of -path.  Braces are not recognised as being special, despite the fact that some shells including Bash imbue braces with a special meaning in shell patterns.  The filename matching is performed with the use of the fnmatch(3) library function.   Don\'t forget to enclose the pattern in quotes in order to protect it from expansion by the shell.'],
163
            ['maxdepth', 'M', InputOption::VALUE_OPTIONAL, '-maxdepth alias -M'],
164
            ['delete', 'd', InputOption::VALUE_OPTIONAL, 'Delete files; true if removal succeeded.  If the removal failed, an error message is issued.  If -delete fails, find\'s exit status will be nonzervagranto (when it  eventually exits).  Use of -delete automatically turns on the -depth option.'],
165
        ];
166
    }
167
}
168