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

CreateObserverCommand   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createClass() 0 10 1
A actionIndex() 0 18 2
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 CreateObserverCommand 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 observer name: ", "magneta");
19
        $prefix    = str_replace(PHP_EOL, "", Cli::reader());
20
        $className = ucfirst($prefix) . 'Observer';
21
22
        Cli::printer("Enter container: ", "magneta");
23
        $container = ucfirst(str_replace(PHP_EOL, "", Cli::reader()));
24
25
        if (!empty($container)) {
26
27
            $this->writeFile(
28
                [str_replace('/', DIRECTORY_SEPARATOR, Rudra::config()->get('app.path') . "/app/Containers/$container/Observer/"), "{$className}.php"],
29
                $this->createClass($className, $container)
30
            );
31
32
        } else {
33
            $this->actionIndex();
34
        }
35
    }
36
37
    /**
38
     * Creates class data
39
     * ------------------
40
     * Создает данные класса
41
     *
42
     * @param string $className
43
     * @param string $container
44
     * @return string
45
     */
46
    private function createClass(string $className, string $container): string
47
    {
48
        return <<<EOT
49
<?php
50
51
namespace App\Containers\\{$container}\Observer;
52
53
use Rudra\EventDispatcher\ObserverInterface;
54
55
class {$className} implements ObserverInterface
56
{
57
    public function onEvent()
58
    {
59
60
    }
61
}\r\n
62
EOT;
63
    }
64
}
65