Passed
Pull Request — master (#58)
by Sergei
03:14 queued 38s
created

LazyDefinition   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 23
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resolve() 0 12 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
    /**
16
     * @psalm-param class-string $className
17
     */
18 3
    public function __construct(
19
        private mixed $definition,
20
        private string $className,
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 3
        return $factory->createProxy(
30 3
            $this->className,
31 3
            function (&$wrappedObject) use ($container): bool {
32 1
                $definition = Normalizer::normalize($this->definition, $this->className);
33 1
                $wrappedObject = $definition->resolve($container);
34
35 1
                return true;
36 3
            }
37 3
        );
38
    }
39
}
40