Passed
Pull Request — master (#58)
by Sergei
02:34
created

LazyDefinition::resolve()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

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