Passed
Push — main ( a54930...73027a )
by Sílvio
13:04
created

CacheDatabaseRepository::getCurrentDateTime()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 1
c 1
b 1
f 0
nc 2
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Silviooosilva\CacheerPhp\Repositories;
4
5
use PDO;
6
use Silviooosilva\CacheerPhp\Core\Connect;
7
8
class CacheDatabaseRepository
9
{
10
    /**
11
     * Summary of connection
12
     */
13
    private $connection = null;
14
15
    public function __construct()
16
    {
17
        $this->connection = Connect::getInstance();
18
    }
19
20
21
    /**
22
     * @param string $cacheKey
23
     * @param mixed $cacheData
24
     * @param string $namespace
25
     * @param int | string $ttl
26
     * @return bool
27
     */
28
    public function store(string $cacheKey, mixed $cacheData, string $namespace, int | string $ttl = 3600)
29
    {
30
        $expirationTime = date('Y-m-d H:i:s', time() + $ttl);
31
        $createdAt = date('Y-m-d H:i:s');
32
33
        $stmt = $this->connection->prepare(
0 ignored issues
show
Bug introduced by
The method prepare() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
        /** @scrutinizer ignore-call */ 
34
        $stmt = $this->connection->prepare(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
34
                "INSERT INTO cacheer_table (cacheKey, cacheData, cacheNamespace, expirationTime, created_at) VALUES (?, ?, ?, ?, ?)"
35
            );
36
37
        $stmt->bindValue(1, $cacheKey);
38
        $stmt->bindValue(2, $this->serialize($cacheData));
39
        $stmt->bindValue(3, $namespace);
40
        $stmt->bindValue(4, $expirationTime);
41
        $stmt->bindValue(5, $createdAt);
42
43
        return $stmt->execute() && $stmt->rowCount() > 0;
44
    }
45
46
    /**
47
    * @param string $cacheKey
48
    * @param string $namespace
49
    * @return mixed
50
    */
51
    public function retrieve(string $cacheKey, string $namespace = '')
52
    {
53
        $driver = $this->connection->getAttribute(PDO::ATTR_DRIVER_NAME);
54
        $nowFunction = $this->getCurrentDateTime($driver);
55
56
        $stmt = $this->connection->prepare(
57
            "SELECT cacheData FROM cacheer_table WHERE cacheKey = ? AND cacheNamespace = ? AND expirationTime > $nowFunction"
58
        );
59
        $stmt->bindValue(1, $cacheKey);
60
        $stmt->bindValue(2, $namespace);
61
        $stmt->execute();
62
63
        $data = $stmt->fetch(PDO::FETCH_ASSOC);
64
        return (!empty($data)) ? $this->serialize($data['cacheData'], false) : null;
65
    }
66
67
    /**
68
     * @param string $cacheKey
69
     * @param mixed $cacheData
70
     * @param string $namespace
71
     * @return bool
72
     */
73
    public function update(string $cacheKey, mixed $cacheData, string $namespace = '')
74
    {
75
        $stmt = $this->connection->prepare(
76
            "UPDATE cacheer_table SET cacheData = ?, cacheNamespace = ? WHERE cacheKey = ?"
77
        );
78
        $stmt->bindValue(1, $this->serialize($cacheData));
79
        $stmt->bindValue(2, $namespace);
80
        $stmt->bindValue(3, $cacheKey);
81
        $stmt->execute();
82
83
        return $stmt->rowCount() > 0;
84
    }
85
86
    /**
87
     * @param string $cacheKey
88
     * @param string $namespace
89
     * @return bool
90
     */
91
    public function clear(string $cacheKey, string $namespace = '')
92
    {
93
        $stmt = $this->connection->prepare(
94
            "DELETE FROM cacheer_table WHERE cacheKey = ? AND cacheNamespace = ?"
95
        );
96
        $stmt->bindValue(1, $cacheKey);
97
        $stmt->bindValue(2, $namespace);
98
        $stmt->execute();
99
100
        return $stmt->rowCount() > 0;
101
    }
102
103
    /**
104
    * @param string $cacheKey
105
    * @param int|string $ttl
106
    * @param string $namespace
107
    * @return bool
108
    */
109
    public function renew(string $cacheKey, int|string $ttl, string $namespace = '')
110
    {
111
        $currentTime = date('Y-m-d H:i:s');
112
113
        $actualExpirationTime = $this->connection->prepare(
114
            "SELECT expirationTime FROM cacheer_table 
115
            WHERE cacheKey = ? AND cacheNamespace = ? AND expirationTime > ?"
116
        );
117
        $actualExpirationTime->bindValue(1, $cacheKey);
118
        $actualExpirationTime->bindValue(2, $namespace);
119
        $actualExpirationTime->bindValue(3, $currentTime);
120
        $actualExpirationTime->execute();
121
122
        if ($actualExpirationTime->rowCount() > 0) {
123
           
124
            $stmt = $this->connection->prepare(
125
                "UPDATE cacheer_table
126
                SET expirationTime = DATE_ADD(expirationTime, INTERVAL ? SECOND)
127
                WHERE cacheKey = ? AND cacheNamespace = ? AND expirationTime > ?"
128
            );
129
            $stmt->bindValue(1, (int) $ttl, PDO::PARAM_INT);
130
            $stmt->bindValue(2, $cacheKey);
131
            $stmt->bindValue(3, $namespace);
132
            $stmt->bindValue(4, $currentTime);
133
            $stmt->execute();
134
135
            return $stmt->rowCount() > 0;
136
        }
137
138
        return false;
139
    }
140
141
142
143
    /**
144
     * @return bool
145
     */
146
    public function flush()
147
    {
148
        return $this->connection->exec("DELETE FROM cacheer_table") !== false;
149
    }
150
151
    /**
152
     * Serializa os dados de cache para armazenamento.
153
     *
154
     * @param mixed $data
155
     * @return string
156
     */
157
    private function serialize(mixed $data, bool $serialize = true)
158
    {
159
        return $serialize ? serialize($data) : unserialize($data);
160
    }
161
162
    /**
163
    * @param string $driver
164
    * @return string
165
    */
166
    private function getCurrentDateTime(string $driver)
167
    {
168
        return ($driver === 'sqlite') ? "DATETIME('now', 'localtime')" : "NOW()";
169
    }
170
}
171