Completed
Pull Request — master (#46)
by John
04:01
created

SeedCommand::getModuleRepository()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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