TemplateRuleHandler::getGenerator()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 6.105

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 6
c 2
b 0
f 0
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
rs 9.2222
cc 6
nc 4
nop 1
crap 6.105
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Gii\Validator;
6
7
use RuntimeException;
8
use Yiisoft\Aliases\Aliases;
9
use Yiisoft\Validator\Exception\UnexpectedRuleException;
10
use Yiisoft\Validator\Result;
11
use Yiisoft\Validator\RuleHandlerInterface;
12
use Yiisoft\Validator\ValidationContext;
13
use Yiisoft\Yii\Gii\GeneratorCommandInterface;
14
use Yiisoft\Yii\Gii\GeneratorInterface;
15
use Yiisoft\Yii\Gii\GeneratorProxy;
16
use Yiisoft\Yii\Gii\GiiInterface;
17
use Yiisoft\Yii\Gii\ParametersProvider;
18
19
final class TemplateRuleHandler implements RuleHandlerInterface
20
{
21 7
    public function __construct(
22
        private readonly Aliases $aliases,
23
        private readonly GiiInterface $gii,
24
        private readonly ParametersProvider $parametersProvider,
25
    ) {
26 7
    }
27
28
    /**
29
     * Validates the template selection.
30
     * This method validates whether the user selects an existing template
31
     * and the template contains all required template files as specified in {@see requiredTemplates()}.
32
     */
33 7
    public function validate(mixed $value, object $rule, ValidationContext $context): Result
34
    {
35 7
        if (!$rule instanceof TemplateRule) {
36
            throw new UnexpectedRuleException(TemplateRule::class, $rule);
37
        }
38 7
        $result = new Result();
39
40 7
        if ($value === 'default') {
41 3
            return $result;
42
        }
43 4
        $command = $context->getRawData();
44 4
        if (!$command instanceof GeneratorCommandInterface) {
45
            throw new RuntimeException(sprintf(
46
                'Unsupported dataset class "%s".',
47
                get_debug_type($command)
48
            ));
49
        }
50 4
        $generator = $this->getGenerator($command);
51 4
        $templates = $this->parametersProvider->getTemplates($generator::getId());
52
53 4
        if ($templates === []) {
54
            return $result;
55
        }
56 4
        if (!isset($templates[$value])) {
57 2
            $result->addError(
58 2
                message: 'Template "{template}" does not exist. Known templates: {templates}',
59 2
                parameters: [
60 2
                    'template' => $value,
61 2
                    'templates' => implode(', ', array_keys($templates)),
62 2
                ],
63 2
            );
64 2
            return $result;
65
        }
66
67 2
        $templatePath = $templates[$value];
68 2
        foreach ($generator->getRequiredTemplates() as $template) {
69 2
            if (!is_file($this->aliases->get($templatePath . '/' . $template))) {
70
                $result->addError(
71
                    message: 'Unable to find the required code template file "{template}".',
72
                    parameters: ['template' => $template],
73
                );
74
            }
75
        }
76
77 2
        return $result;
78
    }
79
80 4
    private function getGenerator(GeneratorCommandInterface $dataSet): GeneratorInterface
81
    {
82 4
        foreach ($this->gii->getGenerators() as $generator) {
83 4
            if ($generator instanceof GeneratorInterface && $generator::getCommandClass() === $dataSet::class) {
84 2
                return $generator;
85
            }
86 2
            if ($generator instanceof GeneratorProxy && $generator->getClass()::getCommandClass() === $dataSet::class) {
87 2
                return $generator->loadGenerator();
88
            }
89
        }
90
        throw new RuntimeException(sprintf('Unknown generator "%s".', $dataSet::class));
91
    }
92
}
93