CacheFactory   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 81.25%

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 6
dl 0
loc 78
ccs 26
cts 32
cp 0.8125
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createService() 0 4 1
C __invoke() 0 51 13
A getOptionsClass() 0 4 1
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