Passed
Push — master ( 67f09f...a27d00 )
by Jens
02:12
created

Cache::clearCacheForPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 16
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by jensk on 25-10-2017.
4
 */
5
6
namespace CloudControl\Cms\storage;
7
8
9
use CloudControl\Cms\util\GlobalFunctions;
10
11
class Cache
12
{
13
    /**
14
     * @var Cache
15
     */
16
    private static $instance;
17
18
    /**
19
     * @var string
20
     */
21
    protected $storagePath;
22
    /**
23
     * @var \PDO
24
     */
25
    protected $dbHandle;
26
27
    private function __construct()
28
    {
29
        // Singleton, so private constructor
30
    }
31
32
    /**
33
     * @return Cache
34
     */
35
    public static function getInstance()
36
    {
37
        if (!self::$instance instanceof Cache) {
0 ignored issues
show
introduced by
self::instance is always a sub-type of CloudControl\Cms\storage\Cache.
Loading history...
38
            self::$instance = new Cache();
39
        }
40
        return self::$instance;
41
    }
42
43
    /**
44
     * @param $path
45
     * @return \stdClass
46
     * @throws \RuntimeException
47
     */
48
    public function getCacheForPath($path)
49
    {
50
        $dbInstace = $this->getDbInstance();
51
        $sql = '
52
            SELECT *
53
              FROM `cache`
54
             WHERE `path` = :path
55
             LIMIT 1
56
        ';
57
        $stmt = $dbInstace->prepare($sql);
58
        $stmt->bindParam(':path', $path);
59
        if ($stmt->execute()) {
60
            return $stmt->fetch(\PDO::FETCH_OBJ);
61
        } else {
62
            $error = $stmt->errorInfo();
63
            $errorMsg = $error[2];
64
            throw new \RuntimeException('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>');
65
        }
66
    }
67
68
    /**
69
     * Clears all cache
70
     */
71
    public function clearCache()
72
    {
73
        $dbInstace = $this->getDbInstance();
74
        $sql = '
75
            DELETE FROM `cache`;
76
            VACUUM;
77
        ';
78
        $stmt = $dbInstace->prepare($sql);
79
        if ($stmt->execute()) {
80
            return;
81
        } else {
82
            $error = $stmt->errorInfo();
83
            $errorMsg = $error[2];
84
            throw new \RuntimeException('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>');
85
        }
86
    }
87
88
    /**
89
     * Clears the cache for path
90
     *
91
     * @param $path string
92
     * @return mixed
93
     */
94
    public function clearCacheForPath($path)
95
    {
96
        $dbInstace = $this->getDbInstance();
97
        $sql = '
98
       DELETE FROM `cache`
99
             WHERE `path` = :path
100
             LIMIT 1
101
        ';
102
        $stmt = $dbInstace->prepare($sql);
103
        $stmt->bindParam(':path', $path);
104
        if ($stmt->execute()) {
105
            return $stmt->fetch(\PDO::FETCH_OBJ);
106
        } else {
107
            $error = $stmt->errorInfo();
108
            $errorMsg = $error[2];
109
            throw new \RuntimeException('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>');
110
        }
111
    }
112
113
    /**
114
     * @param $storagePath
115
     */
116
    public function setStoragePath($storagePath)
117
    {
118
        $this->storagePath = $storagePath;
119
    }
120
121
    /**
122
     * @return \PDO
123
     */
124
    private function getDbInstance()
125
    {
126
        if ($this->dbHandle === null) {
127
            $this->dbHandle = new \PDO('sqlite:' . $this->storagePath . DIRECTORY_SEPARATOR . 'cache.db');
128
        }
129
        return $this->dbHandle;
130
    }
131
132
    /**
133
     * @param $baseCacheSqlPath
134
     */
135
    public function init($baseCacheSqlPath)
136
    {
137
        $realBaseCacheSqlPath = realpath($baseCacheSqlPath);
138
139
        $db = $this->getDbInstance();
140
        $sql = file_get_contents($realBaseCacheSqlPath);
141
        $db->exec($sql);
142
    }
143
144
    /**
145
     * @param string $requestUri
146
     * @param string $renderedContent
147
     * @param string $headers
148
     * @throws \RuntimeException
149
     */
150
    public function setCacheForPath($requestUri, $renderedContent, $headers)
151
    {
152
        $dbInstace = $this->getDbInstance();
153
        $sql = '
154
            INSERT OR REPLACE INTO `cache` (path, creationStamp, headers, contents)
155
                 VALUES (:path, :creationStamp, :headers, :contents);
156
        ';
157
        $contents = GlobalFunctions::sanitizeOutput($renderedContent);
158
        $creationStamp = time();
159
        $stmt = $dbInstace->prepare($sql);
160
        $stmt->bindParam(':path', $requestUri);
161
        $stmt->bindParam(':creationStamp', $creationStamp);
162
        $stmt->bindParam(':contents', $contents);
163
        $stmt->bindParam(':headers', $headers);
164
        if ($stmt->execute()) {
165
            return;
166
        } else {
167
            $error = $stmt->errorInfo();
168
            $errorMsg = $error[2];
169
            throw new \RuntimeException('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>');
170
        }
171
    }
172
173
}