Passed
Pull Request — master (#110)
by Dmitriy
03:29
created

Generator::doGenerate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2.0023

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 11
c 1
b 1
f 0
dl 0
loc 18
ccs 11
cts 12
cp 0.9167
rs 9.9
cc 2
nc 2
nop 1
crap 2.0023
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Gii\Generator\ActiveRecord;
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 8
    public static function getId(): string
18
    {
19 8
        return 'active-record';
20
    }
21
22
    public static function getName(): string
23
    {
24
        return 'Active Record';
25
    }
26
27
    public static function getDescription(): string
28
    {
29
        return '';
30
    }
31
32 1
    public function getRequiredTemplates(): array
33
    {
34 1
        return [
35 1
            'model.php',
36 1
        ];
37
    }
38
39 2
    public function doGenerate(GeneratorCommandInterface $command): array
40
    {
41 2
        if (!$command instanceof Command) {
42
            throw new InvalidArgumentException();
43
        }
44
45 2
        $files = [];
46
47 2
        $rootPath = $this->aliases->get('@root');
48
49 2
        $path = $this->getControllerFile($command);
50 2
        $codeFile = (new CodeFile(
51 2
            $path,
52 2
            $this->render($command, 'model.php')
53 2
        ))->withBasePath($rootPath);
54 2
        $files[$codeFile->getId()] = $codeFile;
55
56 2
        return $files;
57
    }
58
59
    /**
60
     * @return string the controller class file path
61
     */
62 2
    private function getControllerFile(Command $command): string
63
    {
64 2
        $directory = '@src/Model/';
65
66 2
        return $this->aliases->get(
67 2
            str_replace(
68 2
                ['\\', '//'],
69 2
                '/',
70 2
                sprintf(
71 2
                    '%s/%s.php',
72 2
                    $directory,
73 2
                    $command->getModelName(),
74 2
                ),
75 2
            ),
76 2
        );
77
    }
78
79 2
    public static function getCommandClass(): string
80
    {
81 2
        return Command::class;
82
    }
83
}
84