Test Failed
Pull Request — master (#110)
by Dmitriy
05:39 queued 02:42
created

Generator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
eloc 43
c 2
b 0
f 0
dl 0
loc 99
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A doGenerate() 0 25 3
A getViewFile() 0 13 2
A getCommandClass() 0 3 1
A getName() 0 3 1
A getControllerFile() 0 12 2
A getId() 0 3 1
A getDescription() 0 3 1
A getRequiredTemplates() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Gii\Generator\Controller;
6
7
use InvalidArgumentException;
8
use Yiisoft\Yii\Gii\Component\CodeFile\CodeFile;
9
use Yiisoft\Yii\Gii\Generator\AbstractGenerator;
10
use Yiisoft\Yii\Gii\GeneratorCommandInterface;
11
12
/**
13
 * This generator will generate a controller and one or a few action view files.
14
 */
15
final class Generator extends AbstractGenerator
16
{
17
    public static function getId(): string
18
    {
19
        return 'controller';
20
    }
21
22
    public static function getName(): string
23
    {
24
        return 'Controller';
25
    }
26
27
    public static function getDescription(): string
28
    {
29
        return 'This generator helps you to quickly generate a new controller class with
30
            one or several controller actions and their corresponding views.';
31
    }
32
33
    public function getRequiredTemplates(): array
34
    {
35
        return [
36
            'controller.php',
37
            'view.php',
38
        ];
39
    }
40
41
    public function doGenerate(GeneratorCommandInterface $command): array
42
    {
43
        if (!$command instanceof Command) {
44
            throw new InvalidArgumentException();
45
        }
46
47
        $files = [];
48
49
        $rootPath = $this->aliases->get('@root');
50
51
        $codeFile = (new CodeFile(
52
            $this->getControllerFile($command),
53
            $this->render($command, 'controller.php')
54
        ))->withBasePath($rootPath);
55
        $files[$codeFile->getId()] = $codeFile;
56
57
        foreach ($command->getActions() as $action) {
58
            $codeFile = (new CodeFile(
59
                $this->getViewFile($command, $action),
60
                $this->render($command, 'view.php', ['action' => $action])
61
            ))->withBasePath($rootPath);
62
            $files[$codeFile->getId()] = $codeFile;
63
        }
64
65
        return $files;
66
    }
67
68
    /**
69
     * @return string the controller class file path
70
     */
71
    private function getControllerFile(Command $command): string
72
    {
73
        $directory = empty($command->getDirectory()) ? '@src/Controller/' : $command->getDirectory();
74
75
        return $this->aliases->get(
76
            str_replace(
77
                ['\\', '//'],
78
                '/',
79
                sprintf(
80
                    '%s/%s.php',
81
                    $directory,
82
                    $command->getControllerClass(),
83
                ),
84
            ),
85
        );
86
    }
87
88
    /**
89
     * @param string $action the action ID
90
     *
91
     * @return string the action view file path
92
     */
93
    public function getViewFile(Command $command, string $action): string
94
    {
95
        $directory = empty($command->getViewsPath()) ? '@views/' : $command->getViewsPath();
96
97
        return $this->aliases->get(
98
            str_replace(
99
                ['\\', '//'],
100
                '/',
101
                sprintf(
102
                    '%s/%s/%s.php',
103
                    $directory,
104
                    $command->getControllerID(),
105
                    $action,
106
                ),
107
            ),
108
        );
109
    }
110
111
    public static function getCommandClass(): string
112
    {
113
        return Command::class;
114
    }
115
}
116