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

CreateContainerCommand::createRoutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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 CreateContainerCommand
9
{
10
    /**
11
     * Creates a file with Seed data
12
     * -----------------------------
13
     * Создает файл с данными Seed
14
     */
15
    public function actionIndex()
16
    {
17
        Cli::printer("Enter container name: ", "magneta");
18
        $container = ucfirst(str_replace("\n", "", Cli::reader()));
19
        $className = $container . 'Controller';
20
21
        if (!empty($container)) {
22
23
            if (is_dir(Rudra::config()->get('app.path') . "/app/Containers/$container/")) {
24
                Cli::printer("The container $container already exists\n", "light_yellow");
25
                return;
26
            }
27
28
            $this->writeFile(
29
                [Rudra::config()->get('app.path') . "/app/Containers/$container/", "{$className}.php"],
30
                $this->createContainersConroller($className, $container)
31
            );
32
33
            $this->writeFile(
34
                [Rudra::config()->get('app.path') . "/app/Containers/$container/", "routes.php"],
35
                $this->createRoutes()
36
            );
37
38
            $this->createDirectories(Rudra::config()->get('app.path') . "/app/Containers/$container/");
39
            Cli::printer("The container $container was created\n", "light_green");
40
41
        } else {
42
            $this->actionIndex();
43
        }
44
    }
45
46
    /**
47
     * @param string $className
48
     * @param string $container
49
     *
50
     * Creates class data
51
     * ------------------
52
     * Создает данные класса
53
     */
54
    private function createContainersConroller(string $className, string $container)
0 ignored issues
show
Unused Code introduced by
The parameter $className is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

54
    private function createContainersConroller(/** @scrutinizer ignore-unused */ string $className, string $container)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
    {
56
        return <<<EOT
57
<?php
58
59
namespace App\Containers\\{$container};
60
61
use App\Ship\ShipController;
62
use Rudra\View\ViewFacade as View;
63
64
class {$container}Controller extends ShipController
65
{
66
    public function init()
67
    {
68
        View::setup([
69
            "base.path"      => dirname(__DIR__) . '/',
70
            "engine"         => "native",
71
            "view.path"      => "{$container}/UI/tmpl",
72
            "cache.path"     => "{$container}/UI/cache",
73
            "file.extension" => "phtml",
74
        ]);
75
76
        data([
77
            "title" => __CLASS__,
78
        ]);
79
    }
80
}
81
EOT;
82
    }
83
84
    /**
85
     * Creates routes
86
     * ------------------
87
     * Создает файл маршрутизатора
88
     */
89
    private function createRoutes()
90
    {
91
        return <<<EOT
92
<?php
93
94
return [
95
96
];
97
EOT;
98
    }
99
100
    /**
101
     * @param $path
102
     * @param $callable
103
     *
104
     * Writes data to a file
105
     * ---------------------
106
     * Записывает данные в файл
107
     */
108
    private function writeFile(array $path, string $data)
109
    {
110
        if (!is_dir($path[0])) {
111
            mkdir($path[0], 0755, true);
112
        }
113
114
        $fullPath = $path[0] . $path[1];
115
116
        if (!file_exists($fullPath)) {
117
            Cli::printer("The file ", "light_green");
118
            Cli::printer($fullPath, "light_green");
119
            Cli::printer(" was created\n", "light_green");
120
            file_put_contents($fullPath, $data);
121
        } else {
122
            Cli::printer("The file $fullPath is already exists\n", "light_yellow");
123
        }
124
    }
125
126
    /**
127
     * @param $path
128
     * @param $callable
129
     *
130
     * Create UI directories
131
     * ---------------------
132
     * Создает каталоги для UI
133
     */
134
    private function createDirectories(string $path)
135
    {
136
        if (!is_dir($path . 'UI')) {
137
            mkdir($path . 'UI', 0755, true);
138
        }
139
140
        if (!is_dir($path . 'UI/cache')) {
141
            mkdir($path . 'UI/cache', 0755, true);
142
        }
143
144
        if (!is_dir($path . 'UI/tmpl')) {
145
            mkdir($path . 'UI/tmpl', 0755, true);
146
        }
147
    }
148
}
149