Completed
Branch V3 (ec4077)
by PastisD
11:29 queued 09:28
created

Phpfastcache::get()   D

Complexity

Conditions 9
Paths 20

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
rs 4.909
c 0
b 0
f 0
cc 9
eloc 23
nc 20
nop 1
1
<?php
2
/**
3
 *
4
 * This file is part of phpFastCache.
5
 *
6
 * @license MIT License (MIT)
7
 *
8
 * For full copyright and license information, please see the docs/CREDITS.txt file.
9
 *
10
 * @author Georges.L (Geolim4)  <[email protected]>
11
 * @author PastisD https://github.com/PastisD
12
 * @author Khoa Bui (khoaofgod)  <[email protected]> http://www.phpfastcache.com
13
 *
14
 */
15
16
namespace Phpfastcache\Bundle\Service;
17
18
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface;
19
use Phpfastcache\CacheManager;
20
use Phpfastcache\Exceptions\PhpfastcacheDriverException;
21
use Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException;
22
use Symfony\Component\Stopwatch\Stopwatch;
23
24
/**
25
 * Class Cache
26
 * @package Phpfastcache\Bundle\Service
27
 */
28
class Phpfastcache
29
{
30
    /**
31
     * @var array
32
     */
33
    private $config = [];
34
35
    /**
36
     * @var Stopwatch
37
     */
38
    protected $stopwatch;
39
40
    /**
41
     * Contains all cache instances
42
     *
43
     * @var \Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface[]
44
     */
45
    private $cacheInstances = [];
46
47
    /**
48
     * Cache constructor.
49
     *
50
     * @param array $config
51
     * @param Stopwatch $stopwatch
52
     *
53
     * @throws \Phpfastcache\Exceptions\phpFastCacheDriverException
54
     */
55
    public function __construct($config, Stopwatch $stopwatch = null)
56
    {
57
        $this->config = (array) $config;
58
        $this->stopwatch = $stopwatch;
59
    }
60
61
    /**
62
     * Set a new cache instance
63
     *
64
     * @param string $name
65
     * @param ExtendedCacheItemPoolInterface $instance
66
     *
67
     * @throws \Phpfastcache\Exceptions\phpFastCacheDriverException
68
     */
69
    public function createInstance($name, ExtendedCacheItemPoolInterface $instance)
70
    {
71
        if (\array_key_exists($name, $this->cacheInstances) && $this->cacheInstances[ $name ] instanceof ExtendedCacheItemPoolInterface) {
72
            throw new PhpfastcacheDriverException("Cache instance '{$name}' already exists");
73
        }
74
        $this->cacheInstances[ $name ] = $instance;
75
    }
76
77
    /**
78
     * get a cache instance
79
     *
80
     * @param string $name Name of configured driver
81
     *
82
     * @return ExtendedCacheItemPoolInterface
83
     *
84
     * @throws \Phpfastcache\Exceptions\phpFastCacheDriverException
85
     * @throws \Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException
86
     */
87
    public function get($name): ExtendedCacheItemPoolInterface
88
    {
89
        if ($this->stopwatch) {
90
            $this->stopwatch->start(__METHOD__ . "('{$name}')");
91
        }
92
93
        if (!\array_key_exists($name, $this->cacheInstances)) {
94
            if (\array_key_exists($name, $this->config[ 'drivers' ])) {
95
                $driverClass = CacheManager::getDriverClass($this->config[ 'drivers' ][ $name ][ 'type' ]);
96
                if (\is_a($driverClass, ExtendedCacheItemPoolInterface::class, true)){
97
                    $configClass = $driverClass::getConfigClass();
98
                    if(\class_exists($configClass)){
99
                        $this->createInstance(
100
                          $name,
101
                          CacheManager::getInstance(
102
                            $this->config[ 'drivers' ][ $name ][ 'type' ],
103
                            new $configClass($this->config[ 'drivers' ][ $name ][ 'parameters' ])
104
                          )
105
                        );
106
                    }else{
107
                        throw new PhpfastcacheInvalidConfigurationException('Invalid configuration class name: ' . $configClass);
108
                    }
109
                }
110
111
                if (!isset($this->cacheInstances[ $name ]) || !($this->cacheInstances[ $name ] instanceof ExtendedCacheItemPoolInterface)) {
112
                    throw new PhpfastcacheDriverException("Cache instance '{$name}' does not implements ExtendedCacheItemPoolInterface");
113
                }
114
            } else {
115
                throw new PhpfastcacheDriverException("Cache instance '{$name}' not exists, check your config.yml");
116
            }
117
        }
118
119
        if ($this->stopwatch) {
120
            $this->stopwatch->stop(__METHOD__ . "('{$name}')");
121
        }
122
        return $this->cacheInstances[ $name ];
123
    }
124
125
    /**
126
     * @return \Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface
127
     */
128
    public function getTwigCacheInstance(): ExtendedCacheItemPoolInterface
129
    {
130
        return $this->get($this->config[ 'twig_driver' ]);
131
    }
132
133
    /**
134
     * Return all cache instances
135
     *
136
     * @return array
137
     */
138
    public function getConfig(): array
139
    {
140
        return $this->config;
141
    }
142
143
    /**
144
     * Return all cache instances
145
     *
146
     * @return \Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface[]
147
     */
148
    public function getInstances(): array
149
    {
150
        return $this->cacheInstances;
151
    }
152
}