Completed
Pull Request — master (#1088)
by
unknown
02:55
created

SeedMakeCommand::getDestinationFilePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 10
ccs 0
cts 5
cp 0
crap 2
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\CanClearModulesCache;
9
use Nwidart\Modules\Traits\ModuleCommandTrait;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputOption;
12
13
class SeedMakeCommand extends GeneratorCommand
14
{
15
    use ModuleCommandTrait, CanClearModulesCache;
16
17
    protected $argumentName = 'name';
18
19
    /**
20
     * The console command name.
21
     *
22
     * @var string
23
     */
24
    protected $name = 'module:make-seed';
25
26
    /**
27
     * The console command description.
28
     *
29
     * @var string
30
     */
31
    protected $description = 'Generate new seeder for the specified module.';
32
33
    /**
34
     * Get the console command arguments.
35
     *
36
     * @return array
37
     */
38 4
    protected function getArguments()
39
    {
40
        return [
41 4
            ['name', InputArgument::REQUIRED, 'The name of seeder will be created.'],
42
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
43
        ];
44
    }
45
46
    /**
47
     * Get the console command options.
48
     *
49
     * @return array
50
     */
51 4
    protected function getOptions()
52
    {
53
        return [
54
            [
55 4
                'master',
56
                null,
57
                InputOption::VALUE_NONE,
58
                'Indicates the seeder will created is a master database seeder.',
59
            ],
60
        ];
61
    }
62
63
    /**
64
     * @return mixed
65
     */
66
    protected function getTemplateContents()
67
    {
68
        $module = $this->laravel['modules']->findOrFail($this->getModuleName());
69
70
        return (new Stub('/seeder.stub', [
71
            'NAME' => $this->getSeederName(),
72
            'MODULE' => $this->getModuleName(),
73
            'NAMESPACE' => $this->getClassNamespace($module),
74
75
        ]))->render();
76
    }
77
78
    /**
79
     * @return mixed
80
     */
81
    protected function getDestinationFilePath()
82
    {
83
        $this->clearCache();
84
85
        $path = $this->laravel['modules']->getModulePath($this->getModuleName());
86
87
        $seederPath = GenerateConfigReader::read('seeder');
88
89
        return $path . $seederPath->getPath() . '/' . $this->getSeederName() . '.php';
90
    }
91
92
    /**
93
     * Get seeder name.
94
     *
95
     * @return string
96
     */
97
    private function getSeederName()
98
    {
99
        $end = $this->option('master') ? 'DatabaseSeeder' : 'TableSeeder';
100
101
        return Str::studly($this->argument('name')) . $end;
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...
102
    }
103
104
    /**
105
     * Get default namespace.
106
     *
107
     * @return string
108
     */
109
    public function getDefaultNamespace() : string
110
    {
111
        $module = $this->laravel['modules'];
112
113
        return $module->config('paths.generator.seeder.namespace') ?: $module->config('paths.generator.seeder.path', 'Database/Seeders');
114
    }
115
}
116