CreateSeedCommand   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 36
dl 0
loc 93
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createClass() 0 46 2
A actionIndex() 0 31 2
1
<?php
2
3
namespace App\Ship\Command;
4
5
use App\Ship\Utils\FileCreator;
6
use Rudra\Container\Facades\Rudra;
7
use Rudra\Cli\ConsoleFacade as Cli;
8
9
class CreateSeedCommand extends FileCreator
10
{
11
    /**
12
     * Creates a file with Seed data
13
     * -----------------------------
14
     * Создает файл с данными Seed
15
     */
16
    public function actionIndex(): void
17
    {
18
        Cli::printer("Enter table name: ", "magneta");
19
        $table     = str_replace(PHP_EOL, "", Cli::reader());
20
        $date      = date("_dmYHis");
21
        $className = ucfirst($table . $date);
22
23
        Cli::printer("Enter container (empty for Ship): ", "magneta");
24
        $container = ucfirst(str_replace(PHP_EOL, "", Cli::reader()));
25
26
        Cli::printer("multiline Seed (yes): ", "magneta");
27
        $multiline = ucfirst(str_replace(PHP_EOL, "", Cli::reader()));
28
        $multiline = empty($multiline);
29
30
        if (!empty($container)) {
31
32
            $namespace = 'App\Containers\\' . $container . '\Seed';
33
34
            $this->writeFile(
35
                [str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/app/Containers/$container/Seed/"), "{$className}_seed.php"],
36
                $this->createClass($className, $table, $namespace, $multiline)
37
            );
38
39
40
        } else {
41
42
            $namespace = "App\Ship\Seed";
43
44
            $this->writeFile(
45
                [str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/app/Ship/Seed/"), "{$className}_seed.php"],
46
                $this->createClass($className, $table, $namespace, $multiline)
47
            );
48
        }
49
    }
50
51
    /**
52
     * Creates class data
53
     * ------------------
54
     * Создает данные класса
55
     */
56
    private function createClass(string $className, string $table, string $namespace, bool $multiline = false): string
57
    {
58
        if ($multiline) {
59
            return <<<EOT
60
<?php
61
62
namespace {$namespace};
63
64
use App\Ship\Seed\AbstractSeed;
65
66
class {$className}_seed extends AbstractSeed
67
{
68
    public function create(): void
69
    {
70
        \$table = "$table";
71
72
        \$fieldsArray = [
73
            [
74
                "created_at" => date('Y-m-d H:i:s'),
75
                "updated_at" => date('Y-m-d H:i:s'),
76
            ],
77
            [
78
                "created_at" => date('Y-m-d H:i:s'),
79
                "updated_at" => date('Y-m-d H:i:s'),
80
            ],
81
        ];
82
83
        foreach (\$fieldsArray as \$fields) {
84
            \$this->execute(\$table, \$fields);
85
        }
86
    }
87
}\r\n
88
EOT;
89
        } else {
90
            return <<<EOT
91
<?php
92
93
namespace {$namespace};
94
95
use App\Ship\Seed\AbstractSeed;
96
97
class {$className}_seed extends AbstractSeed
98
{
99
    public function create(): void
100
    {
101
        \$table = "$table";
102
        \$fields = [
103
            "created_at" => date('Y-m-d H:i:s'),
104
            "updated_at" => date('Y-m-d H:i:s'),
105
        ];
106
107
        \$this->execute(\$table, \$fields);
108
    }
109
}\r\n
110
EOT;
111
        }
112
    }
113
}
114