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

CreateContainerCommand::createRoutes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 3
rs 10
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 CreateContainerCommand 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 container name: ", "magneta");
19
        $container = ucfirst(str_replace(PHP_EOL, "", Cli::reader()));
20
        $className = $container . 'Controller';
21
22
        if (!empty($container)) {
23
24
            if (is_dir(str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/app/Containers/$container/"))) {
25
                Cli::printer("The container $container already exists" . PHP_EOL, "light_yellow");
26
                return;
27
            }
28
29
            $this->writeFile(
30
                [str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/app/Containers/$container/"), "{$className}.php"],
31
                $this->createContainersController($className, $container)
32
            );
33
34
            $this->writeFile(
35
                [str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/app/Containers/$container/"), "routes.php"],
36
                $this->createRoutes()
37
            );
38
39
            $this->addConfig($container);
40
            $this->createDirectories(str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/app/Containers/$container/"));
41
            Cli::printer("The container $container was created" . PHP_EOL, "light_green");
42
43
        } else {
44
            $this->actionIndex();
45
        }
46
    }
47
48
    /**
49
     * Creates class data
50
     * ------------------
51
     * Создает данные класса
52
     *
53
     * @param string $className
54
     * @param string $container
55
     * @return string
56
     */
57
    private function createContainersController(string $className, string $container): string
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

57
    private function createContainersController(/** @scrutinizer ignore-unused */ string $className, string $container): string

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...
58
    {
59
        return <<<EOT
60
<?php
61
62
namespace App\Containers\\{$container};
63
64
use App\Ship\ShipController;
65
use Rudra\UI\ViewFacade as UI;
66
use Rudra\Controller\ContainerControllerInterface;
67
68
class {$container}Controller extends ShipController implements ContainerControllerInterface
69
{
70
    public function containerInit(): void
71
    {
72
        UI::setup(dirname(__DIR__) . '/', "$container/UI/tmpl", "$container/UI/cache");
73
74
        data([
75
            "title" => __CLASS__,
76
        ]);
77
    }
78
}\r\n
79
EOT;
80
    }
81
82
    /**
83
     * Creates routes
84
     * ------------------
85
     * Создает файл маршрутизатора
86
     */
87
    private function createRoutes(): string
88
    {
89
        return <<<EOT
90
<?php
91
92
return [
93
];\r\n
94
EOT;
95
    }
96
97
    /**
98
     * Create UI directories
99
     * ---------------------
100
     * Создает каталоги для UI
101
     *
102
     * @param string $path
103
     * @return void
104
     */
105
    private function createDirectories(string $path): void
106
    {
107
        if (!is_dir($path . 'UI')) {
108
            mkdir($path . 'UI', 0755, true);
109
        }
110
111
        if (!is_dir($path . 'UI' . DIRECTORY_SEPARATOR . 'cache')) {
112
            mkdir($path . 'UI' . DIRECTORY_SEPARATOR . 'cache', 0755, true);
113
        }
114
115
        if (!is_dir($path . 'UI' . DIRECTORY_SEPARATOR . 'tmpl')) {
116
            mkdir($path . 'UI' . DIRECTORY_SEPARATOR . 'tmpl', 0755, true);
117
        }
118
    }
119
120
    public function addConfig(string $container): void
121
    {
122
        $path      = str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/config/setting.local.yml");
123
        $namespace = strtolower($container) . ": App\Containers\\{$container}\\";
124
        $contents  = <<<EOT
125
        \r\n    $namespace
126
EOT;
127
        file_put_contents($path, $contents, FILE_APPEND | LOCK_EX);
128
    }
129
}
130