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