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

SimpleRuleHandlerContainer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 5
eloc 9
c 2
b 1
f 0
dl 0
loc 24
ccs 9
cts 10
cp 0.9
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 14 4
A __construct() 0 6 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