Passed
Pull Request — master (#134)
by
unknown
07:59 queued 05:19
created

TableExistsHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 76.92%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 12
c 2
b 0
f 0
dl 0
loc 27
ccs 10
cts 13
cp 0.7692
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A validate() 0 20 4
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 4
    public function __construct(
19
        private readonly ConnectionInterface $connection
20
    ) {
21 4
    }
22
23 4
    public function validate(mixed $value, object $rule, ValidationContext $context): Result
24
    {
25 4
        if (!$rule instanceof TableExistsRule) {
26
            throw new UnexpectedRuleException(TableExistsRule::class, $rule);
27
        }
28
29 4
        $result = new Result();
30
31
        try {
32 4
            $tableSchema = $this->connection->getTableSchema($value);
33
        } catch (\Yiisoft\Db\Exception\Exception $e) {
34
            $tableSchema = null;
35
        }
36
37 4
        if ($tableSchema === null) {
38 1
            $result->addError(sprintf('Table "%s" does not exist.', $value));
39 1
            return $result;
40
        }
41
42 3
        return $result;
43
    }
44
}
45