Completed
Push — master ( 3a0f53...56ae52 )
by Tobias
20:55
created

DecoratingFactory::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of php-cache\cache-bundle package.
5
 *
6
 * (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Cache\CacheBundle\DataCollector;
13
14
use Nyholm\NSA;
15
use Psr\Cache\CacheItemPoolInterface;
16
17
/**
18
 * A factory that decorates another factory to be able to use the proxy cache.
19
 *
20
 * @author Tobias Nyholm <[email protected]>
21
 */
22
class DecoratingFactory
23
{
24
    /**
25
     * @type ProxyFactory
26
     */
27
    private $proxyFactory;
28
29
    /**
30
     * @param ProxyFactory $proxyFactory
31
     */
32
    public function __construct(ProxyFactory $proxyFactory)
33
    {
34
        $this->proxyFactory = $proxyFactory;
35
    }
36
37
    /**
38
     * @param CacheItemPoolInterface $originalObject original class
39
     *
40
     * @return CacheProxy|CacheItemPoolInterface
41
     */
42
    public function create($originalObject)
43
    {
44
        $proxyClass = $this->proxyFactory->createProxy(get_class($originalObject));
45
        $rc         = new \ReflectionClass($proxyClass);
46
        $pool       = $rc->newInstanceWithoutConstructor();
47
48
        // Copy properties from original pool to new
49
        foreach (NSA::getProperties($originalObject) as $property) {
50
            NSA::setProperty($pool, $property, NSA::getProperty($originalObject, $property));
51
        }
52
53
        return $pool;
54
    }
55
}
56