Completed
Push — 3.x ( 3e834f...38b337 )
by Grégoire
03:36
created

ExplainAdminCommand::execute()   F

Complexity

Conditions 14
Paths 320

Size

Total Lines 119

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 119
rs 3.1466
c 0
b 0
f 0
cc 14
nc 320
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Command;
15
16
use Sonata\AdminBundle\Admin\Pool;
17
use Symfony\Component\Console\Command\Command;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21
use Symfony\Component\Validator\Mapping\ClassMetadata;
22
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
23
24
/**
25
 * @final since sonata-project/admin-bundle 3.52
26
 *
27
 * @author Thomas Rabaix <[email protected]>
28
 */
29
class ExplainAdminCommand extends Command
30
{
31
    protected static $defaultName = 'sonata:admin:explain';
32
33
    /**
34
     * @var Pool
35
     */
36
    private $pool;
37
38
    /**
39
     * @var MetadataFactoryInterface
40
     */
41
    private $validator;
42
43
    public function __construct(Pool $pool, MetadataFactoryInterface $validator)
44
    {
45
        $this->pool = $pool;
46
        $this->validator = $validator;
47
48
        parent::__construct();
49
    }
50
51
    public function configure()
52
    {
53
        $this->setDescription('Explain an admin service');
54
55
        $this->addArgument('admin', InputArgument::REQUIRED, 'The admin service id');
56
    }
57
58
    public function execute(InputInterface $input, OutputInterface $output)
59
    {
60
        $admin = $this->pool->getInstance($input->getArgument('admin'));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('admin') targeting Symfony\Component\Consol...nterface::getArgument() can also be of type array<integer,string> or null; however, Sonata\AdminBundle\Admin\Pool::getInstance() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
61
62
        $output->writeln('<comment>AdminBundle Information</comment>');
63
        $output->writeln(sprintf('<info>% -20s</info> : %s', 'id', $admin->getCode()));
64
        $output->writeln(sprintf('<info>% -20s</info> : %s', 'Admin', \get_class($admin)));
65
        $output->writeln(sprintf('<info>% -20s</info> : %s', 'Model', $admin->getClass()));
66
        $output->writeln(sprintf('<info>% -20s</info> : %s', 'Controller', $admin->getBaseControllerName()));
67
        $output->writeln(sprintf('<info>% -20s</info> : %s', 'Model Manager', \get_class($admin->getModelManager())));
68
        $output->writeln(sprintf('<info>% -20s</info> : %s', 'Form Builder', \get_class($admin->getFormBuilder())));
69
        $output->writeln(sprintf('<info>% -20s</info> : %s', 'Datagrid Builder', \get_class($admin->getDatagridBuilder())));
70
        $output->writeln(sprintf('<info>% -20s</info> : %s', 'List Builder', \get_class($admin->getListBuilder())));
71
72
        if ($admin->isChild()) {
73
            $output->writeln(sprintf('<info>% -15s</info> : %s', 'Parent', $admin->getParent()->getCode()));
74
        }
75
76
        $output->writeln('');
77
        $output->writeln('<info>Routes</info>');
78
        foreach ($admin->getRoutes()->getElements() as $route) {
79
            $output->writeln(sprintf('  - % -25s %s', $route->getDefault('_sonata_name'), $route->getPath()));
80
        }
81
82
        $output->writeln('');
83
        $output->writeln('<info>Datagrid Columns</info>');
84
        foreach ($admin->getListFieldDescriptions() as $name => $fieldDescription) {
85
            $output->writeln(sprintf(
86
                '  - % -25s  % -15s % -15s',
87
                $name,
88
                $fieldDescription->getType(),
89
                $fieldDescription->getTemplate()
90
            ));
91
        }
92
93
        $output->writeln('');
94
        $output->writeln('<info>Datagrid Filters</info>');
95
        foreach ($admin->getFilterFieldDescriptions() as $name => $fieldDescription) {
96
            $output->writeln(sprintf(
97
                '  - % -25s  % -15s % -15s',
98
                $name,
99
                $fieldDescription->getType(),
100
                $fieldDescription->getTemplate()
101
            ));
102
        }
103
104
        $output->writeln('');
105
        $output->writeln('<info>Form theme(s)</info>');
106
        foreach ($admin->getFormTheme() as $template) {
107
            $output->writeln(sprintf('  - %s', $template));
108
        }
109
110
        $output->writeln('');
111
        $output->writeln('<info>Form Fields</info>');
112
        foreach ($admin->getFormFieldDescriptions() as $name => $fieldDescription) {
113
            $output->writeln(sprintf(
114
                '  - % -25s  % -15s % -15s',
115
                $name,
116
                $fieldDescription->getType(),
117
                $fieldDescription->getTemplate()
118
            ));
119
        }
120
121
        $metadata = $this->validator->getMetadataFor($admin->getClass());
122
        if (!$metadata instanceof ClassMetadata) {
123
            throw new \UnexpectedValueException(
124
                sprintf(
125
                    'Cannot read metadata properties of %s because its metadata is an instance of %s instead of %s',
126
                    $admin->getClass(),
127
                    \get_class($metadata),
128
                    ClassMetadata::class
129
                )
130
            );
131
        }
132
133
        $output->writeln('');
134
        $output->writeln('<comment>Validation Framework</comment> - http://symfony.com/doc/3.0/book/validation.html');
135
        $output->writeln('<info>Properties constraints</info>');
136
137
        if (0 === \count($metadata->properties)) {
138
            $output->writeln('    <error>no property constraints defined !!</error>');
139
        } else {
140
            foreach ($metadata->properties as $name => $property) {
141
                $output->writeln(sprintf('  - %s', $name));
142
143
                foreach ($property->getConstraints() as $constraint) {
144
                    $output->writeln(sprintf(
145
                        '    % -70s %s',
146
                        \get_class($constraint),
147
                        implode('|', $constraint->groups)
148
                    ));
149
                }
150
            }
151
        }
152
153
        $output->writeln('');
154
        $output->writeln('<info>Getters constraints</info>');
155
156
        if (0 === \count($metadata->getters)) {
157
            $output->writeln('    <error>no getter constraints defined !!</error>');
158
        } else {
159
            foreach ($metadata->getters as $name => $property) {
160
                $output->writeln(sprintf('  - %s', $name));
161
162
                foreach ($property->getConstraints() as $constraint) {
163
                    $output->writeln(sprintf(
164
                        '    % -70s %s',
165
                        \get_class($constraint),
166
                        implode('|', $constraint->groups)
167
                    ));
168
                }
169
            }
170
        }
171
172
        $output->writeln('');
173
        $output->writeln('<info>done!</info>');
174
175
        return 0;
176
    }
177
}
178