Completed
Branch feature/pre-split (60f5c0)
by Anton
03:19
created

CacheConfig::hasStore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Cache\Configs;
10
11
use Spiral\Cache\Exceptions\ConfigException;
12
use Spiral\Core\InjectableConfig;
13
use Spiral\Core\Traits\Config\AliasTrait;
14
15
/**
16
 * Cache component configuration manager.
17
 */
18
class CacheConfig extends InjectableConfig
19
{
20
    use AliasTrait;
21
22
    /**
23
     * Configuration section.
24
     */
25
    const CONFIG = 'cache';
26
27
    /**
28
     * @var array
29
     */
30
    protected $config = [
31
        'store'  => '',
32
        'stores' => [],
33
    ];
34
35
    /**
36
     * @return string
37
     */
38
    public function defaultStore(): string
39
    {
40
        return $this->config['store'];
41
    }
42
43
    /**
44
     * @param string $store
45
     *
46
     * @return bool
47
     */
48
    public function hasStore(string $store): bool
49
    {
50
        return isset($this->config['stores'][$store]);
51
    }
52
53
    /**
54
     * @param string $store
55
     *
56
     * @return string
57
     */
58
    public function storeClass(string $store): string
59
    {
60
        if (class_exists($store)) {
61
            //Legacy format support
62
            return $store;
63
        }
64
65
        return $this->config['stores'][$store]['class'];
66
    }
67
68
    /**
69
     * @param string $store
70
     *
71
     * @return array
72
     */
73
    public function storeOptions(string $store): array
74
    {
75
        if (isset($this->config['stores'][$store]['options'])) {
76
            return $this->config['stores'][$store]['options'];
77
78
        }
79
80
        $options = $this->config['stores'][$store];
81
        unset($options['class']);
82
83
        return $options;
84
    }
85
86
    /**
87
     * Detect store ID based on provided store class. Attention, method expects that config key
88
     * is store id/name.
89
     *
90
     * @param \ReflectionClass $class
91
     *
92
     * @return string
93
     *
94
     * @throws ConfigException
95
     */
96
    public function resolveStore(\ReflectionClass $class): string
97
    {
98
        foreach ($this->config['stores'] as $store => $options) {
99
            if ($options['class'] == $class->getName()) {
100
                return $store;
101
            }
102
        }
103
104
        throw new ConfigException(
105
            "Unable to detect store options for cache store '{$class->getName()}'"
106
        );
107
    }
108
}
109