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