Passed
Pull Request — master (#58)
by Dmitriy
04:36 queued 02:06
created

LazyDefinition   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 81.82%

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 33
ccs 9
cts 11
cp 0.8182
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 Psr\Container\ContainerInterface;
9
use Yiisoft\Definitions\Contract\DefinitionInterface;
10
use Yiisoft\Definitions\Helpers\Normalizer;
11
12
final class LazyDefinition implements DefinitionInterface
13
{
14 3
    public function __construct(
15
        private mixed $definition,
16
        /**
17
         * @var class-string
18
         */
19
        private string $objectClass,
20
    ) {
21 3
    }
22
23
    /**
24
     * @psalm-suppress MixedArgumentTypeCoercion
25
     */
26 3
    public function resolve(ContainerInterface $container): mixed
27
    {
28
        /** @var LazyLoadingValueHolderFactory $factory */
29 3
        $factory = $container->get(LazyLoadingValueHolderFactory::class);
30
        /**
31
         * @var mixed $definition
32
         */
33 3
        $definition = $this->definition;
34 3
        $objectClass = $this->objectClass;
35
36
        /** @psalm-suppress InvalidArgument */
37 3
        return $factory->createProxy(
38 3
            $objectClass,
39 3
            function (mixed &$wrappedObject) use ($container, $objectClass, $definition) {
40
                $definition = Normalizer::normalize($definition, $objectClass);
41
                /**
42
                 * @var mixed $wrappedObject
43
                 */
44
                $wrappedObject = $definition->resolve($container);
45 3
            }
46 3
        );
47
    }
48
}
49