Completed
Pull Request — master (#1012)
by
unknown
02:56
created

ObserverMakeCommand::getModelNamespace()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 6
ccs 0
cts 3
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Illuminate\Support\Str;
6
use Nwidart\Modules\Support\Config\GenerateConfigReader;
7
use Nwidart\Modules\Support\Stub;
8
use Nwidart\Modules\Traits\ModuleCommandTrait;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputOption;
11
12
class ObserverMakeCommand extends GeneratorCommand
13
{
14
    use ModuleCommandTrait;
15
16
    /**
17
     * The console command name.
18
     *
19
     * @var string
20
     */
21
    protected $name = 'module:make-observer';
22
23
    protected $argumentName = 'name';
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'Create a new observer class for the specified module.';
31
32
    /**
33
     * Get the default namespace for the class.
34
     *
35
     * @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...
36
     * @return string
37
     */
38
    public function getDefaultNamespace() : string
39
    {
40
        $module = $this->laravel['modules'];
41
42
        return $module->config('paths.generator.observers.namespace') ?: $module->config('paths.generator.observers.path', 'Observers');
43
    }
44
45
    /**
46
     * Get the default namespace for the class.
47
     *
48
     * @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...
49
     * @return string
50
     */
51
    public function getModelNamespace() : string
52
    {
53
        $module = $this->laravel['modules'];
54
55
        return $module->config('paths.generator.model.namespace') ?: $module->config('paths.generator.model.path', 'Entities');
56
    }
57
58
    /**
59
     * Get template contents.
60
     *
61
     * @return string
62
     */
63
    protected function getTemplateContents()
64
    {
65
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
66
67
        if($this->option('model') ) {
68
            return (new Stub('/observer.stub', [
69
                'NAMESPACE' => $this->getClassNamespace($module),
70
                'CLASS'     => $this->getClass(),
71
72
                'NAMESPACEDMODEL'     => $this->getNamespacedModel(),
73
                'DUMMYMODEL'     => $this->getModel(),
74
                'MODEL'     => $this->getModelVariable(),
75
            ]))->render();
76
        }
77
78
        return (new Stub('/observer.plain.stub', [
79
            'NAMESPACE' => $this->getClassNamespace($module),
80
            'CLASS'     => $this->getClass(),
81
        ]))->render();
82
    }
83
84
    /**
85
     * Get the destination file path.
86
     *
87
     * @return string
88
     */
89
    protected function getDestinationFilePath()
90
    {
91
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
92
93
        $notificationPath = GenerateConfigReader::read('observers');
94
95
        return $path . $notificationPath->getPath() . '/' . $this->getFileName() . '.php';
96
    }
97
98
    /**
99
     * @return string
100
     */
101
    private function getFileName()
102
    {
103
        return Str::studly($this->argument('name'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('name') targeting Illuminate\Console\Conce...ractsWithIO::argument() can also be of type array or null; however, Illuminate\Support\Str::studly() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
104
    }
105
106
    /**
107
     * @return string
108
     */
109
    private function getNamespacedModel()
110
    {
111
        $model = $this->option('model');
112
113
        $model = str_replace('/', '\\', $model);
114
115
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
116
        $namespaceModel = $this->getClassNamespace($module, $this->getModelNamespace() ) . '\\' . $model;
117
118
        if (Str::startsWith($model, '\\')) {
119
            return trim($model, '\\');
120
        }
121
122
        return $namespaceModel;
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    private function getModel()
129
    {
130
        $model = $this->option('model');
131
132
        $model = class_basename(trim($model, '\\'));
133
134
        return $model;
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    private function getModelVariable()
141
    {
142
        $model = $this->getModel();
143
144
        return '$' . Str::camel($model);
145
    }
146
147
    /**
148
     * Get the console command arguments.
149
     *
150
     * @return array
151
     */
152 128
    protected function getArguments()
153
    {
154
        return [
155 128
            ['name', InputArgument::REQUIRED, 'The name of the observer class.'],
156
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
157
        ];
158
    }
159
160
    /**
161
     * Get the console command arguments.
162
     *
163
     * @return array
164
     */
165 128
    protected function getOptions()
166
    {
167
        return [
168 128
            ['model', 'm', InputOption::VALUE_OPTIONAL, 'The model that the observer applies to.'],
169
        ];
170
    }
171
}
172