Passed
Pull Request — master (#88)
by Arman
03:00
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 Psr\SimpleCache\CacheInterface;
18
use Quantum\Loader\Setup;
19
20
/**
21
 * Class AuthManager
22
 * @package Quantum\Libraries\Cache
23
 */
24
class CacheManager
25
{
26
27
    const DRIVERS = [
28
        'file',
29
        'memcache',
30
        'redis'
31
    ];
32
33
    /**
34
     *  Get Handler
35
     * @return CacheInterface
36
     * @throws Exception
37
     */
38
    public static function getHandler()
39
    {
40
        if (!config()->has('cache')) {
41
            config()->import(new Setup('Config', 'cache'));
42
        }
43
44
        $cacheDriver = config()->get('cache.current');
45
46
        if (!in_array($cacheDriver, self::DRIVERS)) {
47
            throw new \Exception();
48
        }
49
50
        $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

50
        $cacheAdapterClass = __NAMESPACE__ . '\\Adapters\\' . ucfirst(/** @scrutinizer ignore-type */ $cacheDriver) . 'Cache';
Loading history...
51
52
        $cacheAdapter = new $cacheAdapterClass(config()->get('cache.' . $cacheDriver . '.params'));
53
        
54
        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...
55
    }
56
57
}
58