CachedServiceMakeCommand   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 5
dl 0
loc 149
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 13 4
A getStub() 0 12 3
A getDefaultNamespace() 0 12 3
A getNameInput() 0 4 1
A getParentName() 0 4 1
A getParentFqcn() 0 4 1
A buildClass() 0 8 2
A replaceParentClass() 0 6 1
A replaceParentFqcn() 0 6 1
1
<?php
2
3
namespace BrightComponents\Services\Commands;
4
5
use Illuminate\Support\Facades\Config;
6
use Illuminate\Support\Facades\Artisan;
7
use Illuminate\Console\GeneratorCommand;
8
use BrightComponents\Services\Exceptions\InvalidNamespaceException;
9
10
class CachedServiceMakeCommand extends GeneratorCommand
11
{
12
    /** @var string */
13
    protected $signature = 'adr:cache {name} {--parent} {--generate-parent}';
14
15
    /** @var string */
16
    protected $description = 'Create new cached service class.';
17
18
    /** @var string */
19
    protected $type = 'Cached Service';
20
21
    /** @var string */
22
    protected $defaultNamespace;
23
24
    /**
25
     * Execute the console command.
26
     */
27
    public function handle()
28
    {
29
        if (false === parent::handle() && ! $this->option('force')) {
30
            return;
31
        }
32
33
        if ($this->option('parent')) {
34
            Artisan::call('adr:cache', [
35
                'name' => 'BaseCachedService',
36
                '--generate-parent' => true,
37
            ]);
38
        }
39
    }
40
41
    /**
42
     * Get the stub file for the generator.
43
     *
44
     * @return string
45
     */
46
    protected function getStub()
47
    {
48
        if ($this->option('generate-parent')) {
49
            return __DIR__.'/stubs/cached-service-parent.stub';
50
        }
51
52
        if ($this->option('parent')) {
53
            return __DIR__.'/stubs/cached-service-child.stub';
54
        }
55
56
        return __DIR__.'/stubs/cached-service.stub';
57
    }
58
59
    /**
60
     * Get the default namespace for the class.
61
     *
62
     * @param  string  $rootNamespace
63
     *
64
     * @return string
65
     */
66
    protected function getDefaultNamespace($rootNamespace)
67
    {
68
        $namespace = Config::get('service-classes.cached_services.namespace');
69
        $serviceNamespace = Config::get('service-classes.namespace');
70
71
        if (! $serviceNamespace) {
72
            throw InvalidNamespaceException::missingServiceNamespace();
73
        }
74
75
        return $this->defaultNamespace = $namespace ? $rootNamespace.'\\'.Config::get('service-classes.namespace').'\\'.$namespace
76
                      : $rootNamespace.'\\'.Config::get('service-classes.cached_services.namespace');
77
    }
78
79
    /**
80
     * Get the desired class name from the input.
81
     *
82
     * @return string
83
     */
84
    protected function getNameInput()
85
    {
86
        return studly_case(trim($this->argument('name')));
87
    }
88
89
    /**
90
     * Get the parent name for the child class.
91
     *
92
     * @param  string  $rootNamespace
0 ignored issues
show
Bug introduced by
There is no parameter named $rootNamespace. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
93
     *
94
     * @return string
95
     */
96
    protected function getParentName()
97
    {
98
        return Config::get('service-classes.cached_services.parent');
99
    }
100
101
    /**
102
     * Get the parent fqcn for the child class.
103
     *
104
     * @param  string  $rootNamespace
0 ignored issues
show
Bug introduced by
There is no parameter named $rootNamespace. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
105
     *
106
     * @return string
107
     */
108
    protected function getParentFqcn()
109
    {
110
        return $this->defaultNamespace.'\\'.Config::get('service-classes.cached_services.parent');
111
    }
112
113
    /**
114
     * Build the class with the given name.
115
     *
116
     * @param  string  $name
117
     *
118
     * @return string
119
     */
120
    protected function buildClass($name)
121
    {
122
        $stub = $this->files->get($this->getStub());
123
124
        return $this->option('parent')
125
                        ? $this->replaceNamespace($stub, $name)->replaceParentClass($stub)->replaceParentFqcn($stub)->replaceClass($stub, $name)
126
                        : $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
127
    }
128
129
    /**
130
     * Replace the parent class in the given stub.
131
     *
132
     * @param  string  $stub
133
     * @param  string  $name
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
134
     *
135
     * @return $this
136
     */
137
    protected function replaceParentClass(&$stub)
138
    {
139
        $stub = str_replace('{DummyParent}', $this->getParentName(), $stub);
140
141
        return $this;
142
    }
143
144
    /**
145
     * Replace the parent fqcn in the given stub.
146
     *
147
     * @param  string  $stub
148
     * @param  string  $name
0 ignored issues
show
Bug introduced by
There is no parameter named $name. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
149
     *
150
     * @return $this
151
     */
152
    protected function replaceParentFqcn(&$stub)
153
    {
154
        $stub = str_replace('{DummyParentFqcn}', $this->getParentFqcn(), $stub);
155
156
        return $this;
157
    }
158
}
159