Proxy   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 2
dl 0
loc 38
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A __invoke() 0 17 1
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