Passed
Pull Request — master (#58)
by Dmitriy
12:28 queued 09:49
created

LazyDefinition::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 7
ccs 1
cts 1
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 3
    public function __construct(
16
        private mixed $definition,
17
        /**
18
         * @psalm-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