CleanCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 7
dl 0
loc 52
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
B execute() 0 32 4
1
<?php
2
namespace Genkgo\Srvcleaner\Command;
3
4
use Genkgo\Srvcleaner\Config;
5
use Genkgo\Srvcleaner\TaskInterface;
6
use Genkgo\Srvcleaner\Util\ProcessAwareInterface;
7
use Genkgo\Srvcleaner\Util\Processor;
8
use Psr\Log\LoggerAwareInterface;
9
use Psr\Log\LogLevel;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Logger\ConsoleLogger;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
/**
16
 * Class CleanCommand
17
 * @package Genkgo\Srvcleaner\Command
18
 */
19
class CleanCommand extends AbstractCommand
20
{
21
    /**
22
     *
23
     */
24
    protected function configure()
25
    {
26
        $this
27
            ->setName('clean')
28
            ->setDescription('Clean up your server')
29
            ->addOption('config', '-c', InputOption::VALUE_REQUIRED, 'Configuration file')
30
            ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Do nothing, show what is to be cleaned');
31
    }
32
33
    /**
34
     * @param InputInterface $input
35
     * @param OutputInterface $output
36
     * @return void
37
     */
38
    protected function execute(InputInterface $input, OutputInterface $output)
39
    {
40
        $cwd = getcwd();
41
        $configFile = $input->getOption('config');
42
        if ($configFile === null) {
43
            $configFile = $cwd.'/srvcleaner.json';
44
        }
45
46
        $logger = new ConsoleLogger($output, [
47
            LogLevel::EMERGENCY => OutputInterface::VERBOSITY_NORMAL,
48
            LogLevel::ALERT => OutputInterface::VERBOSITY_NORMAL,
49
            LogLevel::CRITICAL => OutputInterface::VERBOSITY_NORMAL,
50
            LogLevel::ERROR => OutputInterface::VERBOSITY_NORMAL,
51
            LogLevel::WARNING => OutputInterface::VERBOSITY_NORMAL,
52
            LogLevel::NOTICE => OutputInterface::VERBOSITY_NORMAL,
53
            LogLevel::INFO => OutputInterface::VERBOSITY_NORMAL,
54
            LogLevel::DEBUG => OutputInterface::VERBOSITY_VERBOSE,
55
        ]);
56
57
        $config = Config::fromFile($configFile);
58
        $tasks = $config->getTasks();
59
        $tasks->each(function (TaskInterface $task) use ($cwd, $input, $logger) {
60
            if ($task instanceof ProcessAwareInterface) {
61
                $task->setProcessor(new Processor());
62
            }
63
            if ($task instanceof LoggerAwareInterface) {
64
                $task->setLogger($logger);
65
            }
66
            $task->setCurrentWorkingDirectory($cwd);
67
            $task->execute($input->getOption('dry-run'));
68
        });
69
    }
70
}
71