getDefaultNamespace()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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