Completed
Push — master ( 81cc2b...97ccae )
by recca
03:07
created

Find::getArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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