Passed
Push — master ( f9d022...66a5c5 )
by Sergei
02:37
created

RuleHandlerContainer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\RuleHandlerResolver;
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\RuleHandlerInterface;
12
use Yiisoft\Validator\RuleHandlerResolverInterface;
13
14
final class RuleHandlerContainer implements RuleHandlerResolverInterface
15
{
16
    public function __construct(private ContainerInterface $container)
17
    {
18
    }
19
20
    public function resolve(string $className): RuleHandlerInterface
21
    {
22
        try {
23
            $ruleHandler = $this->container->get($className);
24
        } catch (NotFoundExceptionInterface $e) {
25
            throw new RuleHandlerNotFoundException($className, $e);
26
        }
27
28
        if (!$ruleHandler instanceof RuleHandlerInterface) {
29
            throw new RuleHandlerInterfaceNotImplementedException($className);
30
        }
31
32
        return $ruleHandler;
33
    }
34
}
35