Passed
Pull Request — master (#320)
by Dmitriy
02:52
created

SimpleRuleHandlerContainer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 0
c 0
b 0
f 0
dl 0
loc 2
ccs 1
cts 1
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\Validator\Exception\RuleHandlerInterfaceNotImplementedException;
8
use Yiisoft\Validator\Exception\RuleHandlerNotFoundException;
9
10
use function array_key_exists;
11
12
final class SimpleRuleHandlerContainer implements RuleHandlerResolverInterface
13
{
14
    private array $instances = [];
15
16 118
    public function resolve(string $className): RuleHandlerInterface
17
    {
18 118
        if (!class_exists($className)) {
19 1
            throw new RuleHandlerNotFoundException($className);
20
        }
21
22 117
        if (array_key_exists($className, $this->instances)) {
23 35
            return $this->instances[$className];
24
        }
25
26 104
        $classInstance = new $className();
27
28 104
        if (!$classInstance instanceof RuleHandlerInterface) {
29 1
            throw new RuleHandlerInterfaceNotImplementedException($className);
30
        }
31
32 103
        return $this->instances[$className] = $classInstance;
33
    }
34
}
35