Completed
Push — master ( 9919f9...1d1ba6 )
by Anton
09:31
created

GridCommand   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 10

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 10
dl 0
loc 150
ccs 40
cts 40
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 17 1
B execute() 0 43 6
B generate() 0 50 5
A verify() 0 14 3
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link https://github.com/bluzphp/bluzman
5
 */
6
7
namespace Bluzman\Command\Generate;
8
9
use Bluz\Validator\Validator as v;
10
use Bluzman\Input\InputArgument;
11
use Bluzman\Input\InputException;
12
use Bluzman\Generator;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
/**
17
 * ModelCommand
18
 *
19
 * @package  Bluzman\Command\Generate
20
 */
21
class GridCommand extends AbstractGenerateCommand
22
{
23
    /**
24
     * Command configuration
25
     */
26 14
    protected function configure()
27
    {
28
        $this
29
            // the name of the command (the part after "bin/bluzman")
30 14
            ->setName('generate:grid')
31
            // the short description shown while running "php bin/bluzman list"
32 14
            ->setDescription('Generate a GRID for model')
33
            // the full command description shown when running the command with
34
            // the "--help" option
35 14
            ->setHelp('This command allows you to generate GRID files')
36
        ;
37
38
        $this
39 14
            ->addModelArgument()
40 14
            ->addModuleArgument(InputArgument::OPTIONAL)
41
        ;
42 14
    }
43
44
    /**
45
     * @param InputInterface $input
46
     * @param OutputInterface $output
47
     * @return void
48
     */
49 2
    protected function execute(InputInterface $input, OutputInterface $output)
50
    {
51
        try {
52 2
            $this->write("Running <info>generate:grid</info> command");
53
54 2
            $model = $input->getArgument('model');
55 2
            $this->getDefinition()->getArgument('model')->validate($model);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Input\InputArgument as the method validate() does only exist in the following sub-classes of Symfony\Component\Console\Input\InputArgument: Bluzman\Input\InputArgument. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
56
57 1
            if (!$this->getApplication()->isModelExists($model)) {
58
                throw new InputException(
59
                    "Model $model is not exist, " .
60
                    "run command <question>bluzman generate:model $model</question> before"
61
                );
62
            }
63
64 1
            if ($module = $input->getArgument('module')) {
65
                $this->getDefinition()->getArgument('module')->validate($module);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Console\Input\InputArgument as the method validate() does only exist in the following sub-classes of Symfony\Component\Console\Input\InputArgument: Bluzman\Input\InputArgument. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
66
67
                if (!$this->getApplication()->isModuleExists($module)) {
68
                    throw new InputException(
69
                        "Module $module is not exist, " .
70
                        "run command <question>bluzman generate:module $module</question> before"
71
                    );
72
                }
73
            }
74
75
            // generate directories and files
76 1
            $this->generate($input, $output);
77
78
            // verify it
79 1
            $this->verify($input, $output);
80
81 1
            $this->write("GRID for <info>{$model}</info> has been successfully created.");
82
83 1
            if ($module = $input->getArgument('module')) {
84
                $this->write(
85 1
                    "Open page <info>/acl</info> in your browser and set permissions for <info>{$module}</info>"
86
                );
87
            }
88 1
        } catch (InputException $e) {
89 1
            $this->error("ERROR: {$e->getMessage()}");
90
        }
91 2
    }
92
93
    /**
94
     * @param InputInterface $input
95
     * @param OutputInterface $output
96
     * @return void
97
     * @throws InputException
98
     */
99 1
    protected function generate(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Unused Code introduced by
The parameter $output is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
    {
101 1
        $model = ucfirst($input->getArgument('model'));
102
103
        // generate CRUD
104 1
        $crudFile = $this->getApplication()->getModelPath($model) . DS . 'Grid.php';
105
106 1
        if (file_exists($crudFile)) {
107
            $this->comment("Crud file <info>$model/Grid.php</info> already exists");
108
        } else {
109 1
            $template = $this->getTemplate('GridTemplate');
110 1
            $template->setFilePath($crudFile);
111 1
            $template->setTemplateData([
112 1
                'model' => $model
113
            ]);
114
115 1
            $generator = new Generator\Generator($template);
116 1
            $generator->make();
117
        }
118
119 1
        if ($module = $input->getArgument('module')) {
120
            $this->write("Generate <info>$module/controllers/grid.php</info>");
121
122
            $controllerFile = $this->getControllerPath($module, 'grid');
123
            if (file_exists($controllerFile)) {
124
                $this->comment("Controller file <info>$module/grid</info> already exists");
125
            } else {
126
                $template = new Generator\Template\GridControllerTemplate();
127
                $template->setFilePath($controllerFile);
128
                $template->setTemplateData(['model' => $model]);
129
130
                $generator = new Generator\Generator($template);
131
                $generator->make();
132
            }
133
134
            $this->write("Generate <info>$module/views/grid.phtml</info>");
135
136
            $viewFile = $this->getViewPath($module, 'grid');
137
            if (file_exists($viewFile)) {
138
                $this->comment("View file <info>$module/grid</info> already exists");
139
            } else {
140
                $template = new Generator\Template\GridViewTemplate();
141
                $template->setFilePath($viewFile);
142
                $template->setTemplateData(['model' => $model]);
143
144
                $generator = new Generator\Generator($template);
145
                $generator->make();
146
            }
147
        }
148 1
    }
149
150
    /**
151
     * @param InputInterface $input
152
     * @param OutputInterface $output
153
     * @return void
154
     * @throws \Bluzman\Generator\GeneratorException
155
     */
156 1
    public function verify(InputInterface $input, OutputInterface $output)
157
    {
158 1
        $modelPath = $this->getApplication()->getModelPath($input->getArgument('model'));
159
160
        $paths = [
161 1
            $modelPath . DS . 'Grid.php',
162
        ];
163
164 1
        foreach ($paths as $path) {
165 1
            if (!$this->getFs()->exists($path)) {
166 1
                throw new Generator\GeneratorException("File `$path` is not exists");
167
            }
168
        }
169 1
    }
170
}
171