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

ControllerCommand::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\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
    }
38
39
    /**
40
     * @param InputInterface $input
41
     * @param OutputInterface $output
42
     * @return void
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:controller</info> command');
47
        try {
48
            // validate
49 1
            $this->validateModuleArgument();
50 1
            $this->validateControllerArgument();
51
52
            // generate directories and files
53 1
            $this->generate($input, $output);
54
55
            // verify it
56 1
            $this->verify($input, $output);
57
        } catch (\Exception $e) {
58
            $this->error("ERROR: {$e->getMessage()}");
59
        }
60 1
    }
61
62
    /**
63
     * @param InputInterface $input
64
     * @param OutputInterface $output
65
     * @return void
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
        $module = $input->getArgument('module');
70 1
        $controller = $input->getArgument('controller');
71
72 1
        $controllerFile = $this->getControllerPath($module, $controller);
73 1
        $this->generateFile('ControllerTemplate', $controllerFile);
74
75 1
        $viewFile = $this->getViewPath($module, $controller);
76 1
        $this->generateFile('ViewTemplate', $viewFile, ['name' => $controller]);
77 1
    }
78
79
    /**
80
     * @param InputInterface $input
81
     * @param OutputInterface $output
82
     * @return void
83
     * @throws \Bluzman\Generator\GeneratorException
84
     */
85 1
    public function verify(InputInterface $input, OutputInterface $output) : void
86
    {
87 1
        $module = $input->getArgument('module');
88 1
        $controller = $input->getArgument('controller');
89
90 1
        $modulePath = $this->getApplication()->getModulePath($module);
91
92
        $paths = [
93 1
            $modulePath,
94 1
            $modulePath . DS . 'controllers',
95 1
            $modulePath . DS . 'controllers' . DS . $controller . '.php',
96 1
            $modulePath . DS . 'views',
97 1
            $modulePath . DS . 'views' . DS . $controller . '.phtml',
98
99
        ];
100
101 1
        foreach ($paths as $path) {
102 1
            if (!$this->getFs()->exists($path)) {
103
                throw new Generator\GeneratorException("File or directory `$path` is not exists");
104
            }
105
        }
106
107 1
        $this->write(
108 1
            " |> Controller <info>{$controller}</info> has been successfully created " .
109 1
            "in the module <info>{$module}</info>."
110
        );
111 1
    }
112
}
113