Completed
Push — master ( 6cec30...bddb48 )
by nicolas
11s
created

ConfigFileScriptCache::clear()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 10
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
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
    public function __construct($cacheDirectory, $debug)
22
    {
23
        $adbackCacheDir = $cacheDirectory . '/adback';
24
        $file = $adbackCacheDir . '/apiCache.php';
25
        $fs = new FileSystem();
26
        if (!$fs->exists($file)) {
27
            $fs->mkdir($adbackCacheDir);
28
            $fs->touch($file);
29
        }
30
        $this->cache = new ConfigCache($file, $debug);
31
    }
32
33
    /**
34
     * @param string $key
35
     *
36
     * @return string|null
37
     */
38
    protected function get($key)
39
    {
40
        $content = json_decode(file_get_contents($this->cache->getPath()), true);
41
42
        if (array_key_exists($key, $content)) {
43
            return $content[$key];
44
        }
45
46
        return null;
47
    }
48
49
    /**
50
     * @param string $key
51
     * @param string $value
52
     */
53
    protected function set($key, $value)
54
    {
55
        $content = json_decode(file_get_contents($this->cache->getPath()), true);
56
57
        if (null === $content) {
58
            $content = [];
59
        }
60
61
        $content[$key] = $value;
62
        $this->cache->write(json_encode($content));
63
    }
64
65
    /**
66
     * @param string $key
67
     */
68
    protected function clear($key)
69
    {
70
        $content = json_decode(file_get_contents($this->cache->getPath()), true);
71
72
        if (array_key_exists($key, $content)) {
73
            unset($content[$key]);
74
        }
75
76
        $this->cache->write(json_encode($content));
77
    }
78
}
79