Passed
Pull Request — master (#15)
by Richard
06:30
created

ArtomatorModelCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 26
c 1
b 0
f 0
dl 0
loc 110
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getStub() 0 10 2
A handle() 0 10 3
A getDefaultNamespace() 0 3 1
A buildClass() 0 11 1
A getOptions() 0 5 1
1
<?php
2
3
namespace PWWEB\Artomator\Commands;
4
5
use InvalidArgumentException;
6
use PWWEB\Artomator\Artomator;
7
use Illuminate\Support\Str;
8
use Symfony\Component\Console\Input\InputOption;
9
10
class ArtomatorModelCommand extends Artomator
11
{
12
    /**
13
     * The console command name.
14
     *
15
     * @var string
16
     */
17
    protected $name = 'artomator:model';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Create a new Eloquent model';
25
26
    /**
27
     * The type of class being generated.
28
     *
29
     * @var string
30
     */
31
    protected $type = 'Model';
32
33
    /**
34
     * The schema variable.
35
     *
36
     * @var string
37
     */
38
    protected $schema;
39
40
    /**
41
     * Get the stub file for the generator.
42
     *
43
     * @return string
44
     */
45
    protected function getStub()
46
    {
47
        $stub = 'model.stub';
48
        $path = base_path() . config('artomator.stubPath');
49
        $path = $path . $stub;
50
51
        if (file_exists($path) === true) {
52
            return $path;
53
        } else {
54
            return __DIR__ . '/Stubs/' . $stub;
55
        }
56
    }
57
58
    /**
59
     * Get the default namespace for the class.
60
     *
61
     * @param string $rootNamespace The class name to return FQN for.
62
     *
63
     * @return string
64
     */
65
    protected function getDefaultNamespace($rootNamespace)
66
    {
67
        return $rootNamespace . '\Models';
68
    }
69
70
    /**
71
     * Execute the console command.
72
     *
73
     * @return boolean
74
     */
75
    public function handle()
76
    {
77
78
        $this->schema = $this->option('schema');
79
80
        if (parent::handle() === false and $this->option('force') !== false) {
81
            return false;
82
        }
83
84
        return true;
85
    }
86
87
    /**
88
     * Build the class with the given name.
89
     *
90
     * Remove the base controller import if we are already in base namespace.
91
     *
92
     * @param string $name The name of the model to build.
93
     *
94
     * @return string
95
     */
96
    protected function buildClass($name)
97
    {
98
99
        $table = Str::snake(Str::pluralStudly(str_replace('/', '', $this->argument('name'))));
0 ignored issues
show
Unused Code introduced by
The assignment to $table is dead and can be removed.
Loading history...
100
101
        $replace = parent::buildModelReplacements();
102
103
        return str_replace(
104
            array_keys($replace),
105
            array_values($replace),
106
            parent::buildClass($name)
107
        );
108
    }
109
110
    /**
111
     * Get the console command options.
112
     *
113
     * @return array
114
     */
115
    protected function getOptions()
116
    {
117
        return [
118
            ['schema', 's', InputOption::VALUE_OPTIONAL, 'Optional schema to be attached to the migration', null],
119
            ['force', 'f', InputOption::VALUE_OPTIONAL, 'Force the generation of the model again', false]
120
        ];
121
    }
122
}
123