Passed
Pull Request — master (#35)
by Anton
03:28
created

RestCommand::execute()   A

Complexity

Conditions 2
Paths 5

Size

Total Lines 17
Code Lines 9

Duplication

Lines 17
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 2.0438

Importance

Changes 0
Metric Value
dl 17
loc 17
ccs 7
cts 9
cp 0.7778
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 5
nop 2
crap 2.0438
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
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 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...
44
    {
45 1
        $this->write('Running <info>generate:rest</info> command');
46
        try {
47
            // validate
48 1
            $this->validateModelArgument();
49 1
            $this->validateModuleArgument();
50
51
            // generate directories and files
52 1
            $this->generate($input, $output);
53
54
            // verify it
55 1
            $this->verify($input, $output);
56
        } catch (InputException $e) {
57
            $this->error("ERROR: {$e->getMessage()}");
58
        }
59 1
    }
60
61
    /**
62
     * @param InputInterface $input
63
     * @param OutputInterface $output
64
     * @return void
65
     * @throws InputException
66
     */
67 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...
68
    {
69 1
        $model = ucfirst($input->getArgument('model'));
70 1
        $module = $input->getArgument('module');
71
        $data = [
72 1
            'model' => $model,
73 1
            'module' => $module
74
        ];
75
76 1
        $crudPath = $this->getApplication()->getModelPath($model) . DS . 'Crud.php';
77 1
        if (!is_file($crudPath)) {
78
            throw new InputException(
79
                "CRUD for $model is not exist, " .
80
                "run command <question>bluzman generate:crud $model</question> before"
81
            );
82
        }
83
84 1
        $this->write(" |> Generate <info>$module/controllers/rest.php</info>");
85
86 1
        $controllerFile = $this->getControllerPath($module, 'rest');
87 1
        $this->generateFile('RestTemplate', $controllerFile, $data);
88 1
    }
89
90
    /**
91
     * @param InputInterface $input
92
     * @param OutputInterface $output
93
     * @return void
94
     * @throws \Bluzman\Generator\GeneratorException
95
     */
96 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...
97
    {
98 1
        $model = $input->getArgument('model');
99 1
        $module = $input->getArgument('module');
100
101 1
        $modulePath = $this->getApplication()->getModulePath($module);
102
103
        $paths = [
104 1
            $modulePath . DS . 'controllers' . DS . 'rest.php',
105
        ];
106
107 1
        foreach ($paths as $path) {
108 1
            if (!$this->getFs()->exists($path)) {
109
                throw new Generator\GeneratorException("File `$path` is not exists");
110
            }
111
        }
112
113 1
        $this->write(" |> REST for <info>{$model}</info> has been successfully created.");
114 1
        $this->write(
115
            " |> <options=bold>Open page <info>/acl</info> in your browser ".
116 1
            "and set permissions <info>Management</info> for <info>{$module}</info> module</>"
117
        );
118 1
    }
119
}
120