Passed
Pull Request — master (#237)
by Rustam
02:26
created

RuleHandlerContainer::resolve()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
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 RuleHandlerContainer 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