Completed
Pull Request — master (#99)
by Arjay
01:17
created

DataTablesMakeCommand   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 233
Duplicated Lines 18.45 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 25
lcom 1
cbo 3
dl 43
loc 233
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 20 4
A buildClass() 0 10 1
A replaceFilename() 0 8 1
A replaceAction() 0 8 1
A getAction() 0 4 2
A replaceBuilder() 9 9 1
A qualifyClass() 18 18 4
A getDefaultNamespace() 0 4 1
A replaceModel() 0 8 1
A getModel() 0 11 5
A replaceModelImport() 0 8 1
A getStub() 8 8 2
A getOptions() 8 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Yajra\DataTables\Generators;
4
5
use Illuminate\Support\Str;
6
use Illuminate\Console\GeneratorCommand;
7
use Symfony\Component\Console\Input\InputOption;
8
9
class DataTablesMakeCommand extends GeneratorCommand
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'datatables:make
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
                            {--action= : The path of the action view.}
21
                            {--dom= : The dom of the datatable.}
22
                            {--buttons= : The buttons of the datatable.}
23
                            {--columns= : The columns of the datatable.}';
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'Create a new dataTable service class.';
31
32
    /**
33
     * The type of class being generated.
34
     *
35
     * @var string
36
     */
37
    protected $type = 'DataTable';
38
39
    public function handle()
40
    {
41
        parent::handle();
42
43
        $this->call('datatables:html', [
44
            'name'      => $this->getNameInput(),
45
            '--columns' => $this->option('columns') ?: $this->laravel['config']->get(
46
                'datatables-buttons.generator.columns',
47
                'id,add your columns,created_at,updated_at'
48
            ),
49
            '--buttons' => $this->option('buttons') ?: $this->laravel['config']->get(
50
                'datatables-buttons.generator.buttons',
51
                'create,export,print,reset,reload'
52
            ),
53
            '--dom'     => $this->option('dom') ?: $this->laravel['config']->get(
54
                'datatables-buttons.generator.dom',
55
                'Bfrtip'
56
            ),
57
        ]);
58
    }
59
60
    /**
61
     * Build the class with the given name.
62
     *
63
     * @param string $name
64
     * @return string
65
     */
66
    protected function buildClass($name)
67
    {
68
        $stub = parent::buildClass($name);
69
70
        return $this->replaceModelImport($stub)
71
                    ->replaceModel($stub)
72
                    ->replaceBuilder($stub)
73
                    ->replaceAction($stub)
74
                    ->replaceFilename($stub);
75
    }
76
77
    /**
78
     * Replace the filename.
79
     *
80
     * @param string $stub
81
     * @return string
82
     */
83
    protected function replaceFilename(&$stub)
84
    {
85
        $stub = str_replace(
86
            'DummyFilename', preg_replace('#datatable$#i', '', $this->getNameInput()), $stub
87
        );
88
89
        return $stub;
90
    }
91
92
    /**
93
     * Replace the action.
94
     *
95
     * @param string $stub
96
     * @return \Yajra\DataTables\Generators\DataTablesMakeCommand
97
     */
98
    protected function replaceAction(&$stub)
99
    {
100
        $stub = str_replace(
101
            'DummyAction', $this->getAction(), $stub
102
        );
103
104
        return $this;
105
    }
106
107
    /**
108
     * Set the action view to be used.
109
     *
110
     * @return string
111
     */
112
    protected function getAction()
113
    {
114
        return $this->option('action') ? $this->option('action') : Str::lower($this->getNameInput()) . '.action';
115
    }
116
117
    /**
118
     * Replace builder name.
119
     *
120
     * @param string $stub
121
     * @return \Yajra\DataTables\Generators\DataTablesMakeCommand
122
     */
123 View Code Duplication
    protected function replaceBuilder(&$stub)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
    {
125
        $name  = $this->qualifyClass($this->getNameInput());
126
        $class = str_replace($this->getNamespace($name) . '\\', '', $name);
127
128
        $stub = str_replace('DummyBuilder', $class . 'Html', $stub);
129
130
        return $this;
131
    }
132
133
    /**
134
     * Parse the name and format according to the root namespace.
135
     *
136
     * @param string $name
137
     * @return string
138
     */
139 View Code Duplication
    protected function qualifyClass($name)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
    {
141
        $rootNamespace = $this->laravel->getNamespace();
142
143
        if (Str::startsWith($name, $rootNamespace)) {
144
            return $name;
145
        }
146
147
        if (Str::contains($name, '/')) {
148
            $name = str_replace('/', '\\', $name);
149
        }
150
151
        if (! Str::contains(Str::lower($name), 'datatable')) {
152
            $name .= 'DataTable';
153
        }
154
155
        return $this->getDefaultNamespace(trim($rootNamespace, '\\')) . '\\' . $name;
156
    }
157
158
    /**
159
     * Get the default namespace for the class.
160
     *
161
     * @param string $rootNamespace
162
     * @return string
163
     */
164
    protected function getDefaultNamespace($rootNamespace)
165
    {
166
        return $rootNamespace . '\\' . $this->laravel['config']->get('datatables-buttons.namespace.base', 'DataTables');
167
    }
168
169
    /**
170
     * Replace model name.
171
     *
172
     * @param string $stub
173
     * @return \Yajra\DataTables\Generators\DataTablesMakeCommand
174
     */
175
    protected function replaceModel(&$stub)
176
    {
177
        $model = explode('\\', $this->getModel());
178
        $model = array_pop($model);
179
        $stub  = str_replace('ModelName', $model, $stub);
180
181
        return $this;
182
    }
183
184
    /**
185
     * Get model name to use.
186
     */
187
    protected function getModel()
188
    {
189
        $name           = $this->getNameInput();
190
        $rootNamespace  = $this->laravel->getNamespace();
191
        $model          = $this->option('model') || $this->option('model-namespace');
192
        $modelNamespace = $this->option('model-namespace') ? $this->option('model-namespace') : $this->laravel['config']->get('datatables-buttons.namespace.model');
193
194
        return $model
195
            ? $rootNamespace . '\\' . ($modelNamespace ? $modelNamespace . '\\' : '') . Str::singular($name)
196
            : $rootNamespace . '\\User';
197
    }
198
199
    /**
200
     * Replace model import.
201
     *
202
     * @param string $stub
203
     * @return $this
204
     */
205
    protected function replaceModelImport(&$stub)
206
    {
207
        $stub = str_replace(
208
            'DummyModel', str_replace('\\\\', '\\', $this->getModel()), $stub
209
        );
210
211
        return $this;
212
    }
213
214
    /**
215
     * Get the stub file for the generator.
216
     *
217
     * @return string
218
     */
219 View Code Duplication
    protected function getStub()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
220
    {
221
        $config = $this->laravel['config'];
222
223
        return $config->get('datatables-buttons.stub')
224
            ? base_path() . $config->get('datatables-buttons.stub') . '/datatables.stub'
225
            : __DIR__ . '/stubs/datatables.stub';
226
    }
227
228
    /**
229
     * Get the console command options.
230
     *
231
     * @return array
232
     */
233 View Code Duplication
    protected function getOptions()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
234
    {
235
        return [
236
            ['model', null, InputOption::VALUE_NONE, 'Use the provided name as the model.', null],
237
            ['action', null, InputOption::VALUE_OPTIONAL, 'Path to action column template.', null],
238
            ['columns', null, InputOption::VALUE_OPTIONAL, 'Use the provided columns.', null],
239
        ];
240
    }
241
}
242