Completed
Push — master ( 550846...8c8483 )
by Anton
09:53
created

RestCommand::execute()   B

Complexity

Conditions 5
Paths 17

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 8.439
c 0
b 0
f 0
cc 5
eloc 26
nc 17
nop 2
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
20
 */
21
class RestCommand extends AbstractGenerateCommand
22
{
23
    /**
24
     * Command configuration
25
     */
26
    protected function configure()
27
    {
28
        $this
29
            // the name of the command (the part after "bin/bluzman")
30
            ->setName('generate:rest')
31
            // the short description shown while running "php bin/bluzman list"
32
            ->setDescription('Generate a REST controller')
33
            // the full command description shown when running the command with
34
            // the "--help" option
35
            ->setHelp('This command allows you to generate REST controller')
36
        ;
37
38
        $this->addModelArgument();
39
        $this->addModuleArgument();
40
    }
41
42
    /**
43
     * @param InputInterface $input
44
     * @param OutputInterface $output
45
     * @return void
46
     */
47
    protected function execute(InputInterface $input, OutputInterface $output)
48
    {
49
        try {
50
            $this->write("Running <info>generate:rest</info> command");
51
52
            $model = $input->getArgument('model');
53
            $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...
54
55
            if (!$this->getApplication()->isModelExists($model)) {
56
                throw new InputException(
57
                    "Model $model is not exist, ".
58
                    "run command <question>bluzman generate:model $model</question> before");
59
            }
60
61
            $crudPath = $this->getApplication()->getModelPath($model) .DS. 'Crud.php';
62
            if (!is_file($crudPath)) {
63
                throw new InputException(
64
                    "CRUD for $model is not exist, ".
65
                    "run command <question>bluzman generate:crud $model</question> before");
66
            }
67
68
            $module = $input->getArgument('module');
69
            $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...
70
71
            if (!$this->getApplication()->isModuleExists($module)) {
72
                throw new InputException(
73
                    "Module $module is not exist, ".
74
                    "run command <question>bluzman generate:module $module</question> before");
75
            }
76
77
            // generate directories and files
78
            $this->generate($input, $output);
79
80
            // verify it
81
            $this->verify($input, $output);
82
83
            $this->write("REST for <info>{$model}</info> has been successfully created.");
84
            $this->write("Open page <info>/acl</info> in your browser and set permissions for <info>{$module}</info>");
85
86
        } catch (InputException $e) {
87
            $this->error("ERROR: {$e->getMessage()}");
88
        }
89
    }
90
91
    /**
92
     * @param InputInterface $input
93
     * @param OutputInterface $output
94
     * @return void
95
     * @throws InputException
96
     */
97
    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...
98
    {
99
        $model= ucfirst($input->getArgument('model'));
100
        $module= $input->getArgument('module');
101
102
        $this->write("Generate <info>$module/controllers/rest.php</info>");
103
104
        $controllerFile = $this->getControllerPath($module, 'rest');
105
        if (file_exists($controllerFile)) {
106
            $this->comment("Controller file <info>$module/crud</info> already exists");
107
        } else {
108
            $template = $this->getTemplate('RestTemplate');
109
            $template->setFilePath($controllerFile);
110
            $template->setTemplateData(['model' => $model]);
111
112
            $generator = new Generator\Generator($template);
113
            $generator->make();
114
        }
115
    }
116
117
    /**
118
     * @param InputInterface $input
119
     * @param OutputInterface $output
120
     * @return void
121
     * @throws \Bluzman\Generator\GeneratorException
122
     */
123
    public function verify(InputInterface $input, OutputInterface $output)
124
    {
125
        $modulePath = $this->getApplication()->getModulePath($input->getArgument('module'));
126
127
        $paths = [
128
            $modulePath . DS . 'controllers' . DS . 'rest.php',
129
        ];
130
131
        foreach ($paths as $path) {
132
            if (!$this->getFs()->exists($path)) {
133
                throw new Generator\GeneratorException("File `$path` is not exists");
134
            }
135
        }
136
    }
137
}
138