Completed
Push — master ( 7d4021...e7c5b3 )
by wen
11:02
created

ComponentMakeCommand::parseClass()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
3
namespace Sco\Admin\Console;
4
5
use Illuminate\Console\GeneratorCommand;
6
use Illuminate\Support\Str;
7
use InvalidArgumentException;
8
use Symfony\Component\Console\Input\InputOption;
9
10
class ComponentMakeCommand extends GeneratorCommand
11
{
12
    /**
13
     * The console command name.
14
     *
15
     * @var string
16
     */
17
    protected $name = 'make:component';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Create a new component class(ScoAdmin)';
25
26
    /**
27
     * The type of class being generated.
28
     *
29
     * @var string
30
     */
31
    protected $type = 'Component';
32
33
    /**
34
     * Execute the console command.
35
     *
36
     * @return void
37
     */
38
    public function handle()
39
    {
40
        if (parent::handle() === false && !$this->option('force')) {
41
            return;
42
        }
43
44
        if ($this->option('observer')) {
45
            $this->createObserver();
46
        }
47
48
        if ($this->option('model')) {
49
            $this->createModel();
50
        }
51
52
    }
53
54
    /**
55
     * Create a new permission observer for the component
56
     */
57
    protected function createObserver()
58
    {
59
        $observerClass = $this->parseClass($this->option('observer'));
0 ignored issues
show
Bug introduced by
It seems like $this->option('observer') targeting Illuminate\Console\Command::option() can also be of type array; however, Sco\Admin\Console\Compon...keCommand::parseClass() 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...
60
61
        //$this->info('observer:' . $observerClass);
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
62
63
        $this->call('make:observer', [
64
            'name' => $observerClass,
65
        ]);
66
    }
67
68
    protected function createModel()
69
    {
70
        $modelClass = $this->parseClass($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; however, Sco\Admin\Console\Compon...keCommand::parseClass() 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...
71
72
        //$this->info('model:' . $modelClass);
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
73
74
        $this->call('make:model', [
75
            'name' => $modelClass,
76
        ]);
77
    }
78
79
    /**
80
     * Get the fully-qualified class name.
81
     *
82
     * @param string $class
83
     *
84
     * @return string
85
     */
86
    protected function parseClass($class)
87
    {
88
        if (preg_match('([^A-Za-z0-9_/\\\\])', $class)) {
89
            throw new InvalidArgumentException('Model name contains invalid characters.');
90
        }
91
92
        $class = trim(str_replace('/', '\\', $class), '\\');
93
94
        if (!Str::startsWith($class, $rootNamespace = $this->laravel->getNamespace())) {
95
            $class = $rootNamespace . $class;
96
        }
97
98
        return $class;
99
    }
100
101
    /**
102
     * Get the stub file for the generator.
103
     *
104
     * @return string
105
     */
106
    protected function getStub()
107
    {
108
        return __DIR__ . '/stubs/component.stub';
109
    }
110
111
    /**
112
     * Build the class with the given name.
113
     *
114
     * @param string $name
115
     *
116
     * @return string
117
     */
118
    protected function buildClass($name)
119
    {
120
        $observer = $this->option('observer')
121
            ? $this->parseClass($this->option('observer'))
0 ignored issues
show
Bug introduced by
It seems like $this->option('observer') targeting Illuminate\Console\Command::option() can also be of type array; however, Sco\Admin\Console\Compon...keCommand::parseClass() 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...
122
            : \Sco\Admin\Component\Observer::class;
123
124
        return str_replace(
125
            'DummyObserver', $observer, parent::buildClass($name)
126
        );
127
    }
128
129
    /**
130
     * Get the default namespace for the class.
131
     *
132
     * @param  string $rootNamespace
133
     *
134
     * @return string
135
     */
136
    protected function getDefaultNamespace($rootNamespace)
137
    {
138
        return $rootNamespace . '\Components';
139
    }
140
141
    /**
142
     * Get the console command options.
143
     *
144
     * @return array
145
     */
146
    protected function getOptions()
147
    {
148
        return [
149
            [
150
                'observer', 'o', InputOption::VALUE_OPTIONAL,
151
                'Generate a new access observer for the component.',
152
            ],
153
            [
154
                'force', null, InputOption::VALUE_NONE,
155
                'Generate the class even if the component already exists.',
156
            ],
157
            [
158
                'model', 'm', InputOption::VALUE_OPTIONAL,
159
                'Generate a model for the component.',
160
            ],
161
162
        ];
163
    }
164
}
165