Completed
Push — master ( 8e0f66...014191 )
by Nicolas
07:31
created

SeedCommand   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 20.93%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 13
c 2
b 0
f 1
lcom 1
cbo 3
dl 0
loc 124
ccs 9
cts 43
cp 0.2093
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
B fire() 0 33 6
A dbseed() 0 16 4
A getSeederName() 0 8 1
A getArguments() 0 6 1
A getOptions() 0 9 1
1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Str;
7
use Nwidart\Modules\Traits\ModuleCommandTrait;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputOption;
10
11
class SeedCommand extends Command
12
{
13
    use ModuleCommandTrait;
14
15
    /**
16
     * The console command name.
17
     *
18
     * @var string
19
     */
20
    protected $name = 'module:seed';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Run database seeder from the specified module or from all modules.';
28
29
    /**
30
     * Execute the console command.
31
     *
32
     * @return mixed
33
     */
34
    public function fire()
35
    {
36
        $this->module = $this->laravel['modules'];
0 ignored issues
show
Bug introduced by
The property module does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
37
38
        $name = $this->argument('module');
39
40
        if ($name) {
41
            if (!$this->module->has(Str::studly($name))) {
0 ignored issues
show
Bug introduced by
It seems like $name defined by $this->argument('module') on line 38 can also be of type array; however, Illuminate\Support\Str::studly() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
42
                return $this->error("Module [$name] does not exists.");
43
            }
44
45
            $class = $this->getSeederName($name);
0 ignored issues
show
Bug introduced by
It seems like $name defined by $this->argument('module') on line 38 can also be of type array; however, Nwidart\Modules\Commands...ommand::getSeederName() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
46
            if (class_exists($class)) {
47
                $this->dbseed($name);
48
49
                return $this->info("Module [$name] seeded.");
50
            } else {
51
                return $this->error("Class [$class] does not exists.");
52
            }
53
        }
54
55
        foreach ($this->module->getOrdered() as $module) {
56
            $name = $module->getName();
57
58
            if (class_exists($this->getSeederName($name))) {
59
                $this->dbseed($name);
60
61
                $this->info("Module [$name] seeded.");
62
            }
63
        }
64
65
        return $this->info('All modules seeded.');
66
    }
67
68
    /**
69
     * Seed the specified module.
70
     *
71
     * @parama string  $name
72
     *
73
     * @return array
74
     */
75
    protected function dbseed($name)
76
    {
77
        $params = [
78
            '--class' => $this->option('class') ?: $this->getSeederName($name),
79
        ];
80
81
        if ($option = $this->option('database')) {
82
            $params['--database'] = $option;
83
        }
84
85
        if ($option = $this->option('force')) {
86
            $params['--force'] = $option;
87
        }
88
89
        $this->call('db:seed', $params);
90
    }
91
92
    /**
93
     * Get master database seeder name for the specified module.
94
     *
95
     * @param string $name
96
     *
97
     * @return string
98
     */
99
    public function getSeederName($name)
100
    {
101
        $name = Str::studly($name);
102
103
        $namespace = $this->laravel['modules']->config('namespace');
104
105
        return $namespace . '\\' . $name . '\Database\Seeders\\' . $name . 'DatabaseSeeder';
106
    }
107
108
    /**
109
     * Get the console command arguments.
110
     *
111
     * @return array
112
     */
113 32
    protected function getArguments()
114
    {
115
        return array(
116 32
            array('module', InputArgument::OPTIONAL, 'The name of module will be used.'),
117 32
        );
118
    }
119
120
    /**
121
     * Get the console command options.
122
     *
123
     * @return array
124
     */
125 32
    protected function getOptions()
126
    {
127
        return array(
128 32
            array('class', null, InputOption::VALUE_OPTIONAL, 'The class name of the root seeder', null),
129 32
            array('all', null, InputOption::VALUE_NONE, 'Whether or not we should seed all modules.'),
130 32
            array('database', null, InputOption::VALUE_OPTIONAL, 'The database connection to seed.'),
131 32
            array('force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'),
132 32
        );
133
    }
134
}
135