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

CreateModelCommand::actionIndex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 18
rs 9.9
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 CreateModelCommand
9
{
10
    /**
11
     * Creates a file with Seed data
12
     * -----------------------------
13
     * Создает файл с данными Seed
14
     */
15
    public function actionIndex()
16
    {
17
        Cli::printer("Enter table name: ", "magneta");
18
        $prefix    = str_replace("\n", "", Cli::reader());
19
        $className = ucfirst($prefix);
20
21
        Cli::printer("Enter container: ", "magneta");
22
        $container = ucfirst(str_replace("\n", "", Cli::reader()));
23
24
        if (!empty($container)) {
25
26
            $this->writeFile(
27
                [Rudra::config()->get('app.path') . "/app/Containers/$container/Models/", "{$className}.php"],
28
                $this->createClass($className, $container)
29
            );
30
31
        } else {
32
            $this->actionIndex();
33
        }
34
    }
35
36
    /**
37
     * @param string $className
38
     * @param string $container
39
     * @return string
40
     *
41
     * Creates class data
42
     * ------------------
43
     * Создает данные класса
44
     */
45
    private function createClass(string $className, string $container)
46
    {
47
        $table = strtolower($className);
48
49
        return <<<EOT
50
<?php
51
52
namespace App\Containers\\{$container}\Models;
53
54
use Rudra\Model\Model;
55
56
class {$className} extends Model
57
{
58
    public static string \$table = "$table";
59
}  
60
EOT;
61
    }
62
63
    /**
64
     * @param $path
65
     * @param $data
66
     *
67
     * Writes data to a file
68
     * ---------------------
69
     * Записывает данные в файл
70
     */
71
    private function writeFile(array $path, string $data)
72
    {
73
        if (!is_dir($path[0])) {
74
            mkdir($path[0], 0755, true);
75
        }
76
77
        $fullPath = $path[0] . $path[1];
78
79
        if (!file_exists($fullPath)) {
80
            Cli::printer("The file ", "light_green");
81
            Cli::printer($fullPath, "light_green");
82
            Cli::printer(" was created\n", "light_green");
83
            file_put_contents($fullPath, $data);
84
        } else {
85
            Cli::printer("The file $fullPath is already exists\n", "light_yellow");
86
        }
87
    }
88
}
89