PivotSeederCommand::getSeederName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php namespace Wn\Generators\Commands;
2
3
4
class PivotSeederCommand extends BaseCommand {
5
6
	protected $signature = 'wn:pivot-seeder
7
        {model1 : Name of the first model or table}
8
        {model2 : Name of the second model or table}
9
        {--count=10 : number of elements to add in database.}
10
        {--force= : override the existing files}
11
    ';
12
13
	protected $description = 'Generates seeder for pivot table';
14
15
    public function handle()
16
    {
17
        $resources = $this->getResources();
18
        $name = $this->getSeederName($resources);
19
        $tables = $this->getTableNames($resources);
20
        $file = "./database/seeds/{$name}.php";
21
22
        $content = $this->getTemplate('pivot-seeder')
23
            ->with([
24
                'first_resource' => $resources[0],
25
                'second_resource' => $resources[1],
26
                'first_table' => $tables[0],
27
                'second_table' => $tables[1],
28
                'name' => $name,
29
                'count' => $this->option('count')
30
            ])
31
            ->get();
32
33
        $this->save($content, $file, $name);
34
    }
35
36
    protected function getResources()
37
    {
38
        $resources = array_map(function($arg) {
39
            return \Illuminate\Support\Str::snake(\Illuminate\Support\Str::singular($this->argument($arg)));
0 ignored issues
show
Bug introduced by
It seems like $this->argument($arg) targeting Illuminate\Console\Command::argument() can also be of type array; however, Illuminate\Support\Str::singular() 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...
40
        }, ['model1', 'model2']);
41
42
        sort($resources);
43
44
        return $resources;
45
    }
46
47
    protected function getSeederName($resources) {
48
        $resources = array_map(function($resource){
49
            return ucwords(\Illuminate\Support\Str::camel($resource));
50
        }, $resources);
51
        return implode('', $resources) . 'TableSeeder';
52
    }
53
54
    protected function getTableNames($resources) {
55
        return array_map('\Illuminate\Support\Str::plural', $resources);
56
    }
57
58
}
59