Find   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 150
ccs 52
cts 52
cp 1
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 4

5 Methods

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