Test Failed
Pull Request — master (#110)
by Dmitriy
03:10
created

TableExistsHandler::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 16
rs 10
cc 3
nc 3
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Gii\Validator;
6
7
use Yiisoft\Db\Connection\ConnectionInterface;
8
use Yiisoft\Validator\Exception\UnexpectedRuleException;
9
use Yiisoft\Validator\Result;
10
use Yiisoft\Validator\RuleHandlerInterface;
11
use Yiisoft\Validator\ValidationContext;
12
13
/**
14
 * An inline validator that checks if the attribute value refers to an existing class name.
15
 */
16
final class TableExistsHandler implements RuleHandlerInterface
17
{
18
    public function __construct(
19
        private readonly ConnectionInterface $connection
20
    ) {
21
    }
22
23
    public function validate(mixed $value, object $rule, ValidationContext $context): Result
24
    {
25
        if (!$rule instanceof TableExistsRule) {
26
            throw new UnexpectedRuleException(TableExistsRule::class, $rule);
27
        }
28
29
        $result = new Result();
30
31
        $tableSchema = $this->connection->getTableSchema($value);
32
33
        if ($tableSchema === null) {
34
            $result->addError(sprintf('Table "%s" does not exist.', $value));
35
            return $result;
36
        }
37
38
        return $result;
39
    }
40
}
41