Completed
Pull Request — master (#281)
by Marco
04:41
created

LazyLoadingGhostFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 63
ccs 5
cts 5
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getGenerator() 0 4 2
A createProxy() 0 9 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\Factory;
22
23
use Closure;
24
use ProxyManager\Proxy\GhostObjectInterface;
25
use ProxyManager\ProxyGenerator\LazyLoadingGhostGenerator;
26
use ProxyManager\ProxyGenerator\ProxyGeneratorInterface;
27
28
/**
29
 * Factory responsible of producing ghost instances
30
 *
31
 * @author Marco Pivetta <[email protected]>
32
 * @license MIT
33
 */
34
class LazyLoadingGhostFactory extends AbstractBaseFactory
35
{
36
    /**
37
     * @var \ProxyManager\ProxyGenerator\LazyLoadingGhostGenerator|null
38
     */
39
    private $generator;
40
41
    /**
42
     * {@inheritDoc}
43
     */
44 1
    protected function getGenerator() : ProxyGeneratorInterface
45
    {
46 1
        return $this->generator ?: $this->generator = new LazyLoadingGhostGenerator();
47
    }
48
49
    /**
50
     * Creates a new lazy proxy instance of the given class with
51
     * the given initializer
52
     *
53
     * Please refer to the following documentation when using this method:
54
     *
55
     * @link https://github.com/Ocramius/ProxyManager/blob/master/docs/lazy-loading-ghost-object.md
56
     *
57
     * @param string  $className   name of the class to be proxied
58
     * @param Closure $initializer initializer to be passed to the proxy. The initializer closure should have following
59
     *                             signature:
60
     *
61
     *                             <code>
62
     *                             $initializer = function (
63
     *                                 GhostObjectInterface $proxy,
64
     *                                 string $method,
65
     *                                 array $parameters,
66
     *                                 & $initializer,
67
     *                                 array $properties
68
     *                             ) {};
69
     *                             </code>
70
     *
71
     *                             Where:
72
     *                              - $proxy is the proxy instance on which the initializer is acting
73
     *                              - $method is the name of the method that triggered the lazy initialization
74
     *                              - $parameters are the parameters that were passed to $method
75
     *                              - $initializer by-ref initializer - should be assigned null in the initializer body
76
     *                              - $properties a by-ref map of the properties of the object, indexed by PHP
77
     *                                            internal property name. Assign values to it to initialize the
78
     *                                            object state
79
     *
80
     * @param mixed[] $proxyOptions a set of options to be used when generating the proxy. Currently supports only
81
     *                              key "skippedProperties", which allows to skip lazy-loading of some properties.
82
     *                              "skippedProperties" is a string[], containing a list of properties referenced
83
     *                              via PHP's internal property name (i.e. "\0ClassName\0propertyName")
84
     *
85
     * @return GhostObjectInterface
86
     */
87 2
    public function createProxy(
88
        string $className,
89
        Closure $initializer,
90
        array $proxyOptions = []
91
    ) : GhostObjectInterface {
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...
92 2
        $proxyClassName = $this->generateProxy($className, $proxyOptions);
93
94 2
        return $proxyClassName::staticProxyConstructor($initializer);
95
    }
96
}
97