Passed
Push — master ( f62883...154da5 )
by Vladimir
02:06
created

MakeCrud::writeNextStepsMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Koff\Bundle\CrudMakerBundle\Maker;
4
5
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
0 ignored issues
show
Bug introduced by
The type Doctrine\Bundle\DoctrineBundle\DoctrineBundle was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Doctrine\Common\Inflector\Inflector;
7
use Doctrine\ORM\EntityManagerInterface;
8
use Doctrine\ORM\Mapping\Column;
9
use Koff\Bundle\CrudMakerBundle\GeneratorHelper;
10
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
0 ignored issues
show
Bug introduced by
The type Sensio\Bundle\FrameworkE...dle\Configuration\Route was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Symfony\Bundle\MakerBundle\ConsoleStyle;
12
use Symfony\Bundle\MakerBundle\DependencyBuilder;
13
use Symfony\Bundle\MakerBundle\Generator;
14
use Symfony\Bundle\MakerBundle\InputConfiguration;
15
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
16
use Symfony\Bundle\MakerBundle\Str;
17
use Symfony\Bundle\MakerBundle\Validator;
18
use Symfony\Bundle\TwigBundle\TwigBundle;
0 ignored issues
show
Bug introduced by
The type Symfony\Bundle\TwigBundle\TwigBundle was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use Symfony\Component\Console\Command\Command;
20
use Symfony\Component\Console\Input\InputArgument;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Form\AbstractType;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Form\AbstractType was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Symfony\Component\Security\Csrf\CsrfTokenManager;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Security\Csrf\CsrfTokenManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use Symfony\Component\Validator\Validation;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Validator\Validation was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
26
/**
27
 * @author Sadicov Vladimir <[email protected]>
28
 */
29
final class MakeCrud extends AbstractMaker
30
{
31
    private $entityManager;
32
33
    public function __construct(EntityManagerInterface $entityManager)
34
    {
35
        $this->entityManager = $entityManager;
36
    }
37
38
    public static function getCommandName(): string
39
    {
40
        return 'make:crud';
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function configureCommand(Command $command, InputConfiguration $inputConfig)
47
    {
48
        $command
49
            ->setDescription('Creates crud for Doctrine entity class')
50
            ->addArgument('entity-class', InputArgument::OPTIONAL, sprintf('The class name of the entity to create crud (e.g. <fg=yellow>%s</>)', Str::asClassName(Str::getRandomTerm())))
51
            ->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeCrud.txt'))
52
        ;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function configureDependencies(DependencyBuilder $dependencies)
59
    {
60
        $dependencies->addClassDependency(
61
            Route::class,
62
            'annotations'
63
        );
64
65
        $dependencies->addClassDependency(
66
            AbstractType::class,
67
            'form'
68
        );
69
70
        $dependencies->addClassDependency(
71
            Validation::class,
72
            'validator'
73
        );
74
75
        $dependencies->addClassDependency(
76
            TwigBundle::class,
77
            'twig-bundle'
78
        );
79
80
        $dependencies->addClassDependency(
81
            DoctrineBundle::class,
82
            'orm'
83
        );
84
85
        $dependencies->addClassDependency(
86
            Column::class,
87
            'orm'
88
        );
89
90
        $dependencies->addClassDependency(
91
            CsrfTokenManager::class,
92
            'security-csrf'
93
        );
94
    }
95
96
    public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator)
97
    {
98
        $entityClassNameDetails = $generator->createClassNameDetails(
99
            $input->getArgument('entity-class'),
100
            'Entity\\'
101
        );
102
103
        $controllerClassNameDetails = $generator->createClassNameDetails(
104
            $entityClassNameDetails->getRelativeNameWithoutSuffix(),
105
            'Controller\\',
106
            'Controller'
107
        );
108
109
        $formClassNameDetails = $generator->createClassNameDetails(
110
            $entityClassNameDetails->getRelativeNameWithoutSuffix(),
111
            'Form\\',
112
            'Type'
113
        );
114
115
        $metadata = $this->entityManager->getClassMetadata($entityClassNameDetails->getFullName());
116
        $entityVarPlural = lcfirst(Inflector::pluralize($entityClassNameDetails->getShortName()));
117
        $entityVarSingular = lcfirst(Inflector::singularize($entityClassNameDetails->getShortName()));
118
        $routeName = Str::asRouteName($controllerClassNameDetails->getRelativeNameWithoutSuffix());
119
120
        $generator->generateClass(
121
            $controllerClassNameDetails->getFullName(),
122
            'crud/controller/Controller.tpl.php',
123
            [
124
                'entity_full_class_name' => $entityClassNameDetails->getFullName(),
125
                'entity_class_name' => $entityClassNameDetails->getShortName(),
126
                'form_full_class_name' => $formClassNameDetails->getFullName(),
127
                'form_class_name' => $formClassNameDetails->getShortName(),
128
                'route_path' => Str::asRoutePath($controllerClassNameDetails->getRelativeNameWithoutSuffix()),
129
                'route_name' => $routeName,
130
                'entity_var_plural' => $entityVarPlural,
131
                'entity_var_singular' => $entityVarSingular,
132
                'entity_identifier' => $metadata->identifier[0],
0 ignored issues
show
Bug introduced by
Accessing identifier on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
133
            ]
134
        );
135
136
        $helper = new GeneratorHelper();
137
138
        $generator->generateClass(
139
            $formClassNameDetails->getFullName(),
140
            'form/crud/Type.tpl.php',
141
            [
142
                'entity_class_exists' => true,
143
                'entity_full_class_name' => $entityClassNameDetails->getFullName(),
144
                'entity_class_name' => $entityClassNameDetails->getShortName(),
145
                'entity_fields' => $metadata->fieldMappings,
0 ignored issues
show
Bug introduced by
Accessing fieldMappings on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
146
                'helper' => $helper,
147
            ]
148
        );
149
150
        $baseLayoutExists = true;
151
        $templatesPath = Str::asFilePath($controllerClassNameDetails->getRelativeNameWithoutSuffix());
152
153
        $templates = [
154
            '_delete_form' => [
155
                'route_name' => $routeName,
156
                'entity_identifier' => $metadata->identifier[0],
157
            ],
158
            '_form' => [],
159
            'edit' => [
160
                'helper' => $helper,
161
                'base_layout_exists' => $baseLayoutExists,
162
                'entity_class_name' => $entityClassNameDetails->getShortName(),
163
                'entity_var_singular' => $entityVarSingular,
164
                'entity_identifier' => $metadata->identifier[0],
165
                'route_name' => $routeName,
166
            ],
167
            'index' => [
168
                'helper' => $helper,
169
                'base_layout_exists' => $baseLayoutExists,
170
                'entity_class_name' => $entityClassNameDetails->getShortName(),
171
                'entity_var_plural' => $entityVarPlural,
172
                'entity_var_singular' => $entityVarSingular,
173
                'entity_identifier' => $metadata->identifier[0],
174
                'entity_fields' => $metadata->fieldMappings,
175
                'route_name' => $routeName,
176
            ],
177
            'new' => [
178
                'helper' => $helper,
179
                'base_layout_exists' => $baseLayoutExists,
180
                'entity_class_name' => $entityClassNameDetails->getShortName(),
181
                'route_name' => $routeName,
182
            ],
183
            'show' => [
184
                'helper' => $helper,
185
                'base_layout_exists' => $baseLayoutExists,
186
                'entity_class_name' => $entityClassNameDetails->getShortName(),
187
                'entity_var_singular' => $entityVarSingular,
188
                'entity_identifier' => $metadata->identifier[0],
189
                'entity_fields' => $metadata->fieldMappings,
190
                'route_name' => $routeName,
191
            ],
192
        ];
193
194
        foreach ($templates as $template => $variables) {
195
            $generator->generateFile(
196
                'templates/'.$templatesPath.'/'.$template.'.html.twig',
197
                'crud/templates/'.$template.'.tpl.php',
198
                $variables
199
            );
200
        }
201
202
        $generator->writeChanges();
203
204
        $this->writeSuccessMessage($io);
205
206
        $io->text('Next: Check your new crud!');
207
    }
208
}
209