Completed
Pull Request — master (#591)
by
unknown
04:48
created

SeedCommand::getArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Nwidart\Modules\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Contracts\Debug\ExceptionHandler;
7
use Illuminate\Support\Str;
8
use Nwidart\Modules\Contracts\RepositoryInterface;
9
use Nwidart\Modules\Module;
10
use Nwidart\Modules\Support\Config\GenerateConfigReader;
11
use Nwidart\Modules\Traits\ModuleCommandTrait;
12
use RuntimeException;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputOption;
15
16
class SeedCommand extends Command
17
{
18
    use ModuleCommandTrait;
19
20
    /**
21
     * The console command name.
22
     *
23
     * @var string
24
     */
25
    protected $name = 'module:seed';
26
27
    /**
28
     * The console command description.
29
     *
30
     * @var string
31
     */
32
    protected $description = 'Run database seeder from the specified module or from all modules.';
33
34
    /**
35
     * Execute the console command.
36
     * @throws FatalThrowableError
37
     */
38
    public function handle()
39
    {
40
        try {
41
            if ($name = $this->argument('module')) {
42
                $name = Str::studly($name);
43
                $this->moduleSeed($this->getModuleByName($name));
44
            } else {
45
                $modules = $this->getModuleRepository()->getOrdered();
46
                array_walk($modules, [$this, 'moduleSeed']);
47
                $this->info('All modules seeded.');
48
            }
49
        } catch (\Throwable $e) {
50
            $this->reportException($e);
51
52
            $this->renderException($this->getOutput(), $e);
53
54
            return 1;
55
        }
56
    }
57
58
    /**
59
     * @throws RuntimeException
60
     * @return RepositoryInterface
61
     */
62
    public function getModuleRepository(): RepositoryInterface
63
    {
64
        $modules = $this->laravel['modules'];
65
        if (!$modules instanceof RepositoryInterface) {
66
            throw new RuntimeException('Module repository not found!');
67
        }
68
69
        return $modules;
70
    }
71
72
    /**
73
     * @param $name
74
     *
75
     * @throws RuntimeException
76
     *
77
     * @return Module
78
     */
79
    public function getModuleByName($name)
80
    {
81
        $modules = $this->getModuleRepository();
82
        if ($modules->has($name) === false) {
83
            throw new RuntimeException("Module [$name] does not exists.");
84
        }
85
86
        return $modules->find($name);
87
    }
88
89
    /**
90
     * @param Module $module
91
     *
92
     * @return void
93
     */
94
    public function moduleSeed(Module $module)
95
    {
96
        $seeders = [];
97
        $name = $module->getName();
0 ignored issues
show
Bug introduced by
Consider using $module->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
98
        $config = $module->get('migration');
99
        if (is_array($config) && array_key_exists('seeds', $config)) {
100
            foreach ((array)$config['seeds'] as $class) {
101
                if (class_exists($class)) {
102
                    $seeders[] = $class;
103
                }
104
            }
105
        } else {
106
            $class = $this->getSeederName($name); //legacy support
107
            if (class_exists($class)) {
108
                $seeders[] = $class;
109
            }
110
        }
111
112
        if (count($seeders) > 0) {
113
            array_walk($seeders, [$this, 'dbSeed']);
114
            $this->info("Module [$name] seeded.");
115
        }
116
    }
117
118
    /**
119
     * Seed the specified module.
120
     *
121
     * @param string $className
122
     */
123
    protected function dbSeed($className)
124
    {
125
        if ($option = $this->option('class')) {
126
            $params['--class'] = Str::finish(substr($className, 0, strrpos($className, '\\')), '\\') . $option;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
127
        } else {
128
            $params = ['--class' => $className];
129
        }
130
131
        if ($option = $this->option('database')) {
132
            $params['--database'] = $option;
133
        }
134
135
        if ($option = $this->option('force')) {
136
            $params['--force'] = $option;
137
        }
138
139
        $this->call('db:seed', $params);
140
    }
141
142
    /**
143
     * Get master database seeder name for the specified module.
144
     *
145
     * @param string $name
146
     *
147
     * @return string
148
     */
149
    public function getSeederName($name)
150
    {
151
        $name = Str::studly($name);
152
153
        $namespace = $this->laravel['modules']->config('namespace');
154
        $seederPath = GenerateConfigReader::read('seeder');
155
        $seederPath = str_replace('/', '\\', $seederPath->getPath());
156
157
        return $namespace . '\\' . $name . '\\' . $seederPath . '\\' . $name . 'DatabaseSeeder';
158
    }
159
160
    /**
161
     * Report the exception to the exception handler.
162
     *
163
     * @param  \Symfony\Component\Console\Output\OutputInterface  $output
164
     * @param  \Throwable  $e
165
     * @return void
166
     */
167
    protected function renderException($output, \Throwable $e)
168
    {
169
        $this->laravel[ExceptionHandler::class]->renderForConsole($output, $e);
170
    }
171
172
    /**
173
     * Report the exception to the exception handler.
174
     *
175
     * @param  \Throwable  $e
176
     * @return void
177
     */
178
    protected function reportException(\Throwable $e)
179
    {
180
        $this->laravel[ExceptionHandler::class]->report($e);
181
    }
182
183
    /**
184
     * Get the console command arguments.
185
     *
186
     * @return array
187
     */
188 94
    protected function getArguments()
189
    {
190
        return [
191 94
            ['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
192
        ];
193
    }
194
195
    /**
196
     * Get the console command options.
197
     *
198
     * @return array
199
     */
200 94 View Code Duplication
    protected function getOptions()
201
    {
202
        return [
203 94
            ['class', null, InputOption::VALUE_OPTIONAL, 'The class name of the root seeder.'],
204
            ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to seed.'],
205
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production.'],
206
        ];
207
    }
208
}
209