SeedCommand::componentSeed()   C
last analyzed

Complexity

Conditions 7
Paths 8

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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