Passed
Pull Request — master (#75)
by Korotkov
12:47
created

CreateModelCommand::createEntity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 2
dl 0
loc 17
rs 9.9666
c 0
b 0
f 0
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 CreateModelCommand 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
        $prefix    = str_replace(PHP_EOL, "", Cli::reader());
20
        $className = ucfirst($prefix);
21
22
        Cli::printer("Enter container: ", "magneta");
23
        $container = ucfirst(str_replace(PHP_EOL, "", Cli::reader()));
24
25
        if (!empty($container)) {
26
27
            $this->writeFile(
28
                [str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/app/Containers/$container/Entity/"), "{$className}.php"],
29
                $this->createEntity($className, $container)
30
            );
31
32
            $this->writeFile(
33
                [str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/app/Containers/$container/Model/"), "{$className}.php"],
34
                $this->createModel($className, $container)
35
            );
36
37
            $this->writeFile(
38
                [str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/app/Containers/$container/Repository/"), "{$className}Repository.php"],
39
                $this->createRepository($className, $container)
40
            );
41
42
        } else {
43
            $this->actionIndex();
44
        }
45
    }
46
47
    /**
48
     * Creates class data
49
     * ------------------
50
     * Создает данные класса
51
     *
52
     * @param string $className
53
     * @param string $container
54
     * @return string
55
     */
56
    private function createEntity(string $className, string $container): string
57
    {
58
        $table = strtolower($className);
59
60
        return <<<EOT
61
<?php
62
63
namespace App\Containers\\{$container}\Entity;
64
65
use Rudra\Model\Entity;
66
67
/**
68
 * @see \App\Containers\\$container\Repository\\{$className}Repository
69
 */
70
class {$className} extends Entity
71
{
72
    public static string \$table = "$table";
73
    public static string \$directory = __DIR__;
74
}\r\n
75
EOT;
76
    }
77
78
    /**
79
     * Creates class data
80
     * ------------------
81
     * Создает данные класса
82
     *
83
     * @param string $className
84
     * @param string $container
85
     * @return string
86
     */
87
    private function createModel(string $className, string $container): string
88
    {
89
        $table = strtolower($className);
0 ignored issues
show
Unused Code introduced by
The assignment to $table is dead and can be removed.
Loading history...
90
91
        return <<<EOT
92
<?php
93
94
namespace App\Containers\\{$container}\Model;
95
96
use Rudra\Model\Model;
97
98
class {$className} extends Model
99
{
100
101
}\r\n
102
EOT;
103
    }
104
105
    /**
106
     * Creates class data
107
     * ------------------
108
     * Создает данные класса
109
     *
110
     * @param string $className
111
     * @param string $container
112
     * @return string
113
     */
114
    private function createRepository(string $className, string $container): string
115
    {
116
        $table = strtolower($className);
0 ignored issues
show
Unused Code introduced by
The assignment to $table is dead and can be removed.
Loading history...
117
118
        return <<<EOT
119
<?php
120
121
namespace App\Containers\\{$container}\Repository;
122
123
use Rudra\Model\QBFacade;
124
use Rudra\Model\Repository;
125
126
class {$className}Repository extends Repository
127
{
128
129
}\r\n
130
EOT;
131
    }
132
}
133