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

TemplateRuleHandler   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 72.5%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
eloc 33
c 2
b 0
f 0
dl 0
loc 69
ccs 29
cts 40
cp 0.725
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getGenerator() 0 8 3
B validate() 0 45 8
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\GiiInterface;
16
use Yiisoft\Yii\Gii\ParametersProvider;
17
18
final class TemplateRuleHandler implements RuleHandlerInterface
19
{
20 7
    public function __construct(
21
        private readonly Aliases $aliases,
22
        private readonly GiiInterface $gii,
23
        private readonly ParametersProvider $parametersProvider,
24
    ) {
25 7
    }
26
27
    /**
28
     * Validates the template selection.
29
     * This method validates whether the user selects an existing template
30
     * and the template contains all required template files as specified in {@see requiredTemplates()}.
31
     */
32 7
    public function validate(mixed $value, object $rule, ValidationContext $context): Result
33
    {
34 7
        if (!$rule instanceof TemplateRule) {
35
            throw new UnexpectedRuleException(TemplateRule::class, $rule);
36
        }
37 7
        $result = new Result();
38
39 7
        if ($value === 'default') {
40 3
            return $result;
41
        }
42 4
        $command = $context->getRawData();
43 4
        if (!$command instanceof GeneratorCommandInterface) {
44
            throw new RuntimeException(sprintf(
45
                'Unsupported dataset class "%s".',
46
                get_debug_type($command)
47
            ));
48
        }
49 4
        $generator = $this->getGenerator($command);
50 4
        $templates = $this->parametersProvider->getTemplates($generator::getId());
51
52 4
        if ($templates === []) {
53
            return $result;
54
        }
55 4
        if (!isset($templates[$value])) {
56 2
            $result->addError(
57 2
                message: 'Template "{template}" does not exist. Known templates: {templates}',
58 2
                parameters: [
59 2
                    'template' => $value,
60 2
                    'templates' => implode(', ', array_keys($templates)),
61 2
                ],
62 2
            );
63 2
            return $result;
64
        }
65
66 2
        $templatePath = $templates[$value];
67 2
        foreach ($generator->getRequiredTemplates() as $template) {
68 2
            if (!is_file($this->aliases->get($templatePath . '/' . $template))) {
69
                $result->addError(
70
                    message: 'Unable to find the required code template file "{template}".',
71
                    parameters: ['template' => $template],
72
                );
73
            }
74
        }
75
76 2
        return $result;
77
    }
78
79 4
    private function getGenerator(GeneratorCommandInterface $dataSet): GeneratorInterface
80
    {
81 4
        foreach ($this->gii->getGenerators() as $generator) {
82 4
            if ($generator::getCommandClass() === $dataSet::class) {
83 4
                return $generator;
84
            }
85
        }
86
        throw new RuntimeException(sprintf('Unknown generator "%s".', $dataSet::class));
87
    }
88
}
89