Completed
Push — master ( 15e02a...758c52 )
by Nicolas
05:13
created

ControllerCommand::getControllerName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.1481

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 4
cts 6
cp 0.6667
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 2.1481
1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Pingpong\Support\Stub;
6
use Nwidart\Modules\Traits\ModuleCommandTrait;
7
use Symfony\Component\Console\Input\InputArgument;
8
9
class ControllerCommand extends GeneratorCommand
10
{
11
    use ModuleCommandTrait;
12
13
    /**
14
     * The name of argument being used.
15
     *
16
     * @var string
17
     */
18
    protected $argumentName = 'controller';
19
20
    /**
21
     * The console command name.
22
     *
23
     * @var string
24
     */
25
    protected $name = 'module:make-controller';
26
27
    /**
28
     * The console command description.
29
     *
30
     * @var string
31
     */
32
    protected $description = 'Generate new restful controller for the specified module.';
33
34
    /**
35
     * Get controller name.
36
     *
37
     * @return string
38
     */
39 16
    public function getDestinationFilePath()
40
    {
41 16
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
42
43 16
        $controllerPath = $this->laravel['modules']->config('paths.generator.controller');
44
45 16
        return $path.$controllerPath.'/'.$this->getControllerName().'.php';
46
    }
47
48
    /**
49
     * @return Stub
50
     */
51 16
    protected function getTemplateContents()
52
    {
53 16
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
54
55 16
        return (new Stub('/controller.stub', [
56 16
            'MODULENAME'        => $module->getStudlyName(),
57 16
            'CONTROLLERNAME'    => $this->getControllerName(),
58 16
            'NAMESPACE'         => $module->getStudlyName(),
59 16
            'CLASS_NAMESPACE'   => $this->getClassNamespace($module),
60 16
            'CLASS'             => $this->getClass(),
61 16
            'LOWER_NAME'        => $module->getLowerName(),
62 16
            'MODULE'            => $this->getModuleName(),
63 16
            'NAME'              => $this->getModuleName(),
64 16
            'STUDLY_NAME'       => $module->getStudlyName(),
65 16
            'MODULE_NAMESPACE'  => $this->laravel['modules']->config('namespace'),
66 16
        ]))->render();
67
    }
68
69
    /**
70
     * Get the console command arguments.
71
     *
72
     * @return array
73
     */
74 16
    protected function getArguments()
75
    {
76
        return array(
77 16
            array('controller', InputArgument::REQUIRED, 'The name of the controller class.'),
78 16
            array('module', InputArgument::OPTIONAL, 'The name of module will be used.'),
79 16
        );
80
    }
81
82
    /**
83
     * @return array|string
84
     */
85 16
    protected function getControllerName()
86
    {
87 16
        $controller = studly_case($this->argument('controller'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('controller') targeting Illuminate\Console\Command::argument() can also be of type array; however, studly_case() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
88
89 16
        if (!str_contains(strtolower($controller), 'controller')) {
90
            $controller = $controller.'Controller';
91
        }
92
93 16
        return $controller;
94
    }
95
96
    /**
97
     * Get default namespace.
98
     *
99
     * @return string
100
     */
101 16
    public function getDefaultNamespace()
102
    {
103 16
        return 'Http\Controllers';
104
    }
105
}
106