Completed
Pull Request — develop (#683)
by Tom
01:23
created

CacheFactory::createService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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