ModulesCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
namespace Koriit\PHPDeps\Commands;
4
5
use Koriit\PHPDeps\Config\Exceptions\InvalidConfig;
6
use Koriit\PHPDeps\Config\Exceptions\InvalidSchema;
7
use Koriit\PHPDeps\ExitCodes;
8
use Koriit\PHPDeps\Helpers\InputHelper;
9
use Koriit\PHPDeps\Helpers\ModulesHelper;
10
use Koriit\PHPDeps\Modules\Module;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\Console\Style\SymfonyStyle;
16
17
class ModulesCommand extends Command
18
{
19
    /** @var ModulesHelper */
20
    private $modulesHelper;
21
22
    /** @var InputHelper */
23
    private $inputHelper;
24
25
    public function __construct(ModulesHelper $modulesHelper, InputHelper $inputHelper)
26
    {
27
        parent::__construct();
28
29
        $this->modulesHelper = $modulesHelper;
30
        $this->inputHelper = $inputHelper;
31
    }
32
33
    protected function configure()
34
    {
35
        $this
36
              ->setName('modules')
37
              ->setDescription('Lists configured modules')
38
              ->addOption('config', 'c', InputOption::VALUE_OPTIONAL, 'Custom location of configuration file', './phpdeps.xml');
39
    }
40
41
    /**
42
     * @param InputInterface  $input
43
     * @param OutputInterface $output
44
     *
45
     * @throws InvalidConfig
46
     * @throws InvalidSchema
47
     *
48
     * @return int Exit code
49
     */
50
    protected function execute(InputInterface $input, OutputInterface $output)
51
    {
52
        $io = new SymfonyStyle($input, $output);
53
54
        $config = $this->inputHelper->readConfig($input);
55
56
        $modules = $this->modulesHelper->findModules($config);
57
        if (!$this->modulesHelper->validateModules($modules, $io)) {
58
            return ExitCodes::UNEXPECTED_ERROR;
59
        }
60
61
        if (empty($modules)) {
62
            $io->warning('There are no configured modules!');
63
        } else {
64
            $this->displayModules($modules, $io);
65
        }
66
67
        return ExitCodes::OK;
68
    }
69
70
    /**
71
     * @param Module[]     $modules
72
     * @param SymfonyStyle $io
73
     */
74
    private function displayModules(array $modules, SymfonyStyle $io)
75
    {
76
        $i = 1;
77
        foreach ($modules as $module) {
78
            $io->section($i++ . '. ' . $module->getName() . ' [<fg=magenta>' . $module->getNamespace() . '</>]');
79
        }
80
    }
81
}
82