Completed
Push — master ( eca1e1...16b172 )
by Nicolas
13:25
created

SeedCommand::handle()   A

Complexity

Conditions 3
Paths 8

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 8
nop 0
dl 0
loc 15
ccs 0
cts 10
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Str;
7
use Nwidart\Modules\Module;
8
use Nwidart\Modules\Repository;
9
use Nwidart\Modules\Traits\ModuleCommandTrait;
10
use RuntimeException;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputOption;
13
14
class SeedCommand extends Command
15
{
16
    use ModuleCommandTrait;
17
18
    /**
19
     * The console command name.
20
     *
21
     * @var string
22
     */
23
    protected $name = 'module:seed';
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'Run database seeder from the specified module or from all modules.';
31
32
    /**
33
     * Execute the console command.
34
     */
35
    public function handle()
36
    {
37
        try {
38
            if ($name = $this->argument('module')) {
39
                $name = Str::studly($name);
0 ignored issues
show
Bug introduced by
It seems like $name 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...
40
                $this->moduleSeed($this->getModuleByName($name));
41
            } else {
42
                $modules = $this->getModuleRepository()->getOrdered();
43
                array_walk($modules, [$this, 'moduleSeed']);
44
                $this->info('All modules seeded.');
45
            }
46
        } catch (\Exception $e) {
47
            $this->error($e->getMessage());
48
        }
49
    }
50
51
    /**
52
     * @throws RuntimeException
53
     *
54
     * @return Repository
55
     */
56
    public function getModuleRepository()
57
    {
58
        $modules = $this->laravel['modules'];
59
        if (!$modules instanceof Repository) {
60
            throw new RuntimeException("Module repository not found!");
61
        }
62
63
        return $modules;
64
    }
65
66
    /**
67
     * @param $name
68
     *
69
     * @throws RuntimeException
70
     *
71
     * @return Module
72
     */
73
    public function getModuleByName($name)
74
    {
75
        $modules = $this->getModuleRepository();
76
        if ($modules->has($name) === false) {
77
            throw new RuntimeException("Module [$name] does not exists.");
78
        }
79
80
        return $modules->get($name);
81
    }
82
83
    /**
84
     * @param Module $module
85
     *
86
     * @return void
87
     */
88
    public function moduleSeed(Module $module)
89
    {
90
        $seeders = [];
91
        $name = $module->getName();
92
        $config = $module->get('migration');
93
        if (is_array($config) && array_key_exists('seeds', $config)) {
94
            foreach ((array)$config['seeds'] as $class) {
95
                if (class_exists($class)) {
96
                    $seeders[] = $class;
97
                }
98
            }
99
        } else {
100
            $class = $this->getSeederName($name); //legacy support
101
            if (class_exists($class)) {
102
                $seeders[] = $class;
103
            }
104
        }
105
106
        if (count($seeders) > 0) {
107
            array_walk($seeders, [$this, 'dbSeed']);
108
            $this->info("Module [$name] seeded.");
109
        }
110
    }
111
112
    /**
113
     * Seed the specified module.
114
     *
115
     * @param string $className
116
     */
117
    protected function dbSeed($className)
118
    {
119
        $params = [
120
            '--class' => $className,
121
        ];
122
123
        if ($option = $this->option('database')) {
124
            $params['--database'] = $option;
125
        }
126
127
        if ($option = $this->option('force')) {
128
            $params['--force'] = $option;
129
        }
130
131
        $this->call('db:seed', $params);
132
    }
133
134
    /**
135
     * Get master database seeder name for the specified module.
136
     *
137
     * @param string $name
138
     *
139
     * @return string
140
     */
141
    public function getSeederName($name)
142
    {
143
        $name = Str::studly($name);
144
145
        $namespace = $this->laravel['modules']->config('namespace');
146
147
        return $namespace . '\\' . $name . '\Database\Seeders\\' . $name . 'DatabaseSeeder';
148
    }
149
150
    /**
151
     * Get the console command arguments.
152
     *
153
     * @return array
154
     */
155 56
    protected function getArguments()
156
    {
157
        return array(
158 56
            array('module', InputArgument::OPTIONAL, 'The name of module will be used.'),
159
        );
160
    }
161
162
    /**
163
     * Get the console command options.
164
     *
165
     * @return array
166
     */
167 56
    protected function getOptions()
168
    {
169
        return array(
170 56
            array('database', null, InputOption::VALUE_OPTIONAL, 'The database connection to seed.'),
171
            array('force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'),
172
        );
173
    }
174
}
175