Passed
Pull Request — master (#58)
by Sergei
06:12 queued 03:39
created

LazyDefinition   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 30
ccs 9
cts 12
cp 0.75
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A resolve() 0 19 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Definitions;
6
7
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
8
use ProxyManager\Proxy\VirtualProxyInterface;
9
use Psr\Container\ContainerInterface;
10
use Yiisoft\Definitions\Contract\DefinitionInterface;
11
use Yiisoft\Definitions\Helpers\Normalizer;
12
13
final class LazyDefinition implements DefinitionInterface
14
{
15 3
    public function __construct(
16
        private mixed $definition,
17
        /**
18
         * @var class-string
19
         */
20
        private string $class,
21
    ) {
22 3
    }
23
24 3
    public function resolve(ContainerInterface $container): VirtualProxyInterface
25
    {
26
        /** @var LazyLoadingValueHolderFactory $factory */
27 3
        $factory = $container->get(LazyLoadingValueHolderFactory::class);
28
        /**
29
         * @var mixed $definition
30
         */
31 3
        $definition = $this->definition;
32 3
        $class = $this->class;
33
34 3
        return $factory->createProxy(
35 3
            $class,
36 3
            static function (mixed &$wrappedObject) use ($container, $class, $definition) {
37
                $definition = Normalizer::normalize($definition, $class);
38
                /**
39
                 * @var mixed $wrappedObject
40
                 */
41
                $wrappedObject = $definition->resolve($container);
42
                return true;
43 3
            }
44 3
        );
45
    }
46
}
47