Passed
Pull Request — master (#237)
by Rustam
13:25
created

ContainerRuleHandlerResolver   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 10
dl 0
loc 22
ccs 9
cts 9
cp 1
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 13 3
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator;
6
7
use Psr\Container\ContainerInterface;
8
use Psr\Container\NotFoundExceptionInterface;
9
use Yiisoft\Validator\Exception\RuleHandlerInterfaceNotImplementedException;
10
use Yiisoft\Validator\Exception\RuleHandlerNotFoundException;
11
use Yiisoft\Validator\Rule\RuleHandlerInterface;
12
13
final class ContainerRuleHandlerResolver implements RuleHandlerResolverInterface
14
{
15
    private ContainerInterface $container;
16
17 3
    public function __construct(ContainerInterface $container)
18
    {
19 3
        $this->container = $container;
20
    }
21
22 3
    public function resolve(string $className): RuleHandlerInterface
23
    {
24
        try {
25 3
            $ruleHandler = $this->container->get($className);
26 1
        } catch (NotFoundExceptionInterface $e) {
27 1
            throw new RuleHandlerNotFoundException($className, $e);
28
        }
29
30 2
        if (!$ruleHandler instanceof RuleHandlerInterface) {
31 1
            throw new RuleHandlerInterfaceNotImplementedException($className);
32
        }
33
34 1
        return $ruleHandler;
35
    }
36
}
37