DebugCommand::execute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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