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

CreateRepositoryCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 76
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A actionIndex() 0 18 2
A createClass() 0 10 1
A writeFile() 0 15 3
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 CreateRepositoryCommand
9
{
10
    /**
11
     * Creates a file with Seed data
12
     * -----------------------------
13
     * Создает файл с данными Seed
14
     */
15
    public function actionIndex()
16
    {
17
        Cli::printer("Enter repository name: ", "magneta");
18
        $prefix    = str_replace("\n", "", Cli::reader());
19
        $className = ucfirst($prefix) . 'Repository';
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/Repository/", "{$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}\Repository;
51
52
use Rudra\Model\QBFacade;
53
54
class {$className}
55
{
56
57
}
58
EOT;
59
    }
60
61
    /**
62
     * @param $path
63
     * @param $data
64
     *
65
     * Writes data to a file
66
     * ---------------------
67
     * Записывает данные в файл
68
     */
69
    private function writeFile(array $path, string $data)
70
    {
71
        if (!is_dir($path[0])) {
72
            mkdir($path[0], 0755, true);
73
        }
74
75
        $fullPath = $path[0] . $path[1];
76
77
        if (!file_exists($fullPath)) {
78
            Cli::printer("The file ", "light_green");
79
            Cli::printer($fullPath, "light_green");
80
            Cli::printer(" was created\n", "light_green");
81
            file_put_contents($fullPath, $data);
82
        } else {
83
            Cli::printer("The file $fullPath is already exists\n", "light_yellow");
84
        }
85
    }
86
}
87