Test Failed
Pull Request — master (#293)
by Alexander
07:25 queued 04:59
created

SimpleRuleHandlerContainer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A resolve() 0 14 4
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 115
15
    private TranslatorInterface $translator;
16 115
17 1
    public function __construct(TranslatorInterface $translator)
18
    {
19 114
        $this->translator = $translator;
20 101
    }
21 101
22 1
    public function resolve(string $className): RuleHandlerInterface
23
    {
24 100
        if (!class_exists($className)) {
25
            throw new RuleHandlerNotFoundException($className);
26
        }
27 35
        if (!array_key_exists($className, $this->instances)) {
28
            $classInstance = new $className($this->translator);
29
            if (!$classInstance instanceof RuleHandlerInterface) {
30
                throw new RuleHandlerInterfaceNotImplementedException($className);
31
            }
32
            return $this->instances[$className] = $classInstance;
33
        }
34
35
        return $this->instances[$className];
36
    }
37
}
38