Passed
Push — master ( c7346e...14a74d )
by Vladimir
01:57
created

MakeCrud::interact()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 3
dl 0
loc 2
rs 10
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\Mapping\Column;
8
use Doctrine\ORM\EntityManagerInterface;
9
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...
10
use Symfony\Bundle\MakerBundle\ConsoleStyle;
11
use Symfony\Bundle\MakerBundle\DependencyBuilder;
12
use Symfony\Bundle\MakerBundle\InputConfiguration;
13
use Symfony\Bundle\MakerBundle\MakerInterface;
14
use Symfony\Bundle\MakerBundle\Str;
15
use Symfony\Bundle\MakerBundle\Validator;
16
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...
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\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...
21
use Symfony\Component\Routing\RouterInterface;
22
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...
23
use Koff\Bundle\CrudMakerBundle\GeneratorHelper;
24
25
/**
26
 * @author Sadicov Vladimir <[email protected]>
27
 */
28
final class MakeCrud implements MakerInterface
29
{
30
    private $router;
31
32
    private $entityManager;
33
34
    public function __construct(RouterInterface $router, EntityManagerInterface $entityManager)
35
    {
36
        $this->router = $router;
37
        $this->entityManager = $entityManager;
38
    }
39
40
    public static function getCommandName(): string
41
    {
42
        return 'make:crud';
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function configureCommand(Command $command, InputConfiguration $inputConfig)
49
    {
50
        $command
51
            ->setDescription('Creates crud for Doctrine entity class')
52
            ->addArgument('entity-class', InputArgument::OPTIONAL, sprintf('The class name of the entity to create crud (e.g. <fg=yellow>%s</>)', Str::asClassName(Str::getRandomTerm())))
53
            ->setHelp(file_get_contents(__DIR__.'/../Resources/help/MakeCrud.txt'))
54
        ;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function configureDependencies(DependencyBuilder $dependencies)
61
    {
62
        $dependencies->addClassDependency(
63
            Route::class,
64
            'annotations'
65
        );
66
67
        $dependencies->addClassDependency(
68
            AbstractType::class,
69
            'form'
70
        );
71
72
        $dependencies->addClassDependency(
73
            Validation::class,
74
            'validator'
75
        );
76
77
        $dependencies->addClassDependency(
78
            TwigBundle::class,
79
            'twig-bundle'
80
        );
81
82
        $dependencies->addClassDependency(
83
            DoctrineBundle::class,
84
            'orm'
85
        );
86
87
        $dependencies->addClassDependency(
88
            Column::class,
89
            'orm'
90
        );
91
92
        $dependencies->addClassDependency(
93
            CsrfTokenManager::class,
0 ignored issues
show
Bug introduced by
The type Koff\Bundle\CrudMakerBundle\Maker\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...
94
            'security-csrf'
95
        );
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function interact(InputInterface $input, ConsoleStyle $io, Command $command)
102
    {
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function getParameters(InputInterface $input): array
109
    {
110
        $entityClassName = Str::asClassName($input->getArgument('entity-class'));
111
        Validator::validateClassName($entityClassName);
112
        $controllerClassName = Str::asClassName($entityClassName, 'Controller');
113
        Validator::validateClassName($controllerClassName);
114
        $formClassName = Str::asClassName($entityClassName, 'Type');
115
        Validator::validateClassName($formClassName);
116
117
        $metadata = $this->entityManager->getClassMetadata('App\\Entity\\'.$entityClassName);
118
119
        $helper = new GeneratorHelper();
120
121
        return [
122
            'helper' => $helper,
123
            'controller_class_name' => $controllerClassName,
124
            'entity_var_plural' => lcfirst(Inflector::pluralize($entityClassName)),
125
            'entity_var_singular' => lcfirst(Inflector::singularize($entityClassName)),
126
            'entity_class_name' => $entityClassName,
127
            '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...
128
            '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...
129
            'form_class_name' => $formClassName,
130
            // temporary hardcoded var for helper and generator
131
            'base_layout_exists' => true,
132
            'route_path' => Str::asRoutePath(str_replace('Controller', '', $controllerClassName)),
133
            'route_name' => Str::asRouteName(str_replace('Controller', '', $controllerClassName)),
134
        ];
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function getFiles(array $params): array
141
    {
142
        return [
143
            __DIR__.'/../Resources/skeleton/controller/Controller.tpl.php' => 'src/Controller/'.$params['controller_class_name'].'.php',
144
            __DIR__.'/../Resources/skeleton/form/Type.tpl.php' => 'src/Form/'.$params['form_class_name'].'.php',
145
            __DIR__.'/../Resources/skeleton/templates/index.tpl.php' => 'templates/'.$params['route_name'].'/index.html.twig',
146
            __DIR__.'/../Resources/skeleton/templates/show.tpl.php' => 'templates/'.$params['route_name'].'/show.html.twig',
147
            __DIR__.'/../Resources/skeleton/templates/new.tpl.php' => 'templates/'.$params['route_name'].'/new.html.twig',
148
            __DIR__.'/../Resources/skeleton/templates/edit.tpl.php' => 'templates/'.$params['route_name'].'/edit.html.twig',
149
            __DIR__.'/../Resources/skeleton/templates/_form.tpl.php' => 'templates/'.$params['route_name'].'/_form.html.twig',
150
            __DIR__.'/../Resources/skeleton/templates/_delete_form.tpl.php' => 'templates/'.$params['route_name'].'/_delete_form.html.twig',
151
        ];
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    public function writeNextStepsMessage(array $params, ConsoleStyle $io)
158
    {
159
        if (!count($this->router->getRouteCollection())) {
160
            $io->text('<error> Warning! </> No routes configuration defined yet.');
161
            $io->text('           You should probably uncomment the annotation routes in <comment>config/routes.yaml</>');
162
            $io->newLine();
163
        }
164
        $io->text('Next: Check your new crud!');
165
    }
166
}
167