Passed
Push — master ( 53498b...8ea87e )
by Korotkov
01:42 queued 12s
created

CreateControllerCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 42
c 2
b 0
f 1
dl 0
loc 103
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createClass() 0 15 1
A addRoute() 0 15 2
A writeFile() 0 15 3
A actionIndex() 0 19 2
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 = ucfirst(str_replace(PHP_EOL, "", Cli::reader()));
19
20
        Cli::printer("Enter container: ", "magneta");
21
        $container = ucfirst(str_replace(PHP_EOL, "", Cli::reader()));
22
23
        if (!empty($container)) {
24
25
            $this->writeFile(
26
                [str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/app/Containers/$container/Controllers/"), "{$controllerPrefix}Controller.php"],
27
                $this->createClass($controllerPrefix, $container)
28
            );
29
30
            $this->addRoute($container, $controllerPrefix);
31
32
        } else {
33
            $this->actionIndex();
34
        }
35
    }
36
37
    /**
38
     * @param string $controllerPrefix
39
     * @param string $container
40
     * @return string
41
     *
42
     * Creates class data
43
     * ------------------
44
     * Создает данные класса
45
     */
46
    private function createClass(string $controllerPrefix, string $container)
47
    {
48
        $url = strtolower("$container/$controllerPrefix");
49
50
        return <<<EOT
51
<?php
52
53
namespace App\Containers\\{$container}\Controllers;
54
55
use App\Containers\\{$container}\\{$container}Controller;
56
57
class {$controllerPrefix}Controller extends {$container}Controller
58
{
59
    /**
60
     * @Routing(url = '{$url}', method = 'GET')
61
     */
62
    public function actionIndex()
63
    {
64
        dd(__CLASS__);
65
    }
66
}
67
EOT;
68
    }
69
70
    /**
71
     * @param $path
72
     * @param $data
73
     *
74
     * Writes data to a file
75
     * ---------------------
76
     * Записывает данные в файл
77
     */
78
    private function writeFile(array $path, string $data)
79
    {
80
        if (!is_dir($path[0])) {
81
            mkdir($path[0], 0755, true);
82
        }
83
84
        $fullPath = $path[0] . $path[1];
85
86
        if (!file_exists($fullPath)) {
87
            Cli::printer("The file ", "light_green");
88
            Cli::printer($fullPath, "light_green");
89
            Cli::printer(" was created" . PHP_EOL, "light_green");
90
            file_put_contents($fullPath, $data);
91
        } else {
92
            Cli::printer("The file $fullPath is already exists" . PHP_EOL, "light_yellow");
93
        }
94
    }
95
96
    public function addRoute(string $container, string $controllerPrefix)
97
    {
98
        $path   = str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/app/Containers/$container/routes.php");
99
        $routes = require_once $path;
100
        $namespace = "\App\Containers\\{$container}\\Controllers\\{$controllerPrefix}Controller";
101
102
        if (!in_array($namespace, $routes)) {
103
            $contents = file_get_contents($path);
104
            $contents = str_replace("];", '', $contents);
105
            file_put_contents($path, $contents);
106
            $contents = <<<EOT
107
    $namespace::class,
108
];
109
EOT;
110
            file_put_contents($path, $contents, FILE_APPEND | LOCK_EX);
111
        }
112
    }
113
}
114