Completed
Pull Request — master (#1095)
by
unknown
02:49
created

FactoryMakeCommand::getClassNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 12
ccs 6
cts 6
cp 1
crap 1
rs 9.8666
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 FactoryMakeCommand extends GeneratorCommand
13
{
14
    use ModuleCommandTrait;
15
16
    /**
17
     * The name of argument name.
18
     *
19
     * @var string
20
     */
21
    protected $argumentName = 'name';
22
23
    /**
24
     * The console command name.
25
     *
26
     * @var string
27
     */
28
    protected $name = 'module:make-factory';
29
30
    /**
31
     * The console command description.
32
     *
33
     * @var string
34
     */
35
    protected $description = 'Create a new model factory for the specified module.';
36
37
    /**
38
     * Get the console command arguments.
39
     *
40
     * @return array
41
     */
42 130
    protected function getArguments()
43
    {
44
        return [
45 130
            ['name', InputArgument::REQUIRED, 'The name of the factory.'],
46
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
47
        ];
48
    }
49
50
    /**
51
     * @return mixed
52
     */
53 1 View Code Duplication
    protected function getTemplateContents()
54
    {
55 1
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
56
57 1
        return (new Stub('/factory.stub', [
58 1
            'NAMESPACE'         => $this->getClassNamespace($module),
59 1
            'CLASS'             => $this->getClass(),
60 1
            'MODEL_CLASS'        => $this->getModelClass(),
61 1
            'MODEL_NAME'        => $this->getModelName(),
62 1
        ]))->render();
63
    }
64
65
    /**
66
     * @return mixed
67
     */
68 1
    protected function getDestinationFilePath()
69
    {
70 1
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
71
72 1
        $factoryPath = GenerateConfigReader::read('factory');
73
74 1
        return $path . $factoryPath->getPath() . '/' . $this->getFileName();
75
    }
76
77
    /**
78
     * Get class name.
79
     *
80
     * @return string
81
     */
82 1
    public function getClass()
83
    {
84 1
        $studlyName = Str::studly( $this->argument($this->argumentName));
0 ignored issues
show
Bug introduced by
It seems like $this->argument($this->argumentName) 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...
85
86 1
        if (!str_contains($studlyName, 'Factory'))
87
            $studlyName .= 'Factory';
88
89 1
        return $studlyName;
90
    }
91
92
93
    /**
94
     * Get model class name.
95
     *
96
     * @return string
97
     */
98 1
    public function getModelClass()
99
    {
100 1
        $model = $this->option('model');
101
102 1
        if (!$model) {
103 1
            $module = $this->laravel['modules']->findOrFail($this->getModuleName());
104 1
            $modules = $this->laravel['modules'];
105
106 1
            $namespace = $this->laravel['modules']->config('namespace');
107
108 1
            $namespace .= '\\' . $module->getStudlyName();
109
110 1
            $namespace .= '\\' .  $modules->config('paths.generator.model.path', 'Entities');
111
112 1
            $namespace .= '\\' .  str_replace('Factory', '', $this->argument($this->argumentName));
113
114 1
            if (class_exists($namespace))
115
                $model = $namespace;
116
117
        }
118
119 1
        return $model ?: 'Illuminate\\Database\\Eloquent\\Model';
120
    }
121
122
123
    /**
124
     * Get model class name.
125
     *
126
     * @return string
127
     */
128 1
    public function getModelName()
129
    {
130 1
        return class_basename($this->getModelClass());
131
    }
132
133
    /**
134
     * @return string
135
     */
136 1
    private function getFileName()
137
    {
138 1
        return $this->getClass() . '.php';
139
    }
140
141
142
    /**
143
     * Get the console command options.
144
     *
145
     * @return array
146
     */
147 130
    protected function getOptions()
148
    {
149
        return [
150 130
            ['model', 'm', InputOption::VALUE_OPTIONAL, 'The model to factory.', null],
151
        ];
152
    }
153
154 1
    public function getDefaultNamespace() : string
155
    {
156 1
        $module = $this->laravel['modules'];
157
158 1
        return $module->config('paths.generator.factory.namespace') ?: $module->config('paths.generator.factory.path', 'Database\Factories');
159
    }
160
161
    /**
162
     * Get class namespace.
163
     *
164
     * @param \Nwidart\Modules\Module $module
165
     *
166
     * @return string
167
     */
168 1
    public function getClassNamespace($module)
169
    {
170 1
        $namespace = $this->laravel['modules']->config('namespace');
171
172 1
        $namespace .= '\\' . $module->getStudlyName();
173
174 1
        $namespace .= '\\' . $this->getDefaultNamespace();
175
176 1
        $namespace = str_replace('/', '\\', $namespace);
177
178 1
        return trim($namespace, '\\');
179
    }
180
}
181