Completed
Push — master ( 3086dd...6424ee )
by Nicolas
17s queued 10s
created

FactoryMakeCommand::getTemplateContents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 10
ccs 3
cts 3
cp 1
crap 1
rs 9.9332
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
11
class FactoryMakeCommand extends GeneratorCommand
12
{
13
    use ModuleCommandTrait;
14
15
    /**
16
     * The name of argument name.
17
     *
18
     * @var string
19
     */
20
    protected $argumentName = 'name';
21
22
    /**
23
     * The console command name.
24
     *
25
     * @var string
26
     */
27
    protected $name = 'module:make-factory';
28
29
    /**
30
     * The console command description.
31
     *
32
     * @var string
33
     */
34
    protected $description = 'Create a new model factory for the specified module.';
35
36
    /**
37
     * Get the console command arguments.
38
     *
39
     * @return array
40
     */
41 130
    protected function getArguments()
42
    {
43
        return [
44 130
            ['name', InputArgument::REQUIRED, 'The name of the model.'],
45
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
46
        ];
47
    }
48
49
    /**
50
     * @return mixed
51
     */
52 1
    protected function getTemplateContents()
53
    {
54 1
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
55
56
        return (new Stub('/factory.stub', [
57
            'NAMESPACE' => $this->getClassNamespace($module),
58
            'NAME' => $this->getModelName(),
59
            'MODEL_NAMESPACE' => $this->getModelNamespace(),
60 1
        ]))->render();
61
    }
62 1
63
    /**
64 1
     * @return mixed
65
     */
66 1
    protected function getDestinationFilePath()
67
    {
68
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
69
70
        $factoryPath = GenerateConfigReader::read('factory');
71
72 1
        return $path . $factoryPath->getPath() . '/' . $this->getFileName();
73
    }
74 1
75
    /**
76
     * @return string
77
     */
78
    private function getFileName()
79
    {
80
        return Str::studly($this->argument('name')) . 'Factory.php';
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...
81
    }
82
83
    /**
84
     * @return mixed|string
85
     */
86
    private function getModelName()
87
    {
88
        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...
89
    }
90
91
    /**
92
     * Get default namespace.
93
     *
94
     * @return string
95
     */
96
    public function getDefaultNamespace(): string
97
    {
98
        $module = $this->laravel['modules'];
99
100
        return $module->config('paths.generator.factory.namespace') ?: $module->config('paths.generator.factory.path');
101
    }
102
103
    /**
104
     * Get model namespace.
105
     *
106
     * @return string
107
     */
108
    public function getModelNamespace(): string
109
    {
110
        return $this->laravel['modules']->config('namespace') . '\\' . $this->laravel['modules']->findOrFail($this->getModuleName()) . '\\' . $this->laravel['modules']->config('paths.generator.model.path', 'Entities');
111
    }
112
}
113