Completed
Pull Request — master (#38)
by Anton
12:38
created

RestCommand::verify()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23

Duplication

Lines 23
Ratio 100 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
dl 23
loc 23
ccs 12
cts 12
cp 1
rs 9.552
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 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 Bluzman\Input\InputException;
10
use Bluzman\Generator;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
/**
15
 * ModelCommand
16
 *
17
 * @package  Bluzman\Command\Generate
18
 */
19
class RestCommand extends AbstractGenerateCommand
20
{
21
    /**
22
     * Command configuration
23
     */
24 14
    protected function configure()
25
    {
26
        $this
27
            // the name of the command (the part after "bin/bluzman")
28 14
            ->setName('generate:rest')
29
            // the short description shown while running "php bin/bluzman list"
30 14
            ->setDescription('Generate a REST controller')
31
            // the full command description shown when running the command with
32
            // the "--help" option
33 14
            ->setHelp('This command allows you to generate REST controller')
34
        ;
35
36 14
        $this->addModelArgument();
37 14
        $this->addModuleArgument();
38 14
        $this->addForceOption();
39 14
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 1 View Code Duplication
    protected function execute(InputInterface $input, OutputInterface $output) : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46 1
        $this->write('Running <info>generate:rest</info> command');
47
        try {
48
            // validate
49 1
            $this->validateModelArgument();
50 1
            $this->validateModuleArgument();
51
52
            // generate directories and files
53 1
            $this->generate($input, $output);
54
55
            // verify it
56 1
            $this->verify($input, $output);
57
        } catch (InputException $e) {
58
            $this->error("ERROR: {$e->getMessage()}");
59
        }
60 1
    }
61
62
    /**
63
     * @param InputInterface $input
64
     * @param OutputInterface $output
65
     * @return void
66
     * @throws InputException
67
     */
68 1
    protected function generate(InputInterface $input, OutputInterface $output) : void
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...
69
    {
70 1
        $model = ucfirst($input->getArgument('model'));
71 1
        $module = $input->getArgument('module');
72
        $data = [
73 1
            'model' => $model,
74 1
            'module' => $module
75
        ];
76
77 1
        $crudPath = $this->getApplication()->getModelPath($model) . DS . 'Crud.php';
78 1
        if (!is_file($crudPath)) {
79
            throw new InputException(
80
                "CRUD for $model is not exist, " .
81
                "run command <question>bluzman generate:crud $model</question> before"
82
            );
83
        }
84
85 1
        $this->write(" |> Generate <info>$module/controllers/rest.php</info>");
86
87 1
        $controllerFile = $this->getControllerPath($module, 'rest');
88 1
        $this->generateFile('RestTemplate', $controllerFile, $data);
89 1
    }
90
91
    /**
92
     * @param InputInterface $input
93
     * @param OutputInterface $output
94
     * @return void
95
     * @throws \Bluzman\Generator\GeneratorException
96
     */
97 1 View Code Duplication
    public function verify(InputInterface $input, OutputInterface $output) : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
    {
99 1
        $model = $input->getArgument('model');
100 1
        $module = $input->getArgument('module');
101
102 1
        $modulePath = $this->getApplication()->getModulePath($module);
0 ignored issues
show
Bug introduced by
It seems like $module defined by $input->getArgument('module') on line 100 can also be of type array<integer,string> or null; however, Bluzman\Application\Application::getModulePath() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
103
104
        $paths = [
105 1
            $modulePath . DS . 'controllers' . DS . 'rest.php',
106
        ];
107
108 1
        foreach ($paths as $path) {
109 1
            if (!$this->getFs()->exists($path)) {
110 1
                throw new Generator\GeneratorException("File `$path` is not exists");
111
            }
112
        }
113
114 1
        $this->write(" |> REST for <info>{$model}</info> has been successfully created.");
115 1
        $this->write(
116
            " |> <options=bold>Open page <info>/acl</info> in your browser " .
117 1
            "and set permissions <info>Management</info> for <info>{$module}</info> module</>"
118
        );
119 1
    }
120
}
121