Passed
Pull Request — master (#58)
by Dmitriy
13:13 queued 10:22
created

ControllerGenerator::doGenerate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3.0026

Importance

Changes 0
Metric Value
cc 3
eloc 14
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 23
ccs 14
cts 15
cp 0.9333
crap 3.0026
rs 9.7998
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\CodeFile;
9
use Yiisoft\Yii\Gii\Generator\AbstractGenerator;
10
use Yiisoft\Yii\Gii\Generator\AbstractGeneratorCommand;
11
12
/**
13
 * This generator will generate a controller and one or a few action view files.
14
 */
15
final class ControllerGenerator extends AbstractGenerator
16
{
17
    public static function getName(): string
18
    {
19
        return 'Controller';
20
    }
21
22
    public static function getDescription(): string
23
    {
24
        return 'This generator helps you to quickly generate a new controller class with
25
            one or several controller actions and their corresponding views.';
26
    }
27
28 1
    public static function getId(): string
29
    {
30 1
        return 'controller';
31
    }
32
33
    public function requiredTemplates(): array
34
    {
35
        return [
36
            'controller.php',
37
            'view.php',
38
        ];
39
    }
40
41 1
    public function doGenerate(AbstractGeneratorCommand $command): array
42
    {
43 1
        if (!$command instanceof ControllerCommand) {
44
            throw new InvalidArgumentException();
45
        }
46
47 1
        $files = [];
48
49 1
        $rootPath = $this->aliases->get('@root');
50
51 1
        $files[] = (new CodeFile(
52 1
            $this->getControllerFile($command),
53 1
            $this->render($command, 'controller')
54 1
        ))->withBasePath($rootPath);
55
56 1
        foreach ($command->getActions() as $action) {
57 1
            $files[] = (new CodeFile(
58 1
                $this->getViewFile($command, $action),
59 1
                $this->render($command, 'view', ['action' => $action])
60 1
            ))->withBasePath($rootPath);
61
        }
62
63 1
        return $files;
64
    }
65
66
    /**
67
     * @return string the controller class file path
68
     */
69 1
    private function getControllerFile(ControllerCommand $command): string
70
    {
71 1
        return $this->aliases->get(
72 1
            sprintf('%s/%s.php', $this->getDirectory() ?? '', $command->getControllerClass())
73
        );
74
    }
75
76
    /**
77
     * @param string $action the action ID
78
     *
79
     * @return string the action view file path
80
     */
81 1
    public function getViewFile(ControllerCommand $command, string $action): string
82
    {
83 1
        if (empty($command->getViewsPath())) {
84 1
            return $this->aliases->get(
85 1
                "@views/{$command->getControllerID()}/$action.php"
86
            );
87
        }
88
89
        return $this->aliases->get(str_replace('\\', '/', $command->getViewsPath()) . "/$action.php");
90
    }
91
92
    public static function getCommandClass(): string
93
    {
94
        return ControllerCommand::class;
95
    }
96
}
97