Passed
Pull Request — master (#248)
by Rustam
12:42
created

SimpleRuleHandlerContainer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Test Coverage

Coverage 88.89%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 10
c 1
b 0
f 0
dl 0
loc 18
ccs 8
cts 9
cp 0.8889
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 14 4
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
use Yiisoft\Validator\Rule\RuleHandlerInterface;
10
11
final class SimpleRuleHandlerContainer implements RuleHandlerResolverInterface
12
{
13
    private array $instances = [];
14
15 36
    public function resolve(string $className): RuleHandlerInterface
16
    {
17 36
        if (!class_exists($className)) {
18
            throw new RuleHandlerNotFoundException($className);
19
        }
20 36
        if (!array_key_exists($className, $this->instances)) {
21 36
            $classInstance = new $className();
22 36
            if (!$classInstance instanceof RuleHandlerInterface) {
23 1
                throw new RuleHandlerInterfaceNotImplementedException($className);
24
            }
25 35
            return $this->instances[$className] = $classInstance;
26
        }
27
28 8
        return $this->instances[$className];
29
    }
30
}
31