Completed
Pull Request — master (#2254)
by
unknown
11:30
created

MakeCommand::getArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Console;
4
5
use Illuminate\Console\GeneratorCommand;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputOption;
8
9
class MakeCommand extends GeneratorCommand
10
{
11
    /**
12
     * The console command name.
13
     *
14
     * @var string
15
     */
16
    protected $name = 'admin:make';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Make empty admin controller';
24
25
    /**
26
     * Execute the console command.
27
     *
28
     * @return void
29
     */
30
    public function handle()
31
    {
32
        if (!$this->modelExists()) {
33
            $this->error('Model does not exists !');
34
35
            return false;
36
        }
37
38
        parent::handle();
39
    }
40
41
    /**
42
     * Determine if the model is exists.
43
     *
44
     * @return bool
45
     */
46
    protected function modelExists()
47
    {
48
        $model = $this->option('model');
49
50
        if (empty($model)) {
51
            return true;
52
        }
53
54
        return class_exists($model);
55
    }
56
57
    /**
58
     * Replace the class name for the given stub.
59
     *
60
     * @param string $stub
61
     * @param string $name
62
     *
63
     * @return string
64
     */
65
    protected function replaceClass($stub, $name)
66
    {
67
        $stub = parent::replaceClass($stub, $name);
68
69
        return str_replace(
70
            ['DummyModelNamespace', 'DummyModel'],
71
            [$this->option('model'), class_basename($this->option('model'))],
0 ignored issues
show
Bug introduced by
It seems like $this->option('model') targeting Illuminate\Console\Command::option() can also be of type array or null; however, class_basename() does only seem to accept string|object, 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...
72
            $stub
73
        );
74
    }
75
76
    /**
77
     * Get the stub file for the generator.
78
     *
79
     * @return string
80
     */
81
    protected function getStub()
82
    {
83
        if ($this->option('model')) {
84
            return __DIR__.'/stubs/controller.stub';
85
        }
86
87
        return __DIR__.'/stubs/blank.stub';
88
    }
89
90
    /**
91
     * Get the default namespace for the class.
92
     *
93
     * @param string $rootNamespace
94
     *
95
     * @return string
96
     */
97
    protected function getDefaultNamespace($rootNamespace)
98
    {
99
        $directory = config('admin.directory');
100
101
        $namespace = ucfirst(basename($directory));
102
103
        return $rootNamespace."\\$namespace\Controllers";
104
    }
105
106
    /**
107
     * Get the console command arguments.
108
     *
109
     * @return array
110
     */
111
    protected function getArguments()
112
    {
113
        return [
114
            ['name', InputArgument::REQUIRED, 'The name of the controller.'],
115
        ];
116
    }
117
118
    /**
119
     * Get the console command options.
120
     *
121
     * @return array
122
     */
123
    protected function getOptions()
124
    {
125
        return [
126
            ['model', null, InputOption::VALUE_REQUIRED,
127
                'The eloquent model that should be use as controller data source.', ],
128
        ];
129
    }
130
}
131