ConfigFileScriptCache   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 96.3%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 66
ccs 26
cts 27
cp 0.963
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 2
A get() 0 10 2
A set() 0 11 2
A clear() 0 10 2
1
<?php
2
3
namespace Dekalee\AdbackAnalyticsBundle\Driver;
4
5
use Dekalee\AdbackAnalytics\Driver\ScriptCacheInterface;
6
use Dekalee\AdbackAnalytics\Driver\SqlScriptCache;
7
use Symfony\Component\Config\ConfigCache;
8
use Symfony\Component\Filesystem\Filesystem;
9
10
/**
11
 * Class ConfigFileScriptCache
12
 */
13
class ConfigFileScriptCache extends SqlScriptCache implements ScriptCacheInterface
14
{
15
    protected $cache;
16
17
    /**
18
     * @param $cacheDirectory
19
     * @param $debug
20
     */
21 2
    public function __construct($cacheDirectory, $debug)
22
    {
23 2
        $adbackCacheDir = $cacheDirectory . '/adback';
24 2
        $file = $adbackCacheDir . '/apiCache.php';
25 2
        $fs = new FileSystem();
26 2
        if (!$fs->exists($file)) {
27 2
            $fs->mkdir($adbackCacheDir);
28 2
            $fs->touch($file);
29
        }
30 2
        $this->cache = new ConfigCache($file, $debug);
31 2
    }
32
33
    /**
34
     * @param string $key
35
     *
36
     * @return string|null
37
     */
38 1
    protected function get($key)
39
    {
40 1
        $content = json_decode(file_get_contents($this->cache->getPath()), true);
41
42 1
        if (array_key_exists($key, $content)) {
43 1
            return $content[$key];
44
        }
45
46
        return null;
47
    }
48
49
    /**
50
     * @param string $key
51
     * @param string $value
52
     */
53 1
    protected function set($key, $value)
54
    {
55 1
        $content = json_decode(file_get_contents($this->cache->getPath()), true);
56
57 1
        if (null === $content) {
58 1
            $content = [];
59
        }
60
61 1
        $content[$key] = $value;
62 1
        $this->cache->write(json_encode($content));
63 1
    }
64
65
    /**
66
     * @param string $key
67
     */
68 1
    protected function clear($key)
69
    {
70 1
        $content = json_decode(file_get_contents($this->cache->getPath()), true);
71
72 1
        if (array_key_exists($key, $content)) {
73 1
            unset($content[$key]);
74
        }
75
76 1
        $this->cache->write(json_encode($content));
77 1
    }
78
}
79