Passed
Pull Request — master (#190)
by Arman
02:47
created

CacheFactory::createInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 11
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.9.5
13
 */
14
15
namespace Quantum\Libraries\Cache\Factories;
16
17
use Quantum\Libraries\Config\Exceptions\ConfigException;
18
use Quantum\Libraries\Cache\Exceptions\CacheException;
19
use Quantum\Libraries\Cache\Adapters\MemcachedAdapter;
20
use Quantum\Libraries\Cache\Adapters\DatabaseAdapter;
21
use Quantum\Libraries\Cache\Adapters\RedisAdapter;
22
use Quantum\Libraries\Cache\Adapters\FileAdapter;
23
use Quantum\Di\Exceptions\DiException;
24
use Quantum\Exceptions\BaseException;
25
use Quantum\Libraries\Cache\Cache;
26
use Quantum\Loader\Setup;
27
use ReflectionException;
28
29
/**
30
 * Class CacheFactory
31
 * @package Quantum\Libraries\Cache
32
 */
33
class CacheFactory
34
{
35
36
    /**
37
     * Supported adapters
38
     */
39
    const ADAPTERS = [
40
        Cache::FILE => FileAdapter::class,
41
        Cache::DATABASE => DatabaseAdapter::class,
42
        Cache::MEMCACHED => MemcachedAdapter::class,
43
        Cache::REDIS => RedisAdapter::class,
44
    ];
45
46
    /**
47
     * @var Cache|null
48
     */
49
    private static $instance = null;
50
51
    /**
52
     * @return Cache
53
     * @throws BaseException
54
     * @throws ConfigException
55
     * @throws DiException
56
     * @throws ReflectionException
57
     */
58
    public static function get(): Cache
59
    {
60
        if (self::$instance === null) {
61
            return self::$instance = self::createInstance();
62
        }
63
64
        return self::$instance;
65
    }
66
67
    /**
68
     * @return Cache
69
     * @throws BaseException
70
     * @throws DiException
71
     * @throws ConfigException
72
     * @throws ReflectionException
73
     */
74
    private static function createInstance(): Cache
75
    {
76
        if (!config()->has('cache')) {
77
            config()->import(new Setup('Config', 'cache'));
78
        }
79
80
        $adapter = config()->get('cache.current');
81
82
        $adapterClass = self::getAdapterClass($adapter);
0 ignored issues
show
Bug introduced by
It seems like $adapter can also be of type null; however, parameter $adapter of Quantum\Libraries\Cache\...tory::getAdapterClass() 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

82
        $adapterClass = self::getAdapterClass(/** @scrutinizer ignore-type */ $adapter);
Loading history...
83
84
        return new Cache(new $adapterClass(config()->get('cache.' . $adapter)));
85
    }
86
87
    /**
88
     * @param string $adapter
89
     * @return string
90
     * @throws BaseException
91
     */
92
    private static function getAdapterClass(string $adapter): string
93
    {
94
        if (!array_key_exists($adapter, self::ADAPTERS)) {
95
            throw CacheException::adapterNotSupported($adapter);
96
        }
97
98
        return self::ADAPTERS[$adapter];
99
    }
100
}