Passed
Pull Request — master (#293)
by Alexander
02:52
created

SimpleRuleHandlerContainer::__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 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator;
6
7
use Yiisoft\Translator\TranslatorInterface;
8
use Yiisoft\Validator\Exception\RuleHandlerInterfaceNotImplementedException;
9
use Yiisoft\Validator\Exception\RuleHandlerNotFoundException;
10
11
final class SimpleRuleHandlerContainer implements RuleHandlerResolverInterface
12
{
13
    private array $instances = [];
14
15
    private TranslatorInterface $translator;
16
17 639
    public function __construct(TranslatorInterface $translator)
18
    {
19 639
        $this->translator = $translator;
20
    }
21
22 100
    public function resolve(string $className): RuleHandlerInterface
23
    {
24 100
        if (!class_exists($className)) {
25 1
            throw new RuleHandlerNotFoundException($className);
26
        }
27 99
        if (!array_key_exists($className, $this->instances)) {
28 86
            $classInstance = new $className($this->translator);
29 86
            if (!$classInstance instanceof RuleHandlerInterface) {
30 1
                throw new RuleHandlerInterfaceNotImplementedException($className);
31
            }
32 85
            return $this->instances[$className] = $classInstance;
33
        }
34
35 30
        return $this->instances[$className];
36
    }
37
}
38