Completed
Push — master ( f2b8b4...a21374 )
by Tom
24s queued 11s
created

CacheFactory::__invoke()   C

Complexity

Conditions 13
Paths 121

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 14.6632

Importance

Changes 0
Metric Value
dl 0
loc 51
ccs 22
cts 28
cp 0.7856
rs 6.4416
c 0
b 0
f 0
cc 13
nc 121
nop 3
crap 14.6632

How to fix   Long Method    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
declare(strict_types=1);
4
5
namespace DoctrineModule\Service;
6
7
use Doctrine\Common\Cache;
8
use Doctrine\Common\Cache\CacheProvider;
9
use DoctrineModule\Cache\LaminasStorageCache;
10
use Interop\Container\ContainerInterface;
11
use Laminas\ServiceManager\ServiceLocatorInterface;
12
use RuntimeException;
13
use function is_string;
14
15
/**
16
 * Cache ServiceManager factory
17
 *
18
 * @link    http://www.doctrine-project.org/
19
 */
20
class CacheFactory extends AbstractFactory
21
{
22
    /**
23
     * {@inheritDoc}
24
     *
25
     * @return \Doctrine\Common\Cache\Cache
26
     *
27
     * @throws RuntimeException
28
     */
29 3
    public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null)
30
    {
31 3
        $options = $this->getOptions($container, 'cache');
32 3
        $class = $options->getClass();
33
34 3
        if (! $class) {
35
            throw new RuntimeException('Cache must have a class name to instantiate');
36
        }
37
38 3
        $instance = $options->getInstance();
39
40 3
        if (is_string($instance) && $container->has($instance)) {
41 1
            $instance = $container->get($instance);
42
        }
43
44 3
        if ($container->has($class)) {
45 1
            $cache = $container->get($class);
46
        } else {
47 2
            switch ($class) {
48
                case Cache\FilesystemCache::class:
49
                    $cache = new $class($options->getDirectory());
50
                    break;
51
52
                case LaminasStorageCache::class:
53
                case Cache\PredisCache::class:
54 1
                    $cache = new $class($instance);
55 1
                    break;
56
57
                default:
58 1
                    $cache = new $class();
59 1
                    break;
60
            }
61
        }
62
63 3
        if ($cache instanceof Cache\MemcacheCache) {
64
            $cache->setMemcache($instance);
65 3
        } elseif ($cache instanceof Cache\MemcachedCache) {
66
            $cache->setMemcached($instance);
67 3
        } elseif ($cache instanceof Cache\RedisCache) {
68
            $cache->setRedis($instance);
69
        }
70
71 3
        if ($cache instanceof CacheProvider) {
72 3
            $namespace = $options->getNamespace();
73 3
            if ($namespace) {
74 2
                $cache->setNamespace($namespace);
75
            }
76
        }
77
78 3
        return $cache;
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     *
84
     * @return \Doctrine\Common\Cache\Cache
85
     *
86
     * @throws RuntimeException
87
     */
88 5
    public function createService(ServiceLocatorInterface $container)
89
    {
90 5
        return $this($container, \Doctrine\Common\Cache\Cache::class);
91
    }
92
93 3
    public function getOptionsClass() : string
94
    {
95 3
        return 'DoctrineModule\Options\Cache';
96
    }
97
}
98