Passed
Push — master ( 4c3e72...46eb59 )
by Arthur
10:39
created

SeedCommand::handle()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.025

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 2
nop 0
dl 0
loc 17
ccs 9
cts 10
cp 0.9
crap 5.025
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace Foundation\Console;
4
5
use Foundation\Services\BootstrapRegistrarService;
6
use Illuminate\Database\ConnectionResolverInterface as Resolver;
7
use Illuminate\Database\Eloquent\Model;
8
9
class SeedCommand extends \Illuminate\Database\Console\Seeds\SeedCommand
10
{
11
    /**
12
     * The service that registers all module entities.
13
     *
14
     * @var BootstrapRegistrarService
15
     */
16
    protected $service;
17
18 46
    public function __construct(Resolver $resolver, BootstrapRegistrarService $service)
19
    {
20 46
        parent::__construct($resolver);
21 46
        $this->service = $service;
22 46
    }
23
24
    /**
25
     * Execute the console command.
26
     *
27
     * @return void
28
     */
29 46
    public function handle()
30
    {
31 46
        if (!$this->confirmToProceed()) {
32
            return;
33
        }
34
35 46
        $this->resolver->setDefaultConnection($this->getDatabase());
36
37
        Model::unguarded(function () {
38 46
            foreach ($this->getSeeders() as $seeder) {
39 46
                $seeder = $this->laravel->make($seeder);
40 46
                if (!isset($seeder->enabled) || $seeder->enabled)
41 46
                    $seeder->__invoke();
42
            }
43 46
        });
44
45 46
        $this->info('Database seeding completed successfully.');
46 46
    }
47
48 46
    protected function getSeeders(): array
49
    {
50 46
        $this->service = $this->laravel->make(BootstrapRegistrarService::class);
51
52 46
        return $this->service->getSeeders() ?? [];
53
    }
54
}
55