Cache   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 53
ccs 0
cts 18
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fetch() 0 6 1
A save() 0 6 1
A configSplit() 0 10 2
1
<?php
2
declare(strict_types=1);
3
4
namespace WyriHaximus\TwigView\Lib;
5
6
use Asm89\Twig\CacheExtension\CacheProviderInterface;
7
use Cake\Cache\Cache as CakeCache;
8
9
final class Cache implements CacheProviderInterface
10
{
11
    public const CACHE_PREFIX = 'twig-view-in-template-item-';
12
13
    /**
14
     * Retrieve data from the cache.
15
     *
16
     * @param string $identifier Identifier for this bit of data to read.
17
     *
18
     * @return mixed The cached data, or false if the data doesn't exist, has expired, or on error while fetching.
19
     */
20
    public function fetch($identifier)
21
    {
22
        [$config, $key] = $this->configSplit($identifier);
0 ignored issues
show
Bug introduced by
The variable $config does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $key does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
23
24
        return CakeCache::read(static::CACHE_PREFIX . $key, $config);
25
    }
26
27
    /**
28
     * Save data to the cache.
29
     *
30
     * @param string $identifier Identifier for this bit of data to write.
31
     * @param string $data       Data to cache.
32
     * @param int    $lifeTime   Time to life inside the cache.
33
     *
34
     * @return bool
35
     */
36
    public function save($identifier, $data, $lifeTime = 0): bool
37
    {
38
        [$config, $key] = $this->configSplit($identifier);
0 ignored issues
show
Bug introduced by
The variable $config does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $key does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
39
40
        return CakeCache::write(static::CACHE_PREFIX . $key, $data, $config);
41
    }
42
43
    /**
44
     * Extract $configName and $key from $name and $config.
45
     *
46
     * @param string $name   Name.
47
     * @param string $config Cache configuration name to used.
48
     *
49
     * @return array
50
     */
51
    protected function configSplit($name, $config = 'default'): array
52
    {
53
        if (strpos($name, ':') !== false) {
54
            $parts = explode(':', $name, 2);
55
56
            return $parts;
57
        }
58
59
        return [$config, $name];
60
    }
61
}
62