Completed
Push — master ( 1a9936...b9df3b )
by Marco
23:13
created

LazyLoadingValueHolderGenerator   C

Complexity

Total Complexity 3

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 24

Test Coverage

Coverage 100%
Metric Value
wmc 3
lcom 0
cbo 24
dl 0
loc 65
ccs 34
cts 34
cp 1
rs 5.238

2 Methods

Rating   Name   Duplication   Size   Complexity  
B generate() 0 46 2
A buildLazyLoadingMethodInterceptor() 0 12 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license.
17
 */
18
19
declare(strict_types=1);
20
21
namespace ProxyManager\ProxyGenerator;
22
23
use ProxyManager\Generator\Util\ClassGeneratorUtils;
24
use ProxyManager\Proxy\VirtualProxyInterface;
25
use ProxyManager\ProxyGenerator\AccessInterceptor\MethodGenerator\MagicWakeup;
26
use ProxyManager\ProxyGenerator\Assertion\CanProxyAssertion;
27
use ProxyManager\ProxyGenerator\LazyLoading\MethodGenerator\StaticProxyConstructor;
28
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\GetProxyInitializer;
29
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\InitializeProxy;
30
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\IsProxyInitialized;
31
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\LazyLoadingMethodInterceptor;
32
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\MagicClone;
33
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\MagicGet;
34
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\MagicIsset;
35
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\MagicSet;
36
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\MagicSleep;
37
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\MagicUnset;
38
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\MethodGenerator\SetProxyInitializer;
39
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\PropertyGenerator\InitializerProperty;
40
use ProxyManager\ProxyGenerator\LazyLoadingValueHolder\PropertyGenerator\ValueHolderProperty;
41
use ProxyManager\ProxyGenerator\PropertyGenerator\PublicPropertiesMap;
42
use ProxyManager\ProxyGenerator\Util\Properties;
43
use ProxyManager\ProxyGenerator\Util\ProxiedMethodsFilter;
44
use ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\Constructor;
45
use ProxyManager\ProxyGenerator\ValueHolder\MethodGenerator\GetWrappedValueHolderValue;
46
use ReflectionClass;
47
use ReflectionMethod;
48
use Zend\Code\Generator\ClassGenerator;
49
use Zend\Code\Generator\MethodGenerator;
50
use Zend\Code\Reflection\MethodReflection;
51
52
/**
53
 * Generator for proxies implementing {@see \ProxyManager\Proxy\VirtualProxyInterface}
54
 *
55
 * {@inheritDoc}
56
 *
57
 * @author Marco Pivetta <[email protected]>
58
 * @license MIT
59
 */
60
class LazyLoadingValueHolderGenerator implements ProxyGeneratorInterface
61
{
62
    /**
63 8
     * {@inheritDoc}
64
     */
65 8
    public function generate(ReflectionClass $originalClass, ClassGenerator $classGenerator)
66
    {
67 8
        CanProxyAssertion::assertClassCanBeProxied($originalClass);
68 8
69
        $interfaces          = [VirtualProxyInterface::class];
70 8
        $publicProperties    = new PublicPropertiesMap(Properties::fromReflectionClass($originalClass));
71 2
72
        if ($originalClass->isInterface()) {
73 6
            $interfaces[] = $originalClass->getName();
74
        } else {
75
            $classGenerator->setExtendedClass($originalClass->getName());
76 8
        }
77 8
78 8
        $classGenerator->setImplementedInterfaces($interfaces);
79 8
        $classGenerator->addPropertyFromGenerator($valueHolder = new ValueHolderProperty());
80
        $classGenerator->addPropertyFromGenerator($initializer = new InitializerProperty());
81 8
        $classGenerator->addPropertyFromGenerator($publicProperties);
82
83 8
        array_map(
84 8
            function (MethodGenerator $generatedMethod) use ($originalClass, $classGenerator) {
85
                ClassGeneratorUtils::addMethodIfNotFinal($originalClass, $classGenerator, $generatedMethod);
86
            },
87 8
            array_merge(
88 5
                array_map(
89 5
                    $this->buildLazyLoadingMethodInterceptor($initializer, $valueHolder),
90
                    ProxiedMethodsFilter::getProxiedMethods($originalClass)
91
                ),
92
                [
93 8
                    new StaticProxyConstructor($initializer, Properties::fromReflectionClass($originalClass)),
94 8
                    Constructor::generateMethod($originalClass, $valueHolder),
95
                    new MagicGet($originalClass, $initializer, $valueHolder, $publicProperties),
96
                    new MagicSet($originalClass, $initializer, $valueHolder, $publicProperties),
97 8
                    new MagicIsset($originalClass, $initializer, $valueHolder, $publicProperties),
98 8
                    new MagicUnset($originalClass, $initializer, $valueHolder, $publicProperties),
99 8
                    new MagicClone($originalClass, $initializer, $valueHolder),
100 8
                    new MagicSleep($originalClass, $initializer, $valueHolder),
101 8
                    new MagicWakeup($originalClass),
102 8
                    new SetProxyInitializer($initializer),
103 8
                    new GetProxyInitializer($initializer),
104 8
                    new InitializeProxy($initializer, $valueHolder),
105 8
                    new IsProxyInitialized($valueHolder),
106 8
                    new GetWrappedValueHolderValue($valueHolder),
107 8
                ]
108 8
            )
109 8
        );
110 8
    }
111
112
    private function buildLazyLoadingMethodInterceptor(
113
        InitializerProperty $initializer,
114 8
        ValueHolderProperty $valueHolder
115
    ) : callable {
1 ignored issue
show
Coding Style introduced by
There must be a single space between the closing parenthesis and the opening brace of a multi-line function declaration
Loading history...
116
        return function (ReflectionMethod $method) use ($initializer, $valueHolder) : LazyLoadingMethodInterceptor {
117
            return LazyLoadingMethodInterceptor::generateMethod(
118
                new MethodReflection($method->getDeclaringClass()->getName(), $method->getName()),
119
                $initializer,
120
                $valueHolder
121
            );
122
        };
123
    }
124
}
125