Passed
Push — master ( 3ed7ec...a3f41f )
by Korotkov
02:14 queued 11s
created

SeedCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 28
c 0
b 0
f 0
dl 0
loc 42
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
B actionIndex() 0 40 6
1
<?php
2
3
namespace App\Ship\Commands;
4
5
use Rudra\Cli\ConsoleFacade as Cli;
6
use Rudra\Container\Facades\Rudra;
7
8
class SeedCommand
9
{
10
    public function actionIndex()
11
    {
12
13
        Cli::printer("Enter container (empty for Ship): ", "magneta");
14
        $container = ucfirst(str_replace("\n", "", Cli::reader()));
15
16
        if (!empty($container)) {
17
            $fileList  = array_slice(scandir(Rudra::config()->get('app.path') . "/app/Containers/" . $container . "/Seeds/"), 2);
18
            $namespace = "App\\Containers\\$container\\Seeds\\";
19
        } else {
20
            $fileList  = array_slice(scandir(Rudra::config()->get('app.path') . "/app/Ship/Seeds/"), 2);
21
            $namespace = "App\\Ship\\Seeds\\";
22
        }
23
 
24
        $historyPath = Rudra::config()->get('app.path') . "/app/Ship/Data/SeedsHistory.php";
25
        $history     = require_once $historyPath;
26
27
        foreach ($fileList as $filename) {
28
29
            $seedName = $namespace . strstr($filename, '.', true);
30
31
            if ($seedName === 'App\Ship\Seeds\AbstractSeed') {
32
                continue;
33
            }
34
35
            if (in_array($seedName, $history)) {
36
                Cli::printer("The $seedName is already seeded\n", "yellow");
37
            } else {
38
                (new $seedName)->create();
39
                Cli::printer("The $seedName was seed\n", "light_cyan");
40
41
                if (file_exists($historyPath)) {
42
                    $contents = file_get_contents($historyPath);
43
                    $contents = str_replace("];", '', $contents);
44
                    file_put_contents($historyPath, $contents);
45
                    $contents = <<<EOT
46
    "$seedName",
47
];
48
EOT;
49
                    file_put_contents($historyPath, $contents, FILE_APPEND | LOCK_EX);
50
                }
51
            }
52
        }
53
    }
54
}
55