Passed
Push — master ( cd2210...3cb518 )
by Thomas
39s
created

MakeBackpackCrudModel::buildClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Webfactor\Laravel\Generators\Commands;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Console\GeneratorCommand;
7
8
class MakeBackpackCrudModel extends GeneratorCommand
9
{
10
    /**
11
     * The console command name.
12
     *
13
     * @var string
14
     */
15
    protected $name = 'make:crud-model';
16
17
    /**
18
     * The name and signature of the console command.
19
     *
20
     * @var string
21
     */
22
    protected $signature = 'make:crud-model {name}';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'Generate a Backpack CRUD model';
30
31
    /**
32
     * The type of class being generated.
33
     *
34
     * @var string
35
     */
36
    protected $type = 'Model';
37
38
    /**
39
     * Get the stub file for the generator.
40
     *
41
     * @return string
42
     */
43
    protected function getStub()
44
    {
45
        return __DIR__.'/../../stubs/crud-model.stub';
46
    }
47
48
    /**
49
     * Get the default namespace for the class.
50
     *
51
     * @param string $rootNamespace
52
     *
53
     * @return string
54
     */
55
    protected function getDefaultNamespace($rootNamespace)
56
    {
57
        return $rootNamespace.'\Models';
58
    }
59
60
    /**
61
     * Replace the table name for the given stub.
62
     *
63
     * @param string $stub
64
     * @param string $name
65
     *
66
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be MakeBackpackCrudModel?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
67
     */
68
    protected function replaceTable(&$stub, $name)
69
    {
70
        $name = ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', str_replace($this->getNamespace($name).'\\', '', $name))), '_');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 128 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
71
72
        $table = Str::snake(Str::plural($name));
73
74
        $stub = str_replace('DummyTable', $table, $stub);
75
76
        return $this;
77
    }
78
79
    /**
80
     * Build the class with the given name.
81
     *
82
     * @param string $name
83
     *
84
     * @return string
85
     */
86
    protected function buildClass($name)
87
    {
88
        $stub = $this->files->get($this->getStub());
89
90
        return $this->replaceNamespace($stub, $name)->replaceTable($stub, $name)->replaceClass($stub, $name);
91
    }
92
93
    /**
94
     * Get the console command options.
95
     *
96
     * @return array
97
     */
98
    protected function getOptions()
99
    {
100
        return [
101
102
        ];
103
    }
104
}
105