CachePool   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 60%

Importance

Changes 0
Metric Value
dl 0
loc 65
rs 10
c 0
b 0
f 0
ccs 18
cts 30
cp 0.6
wmc 12
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A initPool() 0 6 2
D getCache() 0 27 9
1
<?php
2
/**
3
 * @author    jan huang <[email protected]>
4
 * @copyright 2016
5
 *
6
 * @see      https://www.github.com/janhuang
7
 * @see      https://fastdlabs.com
8
 */
9
10
namespace FastD\Pool;
11
12
use Symfony\Component\Cache\Adapter\AbstractAdapter;
13
use Symfony\Component\Cache\Adapter\RedisAdapter;
14
15
/**
16
 * Class CachePool.
17
 */
18
class CachePool implements PoolInterface
19
{
20
    /**
21
     * @var AbstractAdapter[]
22
     */
23
    protected $caches = [];
24
25
    /**
26
     * @var array
27
     */
28
    protected $config;
29
30
    /**
31
     * Cache constructor.
32
     *
33
     * @param array $config
34
     */
35 32
    public function __construct(array $config)
36
    {
37 32
        $this->config = $config;
38 32
    }
39
40
    /**
41
     * @param $key
42
     *
43
     * @return AbstractAdapter
44
     */
45 9
    public function getCache($key)
46
    {
47 9
        if (!isset($this->caches[$key])) {
48 9
            if (!isset($this->config[$key])) {
49
                throw new \LogicException(sprintf('No set %s cache', $key));
50
            }
51 9
            $config = $this->config[$key];
52 9
            switch ($config['adapter']) {
53 9
                case RedisAdapter::class:
54
                    $this->caches[$key] = new RedisAdapter(
55
                        RedisAdapter::createConnection($config['params']['dsn']),
56
                        isset($config['params']['namespace']) ? $config['params']['namespace'] : '',
57
                        isset($config['params']['lifetime']) ? $config['params']['lifetime'] : ''
58
                    );
59
60
                    break;
61 9
                default:
62 9
                    $this->caches[$key] = new $config['adapter'](
63 9
                        isset($config['params']['namespace']) ? $config['params']['namespace'] : '',
64 9
                        isset($config['params']['lifetime']) ? $config['params']['lifetime'] : '',
65 9
                        isset($config['params']['directory']) ? $config['params']['directory'] : app()->getPath().'/runtime/cache'
66 9
                    );
67 9
            }
68 9
        }
69
70 9
        return $this->caches[$key];
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function initPool()
77
    {
78
        foreach ($this->config as $name => $config) {
79
            $this->getCache($name);
80
        }
81
    }
82
}
83