Completed
Pull Request — master (#45)
by John
03:27
created

SeedCommand::getArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 3
nc 1
nop 0
crap 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
            $config = $module->get('migration');
58
            if(is_array($config) && array_key_exists('seeds', $config)) {
59
                foreach((array)$config['seeds'] as $class) {
60
                    if (class_exists($class)) {
61
                        $this->dbseed($class);
62
                    } else {
63
                        return $this->error("Class [$class] does not exist");
64
                    }
65
                }
66
            } else {
67
                if (class_exists($this->getSeederName($name))) {
68
                    $this->dbseed($name);
69
                }
70
            }
71
            $this->info("Module [$name] seeded.");
72
        }
73
74
        return $this->info('All modules seeded.');
75
    }
76
77
    /**
78
     * Seed the specified module.
79
     *
80
     * @parama string  $name
81
     *
82
     * @return array
83
     */
84
    protected function dbseed($name)
85
    {
86
        $params = [
87
            '--class' => $this->option('class') ?: $this->getSeederName($name),
88
        ];
89
90
        if ($option = $this->option('database')) {
91
            $params['--database'] = $option;
92
        }
93
94
        if ($option = $this->option('force')) {
95
            $params['--force'] = $option;
96
        }
97
98
        $this->call('db:seed', $params);
99
    }
100
101
    /**
102
     * Get master database seeder name for the specified module.
103
     *
104
     * @param string $name
105
     *
106
     * @return string
107
     */
108
    public function getSeederName($name)
109
    {
110
        $name = Str::studly($name);
111
112
        $namespace = $this->laravel['modules']->config('namespace');
113
114
        return $namespace . '\\' . $name . '\Database\Seeders\\' . $name . 'DatabaseSeeder';
115
    }
116
117
    /**
118
     * Get the console command arguments.
119
     *
120
     * @return array
121
     */
122 38
    protected function getArguments()
123
    {
124
        return array(
125 38
            array('module', InputArgument::OPTIONAL, 'The name of module will be used.'),
126
        );
127
    }
128
129
    /**
130
     * Get the console command options.
131
     *
132
     * @return array
133
     */
134 38
    protected function getOptions()
135
    {
136
        return array(
137 38
            array('class', null, InputOption::VALUE_OPTIONAL, 'The class name of the root seeder', null),
138
            array('all', null, InputOption::VALUE_NONE, 'Whether or not we should seed all modules.'),
139
            array('database', null, InputOption::VALUE_OPTIONAL, 'The database connection to seed.'),
140
            array('force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'),
141
        );
142
    }
143
}
144