Completed
Push — master ( da89f6...8759f3 )
by recca
01:28
created

Find::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 2
dl 0
loc 12
ccs 9
cts 9
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Recca0120\Terminal\Console\Commands;
4
5
use Exception;
6
use Illuminate\Console\Command;
7
use Symfony\Component\Finder\Finder;
8
use Illuminate\Filesystem\Filesystem;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Input\StringInput;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
class Find extends Command
16
{
17
    /**
18
     * The console command name.
19
     *
20
     * @var string
21
     */
22
    protected $name = 'find';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'search for files in a directory hierarchy';
30
31
    /**
32
     * $finder.
33
     *
34
     * @var \Symfony\Component\Finder\Finder
35
     */
36
    protected $finder;
37
38
    /**
39
     * $files.
40
     *
41
     * @var \Illuminate\Filesystem\Filesystem
42
     */
43
    protected $files;
44
45
    /**
46
     * __construct.
47
     *
48
     * @param \Symfony\Component\Finder\Finder  $finder
49
     * @param \Illuminate\Filesystem\Filesystem $files
50
     */
51 8
    public function __construct(Finder $finder, Filesystem $files)
52
    {
53 8
        parent::__construct();
54
55 8
        $this->finder = $finder;
56 8
        $this->files = $files;
57 8
    }
58
59
    /**
60
     * run.
61
     *
62
     * @param \Symfony\Component\Console\Input\InputInterface   $input
63
     * @param \Symfony\Component\Console\Output\OutputInterface $output
64
     * @return int
65
     */
66 1
    public function run(InputInterface $input, OutputInterface $output)
67
    {
68 1
        $command = (string) $input;
69 1
        $command = strtr($command, [
70 1
            ' -name' => ' -N',
71 1
            ' -type' => ' -T',
72 1
            ' -maxdepth' => ' -M',
73 1
            ' -delete' => ' -d true',
74 1
        ]);
75
76 1
        return parent::run(new StringInput($command), $output);
77
    }
78
79
    /**
80
     * Handle the command.
81
     *
82 7
     * @throws \InvalidArgumentException
83
     */
84 7
    public function handle()
85 7
    {
86 7
        $path = $this->argument('path');
87 7
        $name = $this->option('name');
88 7
        $type = $this->option('type');
89
        $maxDepth = $this->option('maxdepth');
90 7
        $delete = filter_var($this->option('delete'), FILTER_VALIDATE_BOOLEAN);
91 7
92
        $root = function_exists('base_path') === true ? base_path() : getcwd();
93 7
        $path = rtrim($root, '/').'/'.$path;
94
95 7
        $this->finder->in($path);
96 7
97 7
        if ($name !== null) {
98
            $this->finder->name($name);
99
        }
100 7
101 1
        switch ($type) {
102 1
            case 'd':
103 6
                $this->finder->directories();
104 1
                break;
105 1
            case 'f':
106
                $this->finder->files();
107
                break;
108 7
        }
109 2
110 1
        if (is_null($maxDepth) === false) {
111
            if ($maxDepth == '0') {
112 1
                $this->line($path);
113
114 1
                return;
115 1
            }
116
            $this->finder->depth('<'.$maxDepth);
117 6
        }
118 6
119 6
        foreach ($this->finder->getIterator() as $file) {
120 2
            $realPath = $file->getRealpath();
121
            if ($delete === true && $this->files->exists($realPath) === true) {
122 2
                $removed = false;
123 1
                try {
124 1
                    if ($this->files->isDirectory($realPath) === true) {
125
                        $removed = $this->files->deleteDirectory($realPath);
126
                    } else {
127 2
                        $removed = $this->files->delete($realPath);
128 1
                    }
129
                } catch (Exception $e) {
130 2
                    $removed = false;
131 2
                }
132 4
                $removed === true ? $this->info('removed '.$realPath) : $this->error('removed '.$realPath.' fail');
133
            } else {
134 6
                $this->line($realPath);
135 6
            }
136
        }
137
    }
138
139
    /**
140
     * Get the console command arguments.
141
     *
142 8
     * @return array
143
     */
144
    protected function getArguments()
145 8
    {
146 8
        return [
147
            ['path', InputArgument::REQUIRED, 'path'],
148
        ];
149
    }
150
151
    /**
152
     * Get the console command options.
153
     *
154 8
     * @return array
155
     */
156
    protected function getOptions()
157 8
    {
158 8
        return [
159 8
            ['type', 'T', InputOption::VALUE_OPTIONAL, 'File is of type c: [f, d]'],
160 8
            ['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.'],
161 8
            ['maxdepth', 'M', InputOption::VALUE_OPTIONAL, '-maxdepth alias -M'],
162
            ['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.'],
163
        ];
164
    }
165
}
166