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

ExtensibleService::addExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
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