RegisterServiceDataTableCommand   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 18
c 2
b 0
f 1
dl 0
loc 74
rs 10
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A replaceClass() 0 5 1
A repalceModelNamespace() 0 8 2
A replaceModelName() 0 3 1
A getDefaultNamespace() 0 3 1
A replaceNamespace() 0 10 1
A getStub() 0 3 1
1
<?php
2
3
namespace Laravel\DataTables\Commands;
4
5
use Illuminate\Console\GeneratorCommand;
6
use Illuminate\Support\Str;
7
8
class RegisterServiceDataTableCommand extends GeneratorCommand
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $signature = 'datatable:register {name : The name for the datatable service to be created} {model : The name of the model that datatable service is associated with}';
14
15
    /**
16
     * @var string
17
     */
18
    protected $type = 'Datatable';
19
20
    /**
21
     * @param $name
22
     */
23
    public function replaceModelName($name)
24
    {
25
        return str_replace($this->getNamespace($name).'\\', '', $name);
26
    }
27
28
    /**
29
     * @param $stub
30
     * @param $name
31
     * @return mixed
32
     */
33
    public function replaceNamespace(&$stub, $name)
34
    {
35
        $modelName = $this->replaceModelName($this->argument('model'));
36
        $stub = str_replace(
37
            ['NamespacedDummyModel', 'DummyNamespace', 'DummyModel', 'dummies'],
38
            [$this->repalceModelNamespace($this->argument('model')), $this->getNamespace($name), $modelName, strtolower(Str::plural($modelName))],
39
            $stub
40
        );
41
42
        return $this;
43
    }
44
45
    /**
46
     * @param $rootNamespace
47
     * @return mixed
48
     */
49
    protected function getDefaultNamespace($rootNamespace)
50
    {
51
        return $rootNamespace.'\Services';
52
    }
53
54
    protected function getStub()
55
    {
56
        return __DIR__.'/stubs/DatatableService.stub';
57
    }
58
59
    /**
60
     * @param $stub
61
     * @param $name
62
     */
63
    protected function repalceModelNamespace($name)
64
    {
65
        $class = str_replace($this->getNamespace($name).'\\', '', $name);
66
        if ($namespace = $this->getNamespace($name)) {
67
            return sprintf('%s\\%s', $namespace, $class);
68
        }
69
70
        return sprintf('%s\\%s', 'App', $class);
71
    }
72
73
    /**
74
     * @param $stub
75
     * @param $name
76
     */
77
    protected function replaceClass($stub, $name)
78
    {
79
        $class = str_replace($this->getNamespace($name).'\\', '', $name);
80
81
        return str_replace('DummiesDatatableService', $class, parent::replaceClass($stub, $name));
82
    }
83
}
84