Passed
Push — develop ( 3d11b3...8182c7 )
by Jens
05:04 queued 46s
created

Cache::clearCache()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 7
Ratio 43.75 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 2
eloc 10
c 1
b 1
f 1
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
use CloudControl\Cms\components\CmsComponent;
10
11
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...
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) {
38
            self::$instance = new Cache();
39
        }
40
        return self::$instance;
41
    }
42
43
    /**
44
     * @param $path
45
     * @return \stdClass
46
     */
47
    public function getCacheForPath($path)
48
    {
49
        $dbInstace = $this->getDbInstance();
50
        $sql = '
51
            SELECT *
52
              FROM `cache`
53
             WHERE `path` = :path
54
             LIMIT 1
55
        ';
56
        $stmt = $dbInstace->prepare($sql);
57
        $stmt->bindParam(':path', $path);
58 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...
59
            return $stmt->fetch(\PDO::FETCH_OBJ);
60
        } else {
61
            $error = $stmt->errorInfo();
62
            $errorMsg = $error[2];
63
            throw new \RuntimeException('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>');
64
        }
65
    }
66
67
    /**
68
     * Clears all cache
69
     */
70
    public function clearCache()
71
    {
72
        $dbInstace = $this->getDbInstance();
73
        $sql = '
74
            DELETE FROM `cache`;
75
            VACUUM;
76
        ';
77
        $stmt = $dbInstace->prepare($sql);
78 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...
79
            return;
80
        } else {
81
            $error = $stmt->errorInfo();
82
            $errorMsg = $error[2];
83
            throw new \RuntimeException('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>');
84
        }
85
    }
86
87
    /**
88
     * @param $storagePath
89
     */
90
    public function setStoragePath($storagePath)
91
    {
92
        $this->storagePath = $storagePath;
93
    }
94
95
    /**
96
     * @return \PDO
97
     */
98 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...
99
    {
100
        if ($this->dbHandle === null) {
101
            $this->dbHandle = new \PDO('sqlite:' . $this->storagePath . DIRECTORY_SEPARATOR . 'cache.db');
102
        }
103
        return $this->dbHandle;
104
    }
105
106
    /**
107
     * @param $baseCacheSqlPath
108
     */
109
    public function init($baseCacheSqlPath)
110
    {
111
        $realBaseCacheSqlPath = realpath($baseCacheSqlPath);
112
113
        $db = $this->getDbInstance();
114
        $sql = file_get_contents($realBaseCacheSqlPath);
115
        $db->exec($sql);
116
    }
117
118
    /**
119
     * @param $requestUri
120
     * @param $renderedContent
121
     */
122
    public function setCacheForPath($requestUri, $renderedContent)
123
    {
124
        // Dont cache if youre logged in, or you might cache the frontend editor buttons
125
        if (CmsComponent::isCmsLoggedIn()) {
126
            return;
127
        }
128
        $dbInstace = $this->getDbInstance();
129
        $sql = '
130
            INSERT OR REPLACE INTO `cache` (path, creationStamp, contents)
131
                 VALUES (:path, :creationStamp, :contents);
132
        ';
133
        $contents = \sanitize_output($renderedContent);
134
        $creationStamp = time();
135
        $stmt = $dbInstace->prepare($sql);
136
        $stmt->bindParam(':path', $requestUri);
137
        $stmt->bindParam(':creationStamp', $creationStamp);
138
        $stmt->bindParam(':contents', $contents);
139 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...
140
            return;
141
        } else {
142
            $error = $stmt->errorInfo();
143
            $errorMsg = $error[2];
144
            throw new \RuntimeException('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>');
145
        }
146
    }
147
148
}