Completed
Branch develop (c2aa4c)
by Anton
05:17
created

CacheManager   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
B store() 0 28 5
A createInjection() 0 19 4
A __construct() 0 5 1
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
namespace Spiral\Cache;
9
10
use Spiral\Cache\Configs\CacheConfig;
11
use Spiral\Cache\Exceptions\CacheException;
12
use Spiral\Core\Component;
13
use Spiral\Core\FactoryInterface;
14
use Spiral\Core\Container\InjectorInterface;
15
use Spiral\Core\Container\SingletonInterface;
16
use Spiral\Debug\Traits\BenchmarkTrait;
17
18
/**
19
 * Default implementation of CacheInterface. Better fit for spiral.
20
 */
21
class CacheManager extends Component implements SingletonInterface, CacheInterface, InjectorInterface
22
{
23
    /**
24
     * Some operations can be slow.
25
     */
26
    use BenchmarkTrait;
27
28
    /**
29
     * Declares to Spiral IoC that component instance should be treated as singleton.
30
     */
31
    const SINGLETON = self::class;
32
33
    /**
34
     * Already constructed cache adapters.
35
     *
36
     * @var CacheStore[]
37
     */
38
    private $stores = false;
39
40
    /**
41
     * @var CacheConfig
42
     */
43
    protected $config = null;
44
45
    /**
46
     * @invisible
47
     * @var FactoryInterface
48
     */
49
    protected $factory = null;
50
51
    /**
52
     * @param CacheConfig      $config
53
     * @param FactoryInterface $factory
54
     */
55
    public function __construct(CacheConfig $config, FactoryInterface $factory)
56
    {
57
        $this->config = $config;
58
        $this->factory = $factory;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function store($class = null)
65
    {
66
        //Default store class
67
        $class = $class ?: $this->config['store'];
68
69
        if (isset($this->stores[$class])) {
70
            return $this->stores[$class];
71
        }
72
73
        $benchmark = $this->benchmark('store', $class);
74
        try {
75
            //Constructing cache instance
76
            $this->stores[$class] = $this->factory->make(
77
                $class,
78
                $this->config->storeOptions($class)
79
            );
80
        } finally {
81
            $this->benchmark($benchmark);
82
        }
83
84
        if ($class == $this->config['store'] && !$this->stores[$class]->isAvailable()) {
85
            throw new CacheException(
86
                "Unable to use default store '{$class}', driver is unavailable."
87
            );
88
        }
89
90
        return $this->stores[$class];
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     *
96
     * @throws CacheException
97
     */
98
    public function createInjection(\ReflectionClass $class, $context = null)
99
    {
100
        if (isset($this->stores[$class->getName()])) {
101
            return $this->stores[$class->getName()];
102
        }
103
104
        if (!$class->isInstantiable()) {
105
            //Default store
106
            return $this->store();
107
        }
108
109
        if (!$this->config->hasStore($class->getName())) {
110
            throw new CacheException(
111
                "Unable construct cache store '{$class}', no options found."
112
            );
113
        }
114
115
        return $this->store($class->getName());
116
    }
117
}
118