Completed
Push — master ( 579af5...b29473 )
by Oleg
07:53
created

Cache   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 71
rs 10
c 1
b 1
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 4
A get() 0 12 3
1
<?php /** MicroCache */
2
3
namespace Micro\Cache;
4
5
use Micro\Base\Exception;
6
use Micro\Base\IContainer;
7
8
/**
9
 * Cache class file.
10
 *
11
 * @author Oleg Lunegov <[email protected]>
12
 * @link https://github.com/lugnsk/micro
13
 * @copyright Copyright &copy; 2013 Oleg Lunegov
14
 * @license /LICENSE
15
 * @package Micro
16
 * @subpackage Cache
17
 * @version 1.0
18
 * @since 1.0
19
 */
20
class Cache
21
{
22
    /** @var array $drivers Supported drivers */
23
    protected static $drivers = [
24
        'array'     => '\\Micro\\Cache\\ArrayCache',
25
        'apc'       => '\\Micro\\Cache\\ApcCache',
26
        'file'      => '\\Micro\\Cache\\FileCache',
27
        'memcache'  => '\\Micro\\Cache\\MemcacheCache',
28
        'memcached' => '\\Micro\\Cache\\MemcacheCache',
29
        'redis'     => '\\Micro\\Cache\\RedisCache',
30
        'wincache'  => '\\Micro\\Cache\\WincacheCache',
31
        'xcache'    => '\\Micro\\Cache\\XcacheCache'
32
    ];
33
    /** @var array $servers Activated servers */
34
    protected $servers = [];
35
    /** @var IContainer $container Config container */
36
    protected $container;
37
38
39
    /**
40
     * Constructor is a initialize Caches
41
     *
42
     * @access public
43
     *
44
     * @param array $config Caching config
45
     *
46
     * @result void
47
     * @throws Exception
48
     */
49
    public function __construct(array $config = [])
50
    {
51
        $this->container = $config['container'];
52
53
        if (empty($config['servers'])) {
54
            throw new Exception('Caching not configured');
55
        }
56
57
        foreach ($config['servers'] AS $key => $server) {
58
            if (array_key_exists($server['driver'], array_keys(self::$drivers))) {
59
                $this->servers[$key] = new self::$drivers[$server['driver']] (
60
                    array_merge($server, ['container' => $this->container])
61
                );
62
            } else {
63
                throw new Exception('Cache driver `' . $server['driver'] . '` not found');
64
            }
65
        }
66
    }
67
68
    /**
69
     * Get cache server by name
70
     *
71
     * @access public
72
     *
73
     * @param string $driver server name
74
     *
75
     * @return mixed
76
     * @throws Exception
77
     */
78
    public function get($driver = null)
79
    {
80
        if (!$driver) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $driver of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
81
            return $this->servers[0];
82
        }
83
84
        if (in_array($driver, $this->servers, true)) {
85
            return $this->servers[$driver];
86
        } else {
87
            throw new Exception('Cache `' . $driver . '` not found.');
88
        }
89
    }
90
}
91