Proxy::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Fundic\Factory\Decorator;
6
7
use Fundic\Factory\ValueFactory;
8
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
9
use ProxyManager\Proxy\LazyLoadingInterface;
10
use Psr\Container\ContainerInterface;
11
12
final class Proxy implements ValueFactory
13
{
14
    /**
15
     * @var ValueFactory
16
     */
17
    private $inner;
18
19
    /**
20
     * @var string
21
     */
22
    private $className;
23
24
    public function __construct(
25
        ValueFactory $inner,
26
        ?string $className = null
27
    ) {
28
        $this->inner = $inner;
29
        $this->className = $className;
30
    }
31
32
    public function __invoke(ContainerInterface $container, string $name)
33
    {
34
        $inner = $this->inner;
35
36
        $proxyFactory = new LazyLoadingValueHolderFactory();
37
        $initializer = function(
38
            & $wrappedObject,
39
            LazyLoadingInterface $proxy
40
        ) use ($container, $inner, $name) {
41
            $proxy->setProxyInitializer(null);
42
            $wrappedObject = $inner($container, $name);
43
44
            return true;
45
        };
46
47
        return $proxyFactory->createProxy($this->className ?? $name, $initializer);
48
    }
49
}
50