Completed
Pull Request — master (#105)
by Mikołaj
04:26 queued 01:11
created

CacheFactory   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 93.55%

Importance

Changes 0
Metric Value
dl 0
loc 83
ccs 29
cts 31
cp 0.9355
rs 10
c 0
b 0
f 0
wmc 15
lcom 0
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
D resolveCacheAdapter() 0 29 10
A __invoke() 0 8 1
A getAdapter() 0 13 4
1
<?php
2
namespace Shlinkio\Shlink\Common\Factory;
3
4
use Doctrine\Common\Cache;
5
use Interop\Container\ContainerInterface;
6
use Interop\Container\Exception\ContainerException;
7
use Shlinkio\Shlink\Common;
8
use Shlinkio\Shlink\Core\Options\AppOptions;
9
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
10
use Zend\ServiceManager\Exception\ServiceNotFoundException;
11
use Zend\ServiceManager\Factory\FactoryInterface;
12
13
class CacheFactory implements FactoryInterface
14
{
15
    const VALID_CACHE_ADAPTERS = [
16
        Cache\ApcuCache::class,
17
        Cache\ArrayCache::class,
18
        Cache\FilesystemCache::class,
19
        Cache\PhpFileCache::class,
20
        Cache\MemcachedCache::class,
21
    ];
22
23
    /**
24
     * Create an object
25
     *
26
     * @param  ContainerInterface $container
27
     * @param  string $requestedName
28
     * @param  null|array $options
29
     * @return object
30
     * @throws ServiceNotFoundException if unable to resolve the service.
31
     * @throws ServiceNotCreatedException if an exception is raised when
32
     *     creating a service.
33
     * @throws ContainerException if any other error occurs
34
     */
35 6
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
36
    {
37 6
        $appOptions = $container->get(AppOptions::class);
38 6
        $adapter = $this->getAdapter($container);
39 6
        $adapter->setNamespace($appOptions->__toString());
40
41 6
        return $adapter;
42
    }
43
44
    /**
45
     * @param ContainerInterface $container
46
     * @return Cache\CacheProvider
47
     */
48 6
    protected function getAdapter(ContainerInterface $container)
49
    {
50
        // Try to get the adapter from config
51 6
        $config = $container->get('config');
52 6
        if (isset($config['cache'], $config['cache']['adapter'])
53 6
            && in_array($config['cache']['adapter'], self::VALID_CACHE_ADAPTERS)
54
        ) {
55 3
            return $this->resolveCacheAdapter($config['cache']);
56
        }
57
58
        // If the adapter has not been set in config, create one based on environment
59 3
        return Common\env('APP_ENV', 'pro') === 'pro' ? new Cache\ApcuCache() : new Cache\ArrayCache();
60
    }
61
62
    /**
63
     * @param array $cacheConfig
64
     * @return Cache\CacheProvider
65
     */
66 3
    protected function resolveCacheAdapter(array $cacheConfig)
67
    {
68 3
        switch ($cacheConfig['adapter']) {
69 3
            case Cache\ArrayCache::class:
70 2
            case Cache\ApcuCache::class:
71 1
                return new $cacheConfig['adapter']();
72 2
            case Cache\FilesystemCache::class:
73 1
            case Cache\PhpFileCache::class:
74 1
                return new $cacheConfig['adapter']($cacheConfig['options']['dir']);
75 1
            case Cache\MemcachedCache::class:
76 1
                $memcached = new \Memcached();
77 1
                $servers = isset($cacheConfig['options']['servers']) ? $cacheConfig['options']['servers'] : [];
78
79 1
                foreach ($servers as $server) {
80 1
                    if (! isset($server['host'])) {
81
                        continue;
82
                    }
83 1
                    $port = isset($server['port']) ? (int) $server['port'] : 11211;
84
85 1
                    $memcached->addServer($server['host'], $port);
86
                }
87
88 1
                $cache = new Cache\MemcachedCache();
89 1
                $cache->setMemcached($memcached);
90 1
                return $cache;
91
            default:
92
                return new Cache\ArrayCache();
93
        }
94
    }
95
}
96