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

SimpleRuleHandlerContainer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
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 6
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
final class SimpleRuleHandlerContainer implements RuleHandlerResolverInterface
11
{
12 600
    public function __construct(
13
        /**
14
         * @var array<string, RuleHandlerInterface>
15
         */
16
        private array $instances = [],
17
    ) {
18
    }
19
20 61
    public function resolve(string $className): RuleHandlerInterface
21
    {
22 61
        if (!class_exists($className)) {
23 1
            throw new RuleHandlerNotFoundException($className);
24
        }
25 60
        if (!array_key_exists($className, $this->instances)) {
26 1
            $classInstance = new $className();
27 1
            if (!$classInstance instanceof RuleHandlerInterface) {
28 1
                throw new RuleHandlerInterfaceNotImplementedException($className);
29
            }
30
            return $this->instances[$className] = $classInstance;
31
        }
32
33 59
        return $this->instances[$className];
34
    }
35
}
36