ClearReportsCommand::execute()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 14
nc 4
nop 2
dl 0
loc 24
rs 8.5125
c 1
b 0
f 0
1
<?php
2
3
namespace PhpEarth\Stats\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Console\Question\ConfirmationQuestion;
9
10
/**
11
 * Console command that removes all generated report folders from the defined reports
12
 * directory.
13
 */
14
class ClearReportsCommand extends Command
15
{
16
    private $reportsRoot;
17
18
    public function setReportsRoot($reportsRoot)
19
    {
20
        $this->reportsRoot = $reportsRoot;
21
    }
22
23
    /**
24
     * Configures reports clearing command for Console component.
25
     */
26
    protected function configure()
27
    {
28
        try {
29
            $this
30
                ->setName('clear-reports')
31
                ->setDescription('Clears all reports files and folders')
32
            ;
33
        } catch (\Exception $e) {
34
            echo $e->getMessage();
35
        }
36
    }
37
38
    /**
39
     * @inheritdoc
40
     */
41
    protected function execute(InputInterface $input, OutputInterface $output)
42
    {
43
        if (!$this->reportsRoot) {
44
            die("You must set the reports root directory");
0 ignored issues
show
Coding Style Compatibility introduced by
The method execute() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
45
        }
46
47
        $helper = $this->getHelper('question');
48
        $question = new ConfirmationQuestion('This will remove all reports folders in var/reports? Are you sure you want to continue?', false);
49
50
        if (!$helper->ask($input, $output, $question)) {
51
            $output->writeln("Exiting...");
52
            return;
53
        }
54
55
        $dirs = array_diff(scandir($this->reportsRoot), ['..', '.', '.gitkeep']);
56
        foreach ($dirs as $dir) {
57
            $files = array_diff(scandir($this->reportsRoot.'/'.$dir), ['..', '.']);
58
            foreach ($files as $file) {
59
                unlink($this->reportsRoot.'/'.$dir.'/'.$file);
60
            }
61
            rmdir($this->reportsRoot.'/'.$dir);
62
        }
63
64
        $output->writeln("Reports cleaned.");
65
    }
66
}
67