Passed
Pull Request — master (#233)
by Dmitriy
02:17
created

ExtensibleService   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 10
c 3
b 0
f 0
dl 0
loc 26
ccs 12
cts 12
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 9 2
A __construct() 0 3 1
A addExtension() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Di;
6
7
use Psr\Container\ContainerInterface;
8
use Yiisoft\Factory\Definition\DefinitionInterface;
9
use Yiisoft\Factory\Definition\Normalizer;
10
use Yiisoft\Factory\DependencyResolverInterface;
11
12
final class ExtensibleService implements DefinitionInterface
13
{
14
    /** @psalm-var  array<string,mixed> */
15
    private $definition;
16
    private array $extensions = [];
17
18
    /** @param mixed $definition */
19 2
    public function __construct($definition)
20
    {
21 2
        $this->definition = $definition;
22 2
    }
23
24 2
    public function addExtension(\Closure $closure): void
25
    {
26 2
        $this->extensions[] = $closure;
27 2
    }
28
29 2
    public function resolve(DependencyResolverInterface $container)
30
    {
31 2
        $service = (Normalizer::normalize($this->definition))->resolve($container);
32 2
        $containerInterface = $container->get(ContainerInterface::class);
33 2
        foreach ($this->extensions as $extension) {
34 2
            $service = $extension($containerInterface, $service);
35
        }
36
37 2
        return $service;
38
    }
39
}
40