AlreadyExtractCommand::execute()   C
last analyzed

Complexity

Conditions 9
Paths 40

Size

Total Lines 49
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 5.7446
c 0
b 0
f 0
cc 9
eloc 29
nc 40
nop 2
1
<?php
2
3
namespace AlreadyExtract\Command;
4
5
use AlreadyExtract\Checker\ZipAlreadyExtractChecker;
6
use AlreadyExtract\Factory\AlreadyExtractFactory;
7
use Symfony\Component\Console\Command\Command;
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\Output\OutputInterface;
12
use Symfony\Component\Filesystem\Filesystem;
13
use Symfony\Component\Finder\Finder;
14
use Symfony\Component\Finder\SplFileInfo;
15
16
class AlreadyExtractCommand extends Command
17
{
18
    /**
19
     * @var int
20
     */
21
    protected $countError = 0;
22
23
    /**
24
     * @var int
25
     */
26
    protected $countSuccess = 0;
27
28
    /**
29
     * @var int
30
     */
31
    protected $countWarning = 0;
32
33
    protected function configure()
34
    {
35
        $this
36
            ->setName('already-extract')
37
            ->setDescription('check if archive files are already extracted')
38
            ->addArgument(
39
                'path',
40
                InputArgument::OPTIONAL,
41
                'path of directory to check'
42
            )
43
            ->addArgument(
44
                'path-extracted',
45
                InputArgument::OPTIONAL,
46
                'path to check extracted file'
47
            )
48
            ->addOption(
49
                'drop',
50
                'd',
51
                InputOption::VALUE_NONE,
52
                'Drop extracted archives'
53
            )
54
        ;
55
    }
56
57
    protected function execute(InputInterface $input, OutputInterface $output)
58
    {
59
        $drop = false;
60
        if ($input->getOption('drop') !== false) {
61
            $drop = true;
62
        }
63
64
        $path = getcwd();
65
        if ($input->getArgument('path') !== null) {
66
            $path = rtrim($input->getArgument('path'), '/');
67
        }
68
69
        $pathExtracted = $path;
70
        if ($input->getArgument('path-extracted') !== null) {
71
            $pathExtracted = rtrim($input->getArgument('path-extracted'), '/');
72
        }
73
74
        $fs = new Filesystem();
75
        if (!$fs->exists($path)) {
76
            throw new \Exception('Directory doesn\'t exist');
77
        }
78
79
        if (!$fs->exists($pathExtracted)) {
80
            throw new \Exception('Extracted directory does\'t exist');
81
        }
82
83
        $finder = new Finder();
84
        $finder->files()->in($path)->name('*.zip')->name('*.rar');
85
86
        /** @var SplFileInfo $file */
87
        foreach ($finder as $file) {
88
            $checker = AlreadyExtractFactory::create(
89
                $file->getRealPath(),
90
                $file->getExtension()
91
            );
92
93
            $result = $checker->isAlreadyExtracted($pathExtracted . str_replace($path, '', $file->getPath()) . '/');
94
            if ($result ===  0 && $drop === true) {
95
                $fs->remove($file->getRealPath());
96
            }
97
98
            $this->writeOutput(
99
                $file->getRealPath(),
100
                $output,
101
                $result
102
            );
103
        }
104
        $output->writeln("Success: " . $this->countSuccess . " Warnings: " . $this->countWarning . " Errors: " . $this->countError);
105
    }
106
107
    /**
108
     * @param int $level Severity of alert: 1=warning & 2=error
109
     * @param string $filePath
110
     * @param OutputInterface $output
111
     */
112
    protected function writeOutput($filePath, OutputInterface $output, $level = 0)
113
    {
114
        switch ($level) {
115
            case 0:
116
                //$output->writeln("<info>Success: file " . $filePath . " is extracted</info>");
117
                $this->countSuccess++;
118
                break;
119
            case 1:
120
                $output->writeln("<comment>Warning: file " . $filePath . " looks weird</comment>");
121
                $this->countWarning++;
122
                break;
123
            case 2:
124
                $output->writeln("<error>Error: file " . $filePath . " might be not extracted</error>");
125
                $this->countError++;
126
                break;
127
        }
128
    }
129
}
130