Completed
Push — master ( c11d86...d758ed )
by Gianluca
13:50
created

CacheFactory::__invoke()   C

Complexity

Conditions 12
Paths 65

Size

Total Lines 47
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 13.8285

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 47
ccs 23
cts 30
cp 0.7667
rs 5.1384
cc 12
eloc 27
nc 65
nop 3
crap 13.8285

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace DoctrineModule\Service;
21
22
use Doctrine\Common\Cache\CacheProvider;
23
use Interop\Container\ContainerInterface;
24
use RuntimeException;
25
use Doctrine\Common\Cache\MemcacheCache;
26
use Doctrine\Common\Cache\MemcachedCache;
27
use Doctrine\Common\Cache\RedisCache;
28
use Zend\ServiceManager\ServiceLocatorInterface;
29
30
/**
31
 * Cache ServiceManager factory
32
 *
33
 * @license MIT
34
 * @link    http://www.doctrine-project.org/
35
 * @author  Kyle Spraggs <[email protected]>
36
 */
37
class CacheFactory extends AbstractFactory
38
{
39
    /**
40
     * {@inheritDoc}
41
     *
42
     * @return \Doctrine\Common\Cache\Cache
43
     *
44
     * @throws RuntimeException
45
     */
46 2
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
47
    {
48
        /** @var $options \DoctrineModule\Options\Cache */
49 2
        $options = $this->getOptions($container, 'cache');
50 2
        $class   = $options->getClass();
51
52 2
        if (!$class) {
53
            throw new RuntimeException('Cache must have a class name to instantiate');
54
        }
55
56 2
        $instance = $options->getInstance();
57
58 2
        if (is_string($instance) && $container->has($instance)) {
59 1
            $instance = $container->get($instance);
60 1
        }
61
62
        switch ($class) {
63 2
            case 'Doctrine\Common\Cache\FilesystemCache':
64
                $cache = new $class($options->getDirectory());
65
                break;
66
67 2
            case 'DoctrineModule\Cache\ZendStorageCache':
68 2
            case 'Doctrine\Common\Cache\PredisCache':
69 1
                $cache = new $class($instance);
70 1
                break;
71
72 1
            default:
73 1
                $cache = new $class;
74 1
        }
75
76 2
        if ($cache instanceof MemcacheCache) {
77
            /* @var $cache MemcacheCache */
78
            $cache->setMemcache($instance);
79 2
        } elseif ($cache instanceof MemcachedCache) {
80
            /* @var $cache MemcachedCache */
81
            $cache->setMemcached($instance);
82 2
        } elseif ($cache instanceof RedisCache) {
83
            /* @var $cache RedisCache */
84
            $cache->setRedis($instance);
85
        }
86
87 2
        if ($cache instanceof CacheProvider && ($namespace = $options->getNamespace())) {
88 2
            $cache->setNamespace($namespace);
89 2
        }
90
91 2
        return $cache;
92
    }
93
94
    /**
95
     * {@inheritDoc}
96
     *
97
     * @return \Doctrine\Common\Cache\Cache
98
     *
99
     * @throws RuntimeException
100
     */
101 4
    public function createService(ServiceLocatorInterface $container)
102
    {
103 4
        return $this($container, \Doctrine\Common\Cache\Cache::class);
104
    }
105
106
    /**
107
     * {@inheritDoc}
108
     */
109 2
    public function getOptionsClass()
110
    {
111 2
        return 'DoctrineModule\Options\Cache';
112
    }
113
}
114