Completed
Push — master ( 5e3af2...33ddea )
by Arjay
08:05
created

DataTablesEditorCommand::getStub()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Yajra\DataTables\Generators;
4
5
use Illuminate\Console\GeneratorCommand;
6
use Illuminate\Support\Str;
7
use Symfony\Component\Console\Input\InputOption;
8
9
class DataTablesEditorCommand extends GeneratorCommand
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'datatables:editor 
17
                            {name : The name of the datatable.}
18
                            {--model : The name of the model to be used.}
19
                            {--model-namespace= : The namespace of the model to be used.}';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Create a new DataTables Editor class.';
27
28
    /**
29
     * The type of class being generated.
30
     *
31
     * @var string
32
     */
33
    protected $type = 'DataTablesEditor';
34
35
    /**
36
     * Build the class with the given name.
37
     *
38
     * @param  string $name
39
     * @return string
40
     */
41
    protected function buildClass($name)
42
    {
43
        $stub = parent::buildClass($name);
44
45
        return $this->replaceModelImport($stub)->replaceModel($stub);
46
    }
47
48
    /**
49
     * Replace model name.
50
     *
51
     * @param string $stub
52
     * @return string
53
     */
54
    protected function replaceModel(&$stub)
55
    {
56
        $model = explode('\\', $this->getModel());
57
        $model = array_pop($model);
58
        $stub  = str_replace('ModelName', $model, $stub);
59
60
        return $stub;
61
    }
62
63
    /**
64
     * Get model name to use.
65
     */
66
    protected function getModel()
67
    {
68
        $name           = $this->getNameInput();
69
        $rootNamespace  = $this->laravel->getNamespace();
0 ignored issues
show
Bug introduced by
The method getNamespace() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
70
        $model          = $this->option('model') || $this->option('model-namespace');
71
        $modelNamespace = $this->option('model-namespace') ? $this->option('model-namespace') : $this->laravel['config']->get('datatables-buttons.namespace.model');
72
73
        return $model
74
            ? $rootNamespace . '\\' . ($modelNamespace ? $modelNamespace . '\\' : '') . str_singular($name)
75
            : $rootNamespace . '\\User';
76
    }
77
78
    /**
79
     * Replace model import.
80
     *
81
     * @param string $stub
82
     * @return $this
83
     */
84
    protected function replaceModelImport(&$stub)
85
    {
86
        $stub = str_replace(
87
            'DummyModel', str_replace('\\\\', '\\', $this->getModel()), $stub
88
        );
89
90
        return $this;
91
    }
92
93
    /**
94
     * Get the stub file for the generator.
95
     *
96
     * @return string
97
     */
98
    protected function getStub()
99
    {
100
        return __DIR__ . '/stubs/editor.stub';
101
    }
102
103
    /**
104
     * Replace the filename.
105
     *
106
     * @param string $stub
107
     * @return string
108
     */
109
    protected function replaceFilename(&$stub)
110
    {
111
        $stub = str_replace(
112
            'DummyFilename', str_slug($this->getNameInput()), $stub
113
        );
114
115
        return $stub;
116
    }
117
118
    /**
119
     * Parse the name and format according to the root namespace.
120
     *
121
     * @param  string $name
122
     * @return string
123
     */
124
    protected function qualifyClass($name)
125
    {
126
        $rootNamespace = $this->laravel->getNamespace();
0 ignored issues
show
Bug introduced by
The method getNamespace() does not seem to exist on object<Illuminate\Contra...Foundation\Application>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
127
128
        if (Str::startsWith($name, $rootNamespace)) {
129
            return $name;
130
        }
131
132
        if (Str::contains($name, '/')) {
133
            $name = str_replace('/', '\\', $name);
134
        }
135
136
        if (! Str::contains(Str::lower($name), 'datatable')) {
137
            $name .= $this->type;
138
        }
139
140
        return $this->getDefaultNamespace(trim($rootNamespace, '\\')) . '\\' . $name;
141
    }
142
143
    /**
144
     * Get the default namespace for the class.
145
     *
146
     * @param  string $rootNamespace
147
     * @return string
148
     */
149
    protected function getDefaultNamespace($rootNamespace)
150
    {
151
        return $rootNamespace . '\\' . $this->laravel['config']->get('datatables-buttons.namespace.base', 'DataTables');
152
    }
153
}
154