IntegrationCache::create()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 0
cts 18
cp 0
rs 9.552
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 20
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-integration/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-integration/
7
 */
8
9
namespace flipbox\craft\integration\services;
10
11
use Craft;
12
use craft\helpers\ArrayHelper;
13
use craft\helpers\Component as ComponentHelper;
14
use flipbox\craft\psr16\SimpleCacheAdapter;
15
use Flipbox\Skeleton\Exceptions\InvalidConfigurationException;
16
use Flipbox\Skeleton\Helpers\ObjectHelper;
17
use Psr\SimpleCache\CacheInterface;
18
use yii\base\Component;
19
use yii\caching\DummyCache;
20
21
/**
22
 * @author Flipbox Factory <[email protected]>
23
 * @since 2.2.0
24
 */
25
abstract class IntegrationCache extends Component
26
{
27
    /**
28
     * The dummy cache handle
29
     */
30
    const DUMMY_CACHE = 'dummy';
31
32
    /**
33
     * The app cache handle
34
     */
35
    const APP_CACHE = 'app';
36
37
    /**
38
     * The default cache identifier
39
     */
40
    const DEFAULT_CACHE = 'DEFAULT';
41
42
    /**
43
     * The override file
44
     */
45
    public $overrideFile;
46
47
    /**
48
     *  Cache overrides
49
     *
50
     * @var array|null
51
     */
52
    private $overrides;
53
54
    /**
55
     * @var CacheInterface[]
56
     */
57
    private $cache = [];
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public function init()
63
    {
64
        parent::init();
65
66
        /** @noinspection PhpUnhandledExceptionInspection */
67
        $this->cache[self::APP_CACHE] = Craft::createObject([
68
            'class' => SimpleCacheAdapter::class,
69
            'cache' => Craft::$app->getCache()
70
        ]);
71
72
        /** @noinspection PhpUnhandledExceptionInspection */
73
        $this->cache[self::DUMMY_CACHE] = Craft::createObject([
74
            'class' => SimpleCacheAdapter::class,
75
            'cache' => [
76
                'class' => DummyCache::class
77
            ]
78
        ]);
79
    }
80
81
    /**
82
     * Load override cache configurations
83
     */
84
    protected function loadOverrides()
85
    {
86
        if ($this->overrides === null) {
87
            $this->overrides = [];
88
89
            if ($this->overrideFile !== null) {
90
                $this->overrides = Craft::$app->getConfig()->getConfigFromFile($this->overrideFile);
91
            }
92
        }
93
    }
94
95
    /**
96
     * Returns any configurations from the config file.
97
     *
98
     * @param string $handle
99
     * @return array|null
100
     */
101
    public function getOverrides(string $handle)
102
    {
103
        $this->loadOverrides();
104
        return $this->overrides[$handle] ?? null;
105
    }
106
107
    /**
108
     * @return string
109
     */
110
    protected function getDefaultCache(): string
111
    {
112
        return static::DUMMY_CACHE;
113
    }
114
115
    /**
116
     * @param string $handle
117
     * @return CacheInterface|null
118
     */
119
    protected function handleCacheNotFound(string $handle)
0 ignored issues
show
Unused Code introduced by
The parameter $handle is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
120
    {
121
        return $this->find(static::DUMMY_CACHE);
122
    }
123
124
    /**
125
     * @param string $handle
126
     * @return CacheInterface|null
127
     */
128
    public function find(string $handle = self::DEFAULT_CACHE)
129
    {
130
        if ($handle === self::DEFAULT_CACHE) {
131
            $handle = $this->getDefaultCache();
132
        }
133
134
        if (!array_key_exists($handle, $this->cache)) {
135
            $this->cache[$handle] = $this->create(['handle' => $handle]);
136
        }
137
138
        return $this->cache[$handle];
139
    }
140
141
    /**
142
     * @param string $handle
143
     * @return CacheInterface
144
     */
145
    public function get(string $handle = self::DEFAULT_CACHE): CacheInterface
146
    {
147
        if (null === ($cache = $this->find($handle))) {
148
            return $this->handleCacheNotFound($handle);
149
        }
150
151
        return $cache;
152
    }
153
154
    /**
155
     * @param $config
156
     * @return CacheInterface|null
157
     */
158
    protected function create(array $config)
159
    {
160
        // Merge settings
161
        $config = ComponentHelper::mergeSettings($config);
162
163
        // Apply overrides
164
        if (null !== ($handle = ArrayHelper::remove($config, 'handle')) &&
165
            null !== ($override = $this->getOverrides($handle))
166
        ) {
167
            $config = array_merge($config, $override);
168
        }
169
170
        try {
171
            $cache = ObjectHelper::create(
172
                $config,
173
                CacheInterface::class
174
            );
175
        } catch (InvalidConfigurationException $e) {
176
            return null;
177
        }
178
179
        return $cache;
180
    }
181
182
    /**
183
     * @return array
184
     */
185
    public function all(): array
186
    {
187
        // Ensure configs are loaded
188
        $this->loadOverrides();
189
190
        // The handles defined in the config
191
        $handles = array_keys($this->overrides);
192
193
        // Any configured that aren't in the db
194
        foreach ($handles as $handle) {
195
            $this->cache[$handle] = $this->create(['handle' => $handle]);
196
        }
197
198
        return $this->cache;
199
    }
200
}
201