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

ContainerRuleHandlerResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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