DebugCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace Psi\Bundle\Description\Command;
4
5
use Psi\Component\Description\Schema\Definition;
6
use Psi\Component\Description\Schema\Schema;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Helper\Table;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class DebugCommand extends Command
14
{
15
    private $schema;
16
17
    public function __construct(
18
        Schema $schema
19
    ) {
20
        parent::__construct();
21
        $this->schema = $schema;
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function configure()
28
    {
29
        $this->setName('psi:debug:description');
30
        $this->addArgument('descriptor', InputArgument::OPTIONAL, 'Show information for specific descriptor');
31
        $this->setDescription('List and inspect descriptors');
32
        $this->setHelp(<<<'EOT'
33
Invoke with no arguments in order to list all available descriptors:
34
35
    $ %command.full_name%
36
37
Specify a desacriptor in order to show more information:
38
39
    $ %command.full_name% std.title
40
EOT
41
        );
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function execute(InputInterface $input, OutputInterface $output)
48
    {
49
        $key = $input->getArgument('descriptor');
50
51
        if (null === $key) {
52
            return $this->listDescriptors($output);
53
        }
54
55
        $descriptor = $this->schema->getDefinition($key);
56
57
        return $this->showDescriptor($output, $key, $descriptor);
58
    }
59
60
    private function listDescriptors(OutputInterface $output)
61
    {
62
        $output->writeln('<info>List of available descriptors:</info>');
63
        $table = new Table($output);
64
        $table->setStyle('compact');
65
        foreach ($this->schema->getDefinitions() as $key => $definition) {
66
            $table->addRow([
67
                sprintf('<comment>%s</comment>', $key),
68
                $definition->getInfo(),
69
            ]);
70
        }
71
72
        $table->render();
73
        $output->writeln('// Specify a descriptor key for more information');
74
    }
75
76
    private function showDescriptor(OutputInterface $output, $key, Definition $definition)
77
    {
78
        $data = [
79
            'key' => $key,
80
            'info' => $definition->getInfo(),
81
            'descriptor' => $definition->getClass(),
82
            'extension' => $definition->getExtensionClass(),
83
        ];
84
85
        $table = new Table($output);
86
        $table->setStyle('compact');
87
88
        foreach ($data as $key => $value) {
89
            $table->addRow([sprintf('<comment>%s</comment>', $key), $value]);
90
        }
91
92
        $table->render();
93
    }
94
}
95