Passed
Push — develop ( adb2d3...61a548 )
by Jens
04:06
created

Cache::clearCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 7
Ratio 43.75 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
c 0
b 0
f 0
nc 2
nop 0
dl 7
loc 16
rs 9.4285
1
<?php
2
/**
3
 * Created by jensk on 25-10-2017.
4
 */
5
6
namespace CloudControl\Cms\storage;
7
8
9
class Cache
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
10
{
11
    /**
12
     * @var Cache
13
     */
14
    private static $instance;
15
16
    /**
17
     * @var string
18
     */
19
    protected $storagePath;
20
    /**
21
     * @var \PDO
22
     */
23
    protected $dbHandle;
24
25
    private function __construct()
26
    {
27
        // Singleton, so private constructor
28
    }
29
30
    /**
31
     * @return Cache
32
     */
33
    public static function getInstance()
34
    {
35
        if (!self::$instance instanceof Cache) {
36
            self::$instance = new Cache();
37
        }
38
        return self::$instance;
39
    }
40
41
    /**
42
     * @param $path
43
     * @return \stdClass
44
     */
45
    public function getCacheForPath($path)
46
    {
47
        $dbInstace = $this->getDbInstance();
48
        $sql = '
49
            SELECT *
50
              FROM `cache`
51
             WHERE `path` = :path
52
             LIMIT 1
53
        ';
54
        $stmt = $dbInstace->prepare($sql);
55
        $stmt->bindParam(':path', $path);
56 View Code Duplication
        if ($stmt->execute()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
            return $stmt->fetch(\PDO::FETCH_OBJ);
58
        } else {
59
            $error = $stmt->errorInfo();
60
            $errorMsg = $error[2];
61
            throw new \RuntimeException('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>');
62
        }
63
    }
64
65
    /**
66
     * Clears all cache
67
     */
68
    public function clearCache()
69
    {
70
        $dbInstace = $this->getDbInstance();
71
        $sql = '
72
            DELETE FROM `cache`;
73
            VACUUM;
74
        ';
75
        $stmt = $dbInstace->prepare($sql);
76 View Code Duplication
        if ($stmt->execute()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
            return;
78
        } else {
79
            $error = $stmt->errorInfo();
80
            $errorMsg = $error[2];
81
            throw new \RuntimeException('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>');
82
        }
83
    }
84
85
    /**
86
     * @param $storagePath
87
     */
88
    public function setStoragePath($storagePath)
89
    {
90
        $this->storagePath = $storagePath;
91
    }
92
93
    /**
94
     * @return \PDO
95
     */
96 View Code Duplication
    private function getDbInstance()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
        if ($this->dbHandle === null) {
99
            $this->dbHandle = new \PDO('sqlite:' . $this->storagePath . DIRECTORY_SEPARATOR . 'cache.db');
100
        }
101
        return $this->dbHandle;
102
    }
103
104
    /**
105
     * @param $baseCacheSqlPath
106
     */
107
    public function init($baseCacheSqlPath)
108
    {
109
        $realBaseCacheSqlPath = realpath($baseCacheSqlPath);
110
111
        $db = $this->getDbInstance();
112
        $sql = file_get_contents($realBaseCacheSqlPath);
113
        $db->exec($sql);
114
    }
115
116
    /**
117
     * @param $requestUri
118
     * @param $renderedContent
119
     */
120
    public function setCacheForPath($requestUri, $renderedContent)
121
    {
122
        $dbInstace = $this->getDbInstance();
123
        $sql = '
124
            INSERT OR REPLACE INTO `cache` (path, creationStamp, contents)
125
                 VALUES (:path, :creationStamp, :contents);
126
        ';
127
        $contents = \sanitize_output($renderedContent);
128
        $creationStamp = time();
129
        $stmt = $dbInstace->prepare($sql);
130
        $stmt->bindParam(':path', $requestUri);
131
        $stmt->bindParam(':creationStamp', $creationStamp);
132
        $stmt->bindParam(':contents', $contents);
133 View Code Duplication
        if ($stmt->execute()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
134
            return;
135
        } else {
136
            $error = $stmt->errorInfo();
137
            $errorMsg = $error[2];
138
            throw new \RuntimeException('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>');
139
        }
140
    }
141
142
}