ExplainAdminCommand::execute()   F
last analyzed

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
 * @author Thomas Rabaix <[email protected]>
26
 */
27
final class ExplainAdminCommand extends Command
28
{
29
    protected static $defaultName = 'sonata:admin:explain';
30
31
    /**
32
     * @var Pool
33
     */
34
    private $pool;
35
36
    /**
37
     * @var MetadataFactoryInterface
38
     */
39
    private $validator;
40
41
    public function __construct(Pool $pool, MetadataFactoryInterface $validator)
42
    {
43
        $this->pool = $pool;
44
        $this->validator = $validator;
45
46
        parent::__construct();
47
    }
48
49
    public function configure(): void
50
    {
51
        $this->setDescription('Explain an admin service');
52
53
        $this->addArgument('admin', InputArgument::REQUIRED, 'The admin service id');
54
    }
55
56
    public function execute(InputInterface $input, OutputInterface $output): int
57
    {
58
        $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...
59
60
        $output->writeln('<comment>AdminBundle Information</comment>');
61
        $output->writeln(sprintf('<info>% -20s</info> : %s', 'id', $admin->getCode()));
62
        $output->writeln(sprintf('<info>% -20s</info> : %s', 'Admin', \get_class($admin)));
63
        $output->writeln(sprintf('<info>% -20s</info> : %s', 'Model', $admin->getClass()));
64
        $output->writeln(sprintf('<info>% -20s</info> : %s', 'Controller', $admin->getBaseControllerName()));
65
        $output->writeln(sprintf('<info>% -20s</info> : %s', 'Model Manager', \get_class($admin->getModelManager())));
66
        $output->writeln(sprintf('<info>% -20s</info> : %s', 'Form Builder', \get_class($admin->getFormBuilder())));
67
        $output->writeln(sprintf('<info>% -20s</info> : %s', 'Datagrid Builder', \get_class($admin->getDatagridBuilder())));
68
        $output->writeln(sprintf('<info>% -20s</info> : %s', 'List Builder', \get_class($admin->getListBuilder())));
69
70
        if ($admin->isChild()) {
71
            $output->writeln(sprintf('<info>% -15s</info> : %s', 'Parent', $admin->getParent()->getCode()));
72
        }
73
74
        $output->writeln('');
75
        $output->writeln('<info>Routes</info>');
76
        foreach ($admin->getRoutes()->getElements() as $route) {
77
            $output->writeln(sprintf('  - % -25s %s', $route->getDefault('_sonata_name'), $route->getPath()));
78
        }
79
80
        $output->writeln('');
81
        $output->writeln('<info>Datagrid Columns</info>');
82
        foreach ($admin->getListFieldDescriptions() as $name => $fieldDescription) {
83
            $output->writeln(sprintf(
84
                '  - % -25s  % -15s % -15s',
85
                $name,
86
                $fieldDescription->getType(),
87
                $fieldDescription->getTemplate()
88
            ));
89
        }
90
91
        $output->writeln('');
92
        $output->writeln('<info>Datagrid Filters</info>');
93
        foreach ($admin->getFilterFieldDescriptions() as $name => $fieldDescription) {
94
            $output->writeln(sprintf(
95
                '  - % -25s  % -15s % -15s',
96
                $name,
97
                $fieldDescription->getType(),
98
                $fieldDescription->getTemplate()
99
            ));
100
        }
101
102
        $output->writeln('');
103
        $output->writeln('<info>Form theme(s)</info>');
104
        foreach ($admin->getFormTheme() as $template) {
105
            $output->writeln(sprintf('  - %s', $template));
106
        }
107
108
        $output->writeln('');
109
        $output->writeln('<info>Form Fields</info>');
110
        foreach ($admin->getFormFieldDescriptions() as $name => $fieldDescription) {
111
            $output->writeln(sprintf(
112
                '  - % -25s  % -15s % -15s',
113
                $name,
114
                $fieldDescription->getType(),
115
                $fieldDescription->getTemplate()
116
            ));
117
        }
118
119
        $metadata = $this->validator->getMetadataFor($admin->getClass());
120
        if (!$metadata instanceof ClassMetadata) {
121
            throw new \UnexpectedValueException(
122
                sprintf(
123
                    'Cannot read metadata properties of %s because its metadata is an instance of %s instead of %s',
124
                    $admin->getClass(),
125
                    \get_class($metadata),
126
                    ClassMetadata::class
127
                )
128
            );
129
        }
130
131
        $output->writeln('');
132
        $output->writeln('<comment>Validation Framework</comment> - http://symfony.com/doc/3.0/book/validation.html');
133
        $output->writeln('<info>Properties constraints</info>');
134
135
        if (0 === \count($metadata->properties)) {
136
            $output->writeln('    <error>no property constraints defined !!</error>');
137
        } else {
138
            foreach ($metadata->properties as $name => $property) {
139
                $output->writeln(sprintf('  - %s', $name));
140
141
                foreach ($property->getConstraints() as $constraint) {
142
                    $output->writeln(sprintf(
143
                        '    % -70s %s',
144
                        \get_class($constraint),
145
                        implode('|', $constraint->groups)
146
                    ));
147
                }
148
            }
149
        }
150
151
        $output->writeln('');
152
        $output->writeln('<info>Getters constraints</info>');
153
154
        if (0 === \count($metadata->getters)) {
155
            $output->writeln('    <error>no getter constraints defined !!</error>');
156
        } else {
157
            foreach ($metadata->getters as $name => $property) {
158
                $output->writeln(sprintf('  - %s', $name));
159
160
                foreach ($property->getConstraints() as $constraint) {
161
                    $output->writeln(sprintf(
162
                        '    % -70s %s',
163
                        \get_class($constraint),
164
                        implode('|', $constraint->groups)
165
                    ));
166
                }
167
            }
168
        }
169
170
        $output->writeln('');
171
        $output->writeln('<info>done!</info>');
172
173
        return 0;
174
    }
175
}
176