Passed
Push — develop ( ad1a9e...8ea77e )
by Jens
02:45
created

Cache::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
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
    public static function getInstance()
31
    {
32
        if (!self::$instance instanceof Cache) {
33
            self::$instance = new Cache();
34
        }
35
        return self::$instance;
36
    }
37
38
    public function getCacheForPath($path)
39
    {
40
        $dbInstace = $this->getDbInstance();
41
        $sql = '
42
            SELECT *
43
              FROM `cache`
44
             WHERE `path` = :path
45
             LIMIT 1
46
        ';
47
        $stmt = $dbInstace->prepare($sql);
48
        $stmt->bindParam(':path', $path);
49 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...
50
            return $stmt->fetch(\PDO::FETCH_OBJ);
51
        } else {
52
            $error = $stmt->errorInfo();
53
            $errorMsg = $error[2];
54
            throw new \RuntimeException('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>');
55
        }
56
    }
57
58
    public function setStoragePath($storagePath)
59
    {
60
        $this->storagePath = $storagePath;
61
    }
62
63 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...
64
    {
65
        if ($this->dbHandle === null) {
66
            $this->dbHandle = new \PDO('sqlite:' . $this->storagePath . DIRECTORY_SEPARATOR . 'cache.db');
67
        }
68
        return $this->dbHandle;
69
    }
70
71
    public function init($baseCacheSqlPath)
72
    {
73
        $realBaseCacheSqlPath = realpath($baseCacheSqlPath);
74
75
        $db = $this->getDbInstance();
76
        $sql = file_get_contents($realBaseCacheSqlPath);
77
        $db->exec($sql);
78
    }
79
80
    public function setCacheForPath($requestUri, $renderedContent)
81
    {
82
        $dbInstace = $this->getDbInstance();
83
        $sql = '
84
            INSERT OR REPLACE INTO `cache` (path, creationStamp, contents)
85
                 VALUES (:path, :creationStamp, :contents);
86
        ';
87
        $contents = \sanitize_output($renderedContent);
88
        $creationStamp = time();
89
        $stmt = $dbInstace->prepare($sql);
90
        $stmt->bindParam(':path', $requestUri);
91
        $stmt->bindParam(':creationStamp', $creationStamp);
92
        $stmt->bindParam(':contents', $contents);
93 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...
94
            return;
95
        } else {
96
            $error = $stmt->errorInfo();
97
            $errorMsg = $error[2];
98
            throw new \RuntimeException('SQLite Exception: ' . $errorMsg . ' in SQL: <br /><pre>' . $sql . '</pre>');
99
        }
100
    }
101
102
}