Completed
Push — master ( 4d4876...58c5bd )
by Arman
16s queued 13s
created

CacheManager::getHandler()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 17
rs 10
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.8.0
13
 */
14
15
namespace Quantum\Libraries\Cache;
16
17
use Quantum\Exceptions\CacheException;
18
use Psr\SimpleCache\CacheInterface;
19
use Quantum\Loader\Setup;
20
21
/**
22
 * Class CacheManager
23
 * @package Quantum\Libraries\Cache
24
 */
25
class CacheManager
26
{
27
28
    /**
29
     * Available cache drivers
30
     */
31
    const DRIVERS = [
32
        'file',
33
        'memcache',
34
        'redis'
35
    ];
36
37
    /**
38
     * Get Handler
39
     * @return CacheInterface
40
     * @throws \Quantum\Exceptions\CacheException
41
     */
42
    public static function getHandler()
43
    {
44
        if (!config()->has('cache')) {
45
            config()->import(new Setup('Config', 'cache'));
46
        }
47
48
        $cacheDriver = config()->get('cache.current');
49
50
        if (!in_array($cacheDriver, self::DRIVERS)) {
51
            throw CacheException::unsupportedDriver($cacheDriver);
0 ignored issues
show
Bug introduced by
It seems like $cacheDriver can also be of type null; however, parameter $driver of Quantum\Exceptions\AppEx...on::unsupportedDriver() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
            throw CacheException::unsupportedDriver(/** @scrutinizer ignore-type */ $cacheDriver);
Loading history...
52
        }
53
54
        $cacheAdapterClass = __NAMESPACE__ . '\\Adapters\\' . ucfirst($cacheDriver) . 'Cache';
0 ignored issues
show
Bug introduced by
It seems like $cacheDriver can also be of type null; however, parameter $string of ucfirst() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
        $cacheAdapterClass = __NAMESPACE__ . '\\Adapters\\' . ucfirst(/** @scrutinizer ignore-type */ $cacheDriver) . 'Cache';
Loading history...
55
56
        $cacheAdapter = new $cacheAdapterClass(config()->get('cache.' . $cacheDriver . '.params'));
57
58
        return new Cache($cacheAdapter);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new Quantum\Libra...he\Cache($cacheAdapter) returns the type Quantum\Libraries\Cache\Cache which is incompatible with the documented return type Psr\SimpleCache\CacheInterface.
Loading history...
59
    }
60
61
}
62