Passed
Pull Request — master (#58)
by Dmitriy
02:37
created

ControllerGenerator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 82.93%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
eloc 43
c 2
b 0
f 0
dl 0
loc 99
ccs 34
cts 41
cp 0.8293
rs 10

8 Methods

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