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

ControllerCommand::generate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 8
cts 8
cp 1
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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\Generator;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
/**
14
 * ControllerCommand
15
 *
16
 * @package  Bluzman\Command\Generate
17
 */
18
class ControllerCommand extends AbstractGenerateCommand
19
{
20
    /**
21
     * Command configuration
22
     */
23 14
    protected function configure()
24
    {
25
        $this
26
            // the name of the command (the part after "bin/bluzman")
27 14
            ->setName('generate:controller')
28
            // the short description shown while running "php bin/bluzman list"
29 14
            ->setDescription('Generate a new controller')
30
            // the full command description shown when running the command with
31
            // the "--help" option
32 14
            ->setHelp('This command allows you to generate controller files')
33
        ;
34
35 14
        $this->addModuleArgument();
36 14
        $this->addControllerArgument();
37 14
        $this->addForceOption();
38 14
    }
39
40
    /**
41
     * @param InputInterface $input
42
     * @param OutputInterface $output
43
     * @return void
44
     */
45 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...
46
    {
47 1
        $this->write('Running <info>generate:controller</info> command');
48
        try {
49
            // validate
50 1
            $this->validateModuleArgument();
51 1
            $this->validateControllerArgument();
52
53
            // generate directories and files
54 1
            $this->generate($input, $output);
55
56
            // verify it
57 1
            $this->verify($input, $output);
58
        } catch (\Exception $e) {
59
            $this->error("ERROR: {$e->getMessage()}");
60
        }
61 1
    }
62
63
    /**
64
     * @param InputInterface $input
65
     * @param OutputInterface $output
66
     * @return void
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
        $module = $input->getArgument('module');
71 1
        $controller = $input->getArgument('controller');
72
73 1
        $controllerFile = $this->getControllerPath($module, $controller);
74 1
        $this->generateFile('ControllerTemplate', $controllerFile);
75
76 1
        $viewFile = $this->getViewPath($module, $controller);
77 1
        $this->generateFile('ViewTemplate', $viewFile, ['name' => $controller]);
78 1
    }
79
80
    /**
81
     * @param InputInterface $input
82
     * @param OutputInterface $output
83
     * @return void
84
     * @throws \Bluzman\Generator\GeneratorException
85
     */
86 1
    public function verify(InputInterface $input, OutputInterface $output) : void
87
    {
88 1
        $module = $input->getArgument('module');
89 1
        $controller = $input->getArgument('controller');
90
91 1
        $modulePath = $this->getApplication()->getModulePath($module);
0 ignored issues
show
Bug introduced by
It seems like $module defined by $input->getArgument('module') on line 88 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...
92
93
        $paths = [
94 1
            $modulePath,
95 1
            $modulePath . DS . 'controllers',
96 1
            $modulePath . DS . 'controllers' . DS . $controller . '.php',
97 1
            $modulePath . DS . 'views',
98 1
            $modulePath . DS . 'views' . DS . $controller . '.phtml',
99
100
        ];
101
102 1
        foreach ($paths as $path) {
103 1
            if (!$this->getFs()->exists($path)) {
104 1
                throw new Generator\GeneratorException("File or directory `$path` is not exists");
105
            }
106
        }
107
108 1
        $this->write(
109 1
            " |> Controller <info>{$controller}</info> has been successfully created " .
110 1
            "in the module <info>{$module}</info>."
111
        );
112 1
    }
113
}
114