Passed
Pull Request — master (#222)
by Rustam
02:53 queued 11s
created

StaticRuleHandlerResolver::resolve()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 5
c 2
b 0
f 0
dl 0
loc 10
ccs 4
cts 6
cp 0.6667
rs 10
cc 3
nc 3
nop 1
crap 3.3332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator;
6
7
use Yiisoft\Validator\Exception\RuleHandlerInterfaceNotImplementedException;
8
use Yiisoft\Validator\Exception\RuleHandlerNotFoundException;
9
use Yiisoft\Validator\Rule\RuleHandlerInterface;
10
11
final class StaticRuleHandlerResolver implements RuleHandlerResolverInterface
12
{
13
    private array $handlers;
14
15 518
    public function __construct(array $handlers)
16
    {
17 518
        $this->handlers = $handlers;
18
    }
19
20 28
    public function resolve(string $className): RuleHandlerInterface
21
    {
22 28
        if (!in_array($className, $this->handlers, true)) {
23
            throw new RuleHandlerNotFoundException($className);
24
        }
25 28
        if (!is_subclass_of($className, RuleHandlerInterface::class)) {
26
            throw new RuleHandlerInterfaceNotImplementedException($className);
27
        }
28
29 28
        return new $className();
30
    }
31
}
32