ModelMakeCommand   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 111
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 4

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A handle() 0 16 3
A getDefaultNamespace() 0 4 1
A getStub() 0 4 1
A generateFile() 0 10 1
A toTable() 0 4 1
1
<?php
2
3
namespace LaravelPlus\Extension\Generators\Commands;
4
5
use Jumilla\Generators\Laravel\OneFileGeneratorCommand as BaseCommand;
6
use Jumilla\Generators\FileGenerator;
7
use LaravelPlus\Extension\Addons\Addon;
8
use LaravelPlus\Extension\Generators\GeneratorCommandTrait;
9
use Illuminate\Support\Str;
10
11
class ModelMakeCommand extends BaseCommand
12
{
13
    use GeneratorCommandTrait;
14
15
    /**
16
     * The console command singature.
17
     *
18
     * @var stringphp
19
     */
20
    protected $signature = 'make:model
21
        {name : The name of the class}
22
        {--a|addon= : The name of the addon}
23
        {--m|migration= : Create a new migration file for the model}
24
    ';
25
26
    /**
27
     * The console command description.
28
     *
29
     * @var string
30
     */
31
    protected $description = '[+] Create a new Eloquent model class';
32
33
    /**
34
     * The type of class being generated.
35
     *
36
     * @var string
37
     */
38
    protected $type = 'Model';
39
40
    /**
41
     * The constructor.
42
     */
43 6
    public function __construct()
44
    {
45 6
        parent::__construct();
46
47 6
        $this->setStubDirectory(__DIR__.'/../stubs');
48 6
    }
49
50
    /**
51
     * Execute the console command.
52
     */
53 4
    public function handle()
54
    {
55 4
        $this->addon = $this->getAddon();
0 ignored issues
show
Documentation Bug introduced by Fumio Furukawa
It seems like $this->getAddon() of type string is incompatible with the declared type object<LaravelPlus\Extension\Addons\Addon> of property $addon.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
56
57 3
        if (parent::handle() !== false) {
58 3
            if ($this->option('migration')) {
59 1
                $table = $this->toTable($this->argument('name'));
60
61 1
                $this->call('make:migration', [
62 1
                    'name' => $this->option('migration'),
63 1
                    '--addon' => $this->option('addon'),
64 1
                    '--create' => $table,
65
                ]);
66
            }
67
        }
68 3
    }
69
70
    /**
71
     * Get the default namespace for the class.
72
     *
73
     * @return string
74
     */
75 3
    protected function getDefaultNamespace()
76
    {
77 3
        return $this->getRootNamespace();
78
    }
79
80
    /**
81
     * Get the stub file for the generator.
82
     *
83
     * @return string
84
     */
85 3
    protected function getStub()
86
    {
87 3
        return 'model.stub';
88
    }
89
90
    /**
91
     * Generate file.
92
     *
93
     * @param \Jumilla\Generators\FileGenerator $generator
94
     * @param string $path
95
     * @param string $fqcn
96
     *
97
     * @return bool
98
     */
99 3
    protected function generateFile(FileGenerator $generator, $path, $fqcn)
100
    {
101 3
        list($namespace, $class) = $this->splitFullQualifyClassName($fqcn);
102
103 3
        return $generator->file($path)->template($this->getStub(), [
104 3
            'namespace' => $namespace,
105 3
            'class' => $class,
106 3
            'table' => $this->toTable($class),
107
        ]);
108
    }
109
110
    /**
111
     * Convert name to table.
112
     *
113
     * @param string $name
114
     *
115
     * @return string
116
     */
117 3
    protected function toTable($name)
118
    {
119 3
        return Str::plural(Str::snake(class_basename($name)));
120
    }
121
}
122