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

CreateControllerCommand::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 CreateControllerCommand
9
{
10
    /**
11
     * Creates a file with Seed data
12
     * -----------------------------
13
     * Создает файл с данными Seed
14
     */
15
    public function actionIndex()
16
    {
17
        Cli::printer("Enter controller name: ", "magneta");
18
        $controllerPrefix = str_replace("\n", "", Cli::reader());
19
        $className        = ucfirst($controllerPrefix) . 'Controller';
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/Controllers/", "{$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
        return <<<EOT
48
<?php
49
50
namespace App\Containers\\{$container}\Controllers;
51
52
use App\Containers\\{$container}\\{$container}Controller;
53
54
class {$className} extends {$container}Controller
55
{
56
    /**
57
     * @Routing(url = 'your/url', method = 'GET')
58
     */
59
    public function actionIndex()
60
    {
61
62
    }
63
}
64
EOT;
65
    }
66
67
    /**
68
     * @param $path
69
     * @param $data
70
     *
71
     * Writes data to a file
72
     * ---------------------
73
     * Записывает данные в файл
74
     */
75
    private function writeFile(array $path, string $data)
76
    {
77
        if (!is_dir($path[0])) {
78
            mkdir($path[0], 0755, true);
79
        }
80
81
        $fullPath = $path[0] . $path[1];
82
83
        if (!file_exists($fullPath)) {
84
            Cli::printer("The file ", "light_green");
85
            Cli::printer($fullPath, "light_green");
86
            Cli::printer(" was created\n", "light_green");
87
            file_put_contents($fullPath, $data);
88
        } else {
89
            Cli::printer("The file $fullPath is already exists\n", "light_yellow");
90
        }
91
    }
92
}
93