Completed
Push — master ( 12da03...12da03 )
by
unknown
12:55
created

ComponentMakeCommand::createModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
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
49
    /**
50
     * Create a new permission observer for the component
51
     */
52
    protected function createObserver()
53
    {
54
        $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...
55
56
        //$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...
57
58
        $this->call('make:observer', [
59
            'name' => $observerClass,
60
        ]);
61
    }
62
63
    /**
64
     * Get the fully-qualified class name.
65
     *
66
     * @param string $class
67
     *
68
     * @return string
69
     */
70
    protected function parseClass($class)
71
    {
72
        if (preg_match('([^A-Za-z0-9_/\\\\])', $class)) {
73
            throw new InvalidArgumentException('Model name contains invalid characters.');
74
        }
75
76
        $class = trim(str_replace('/', '\\', $class), '\\');
77
78
        if (!Str::startsWith($class, $rootNamespace = $this->laravel->getNamespace())) {
79
            $class = $rootNamespace . $class;
80
        }
81
82
        return $class;
83
    }
84
85
    /**
86
     * Get the stub file for the generator.
87
     *
88
     * @return string
89
     */
90
    protected function getStub()
91
    {
92
        return __DIR__ . '/stubs/component.stub';
93
    }
94
95
    /**
96
     * Build the class with the given name.
97
     *
98
     * @param string $name
99
     *
100
     * @return string
101
     */
102
    protected function buildClass($name)
103
    {
104
        $observer = $this->option('observer')
105
            ? $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...
106
            : \Sco\Admin\Component\Observer::class;
107
108
        return str_replace(
109
            'DummyObserver', $observer, parent::buildClass($name)
110
        );
111
    }
112
113
    /**
114
     * Get the default namespace for the class.
115
     *
116
     * @param  string $rootNamespace
117
     *
118
     * @return string
119
     */
120
    protected function getDefaultNamespace($rootNamespace)
121
    {
122
        return $rootNamespace . '\Components';
123
    }
124
125
    /**
126
     * Get the console command options.
127
     *
128
     * @return array
129
     */
130
    protected function getOptions()
131
    {
132
        return [
133
            [
134
                'observer', 'o', InputOption::VALUE_OPTIONAL,
135
                'Generate a new access observer for the component.',
136
            ],
137
            [
138
                'force', null, InputOption::VALUE_NONE,
139
                'Generate the class even if the component already exists.',
140
            ],
141
        ];
142
    }
143
}
144