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

ExtensibleService   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A addExtension() 0 3 1
A resolve() 0 9 2
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
    private $definition;
15
    private array $extensions;
16
17 2
    public function __construct($definition)
18
    {
19 2
        $this->definition = $definition;
20 2
    }
21
22 2
    public function addExtension(\Closure $closure): void
23
    {
24 2
        $this->extensions[] = $closure;
25 2
    }
26
27 2
    public function resolve(DependencyResolverInterface $resolver)
28
    {
29 2
        $service = (Normalizer::normalize($this->definition))->resolve($resolver);
30 2
        $container = $resolver->get(ContainerInterface::class);
31 2
        foreach ($this->extensions as $extension) {
32 2
            $service = $extension($container, $service);
33
        }
34
35 2
        return $service;
36
    }
37
}
38