Completed
Push — master ( 96faaf...c1c325 )
by Alejandro
03:51
created

CacheFactory::resolveCacheAdapter()   D

Complexity

Conditions 10
Paths 13

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 10.5258

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 10
eloc 21
c 3
b 1
f 0
nc 13
nop 1
dl 0
loc 29
ccs 19
cts 23
cp 0.8261
crap 10.5258
rs 4.8196

How to fix   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
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\Core\Options\AppOptions;
8
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
9
use Zend\ServiceManager\Exception\ServiceNotFoundException;
10
use Zend\ServiceManager\Factory\FactoryInterface;
11
12
class CacheFactory implements FactoryInterface
13
{
14
    const VALID_CACHE_ADAPTERS = [
15
        Cache\ApcuCache::class,
16
        Cache\ArrayCache::class,
17
        Cache\FilesystemCache::class,
18
        Cache\PhpFileCache::class,
19
        Cache\MemcachedCache::class,
20
    ];
21
22
    /**
23
     * Create an object
24
     *
25
     * @param  ContainerInterface $container
26
     * @param  string $requestedName
27
     * @param  null|array $options
28
     * @return object
29
     * @throws ServiceNotFoundException if unable to resolve the service.
30
     * @throws ServiceNotCreatedException if an exception is raised when
31
     *     creating a service.
32
     * @throws ContainerException if any other error occurs
33
     */
34 6
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
35
    {
36 6
        $appOptions = $container->get(AppOptions::class);
37 6
        $adapter = $this->getAdapter($container);
38 6
        $adapter->setNamespace($appOptions->__toString());
39
40 6
        return $adapter;
41
    }
42
43
    /**
44
     * @param ContainerInterface $container
45
     * @return Cache\CacheProvider
46
     */
47 6
    protected function getAdapter(ContainerInterface $container)
48
    {
49
        // Try to get the adapter from config
50 6
        $config = $container->get('config');
51 6
        if (isset($config['cache'])
52 6
            && isset($config['cache']['adapter'])
53 6
            && in_array($config['cache']['adapter'], self::VALID_CACHE_ADAPTERS)
54 6
        ) {
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 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 3
            case Cache\ApcuCache::class:
71 1
                return new $cacheConfig['adapter']();
72 2
            case Cache\FilesystemCache::class:
73 2
            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']) ? intval($server['port']) : 11211;
84
85 1
                    $memcached->addServer($server['host'], $port);
86 1
                }
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