SqlTrait   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 47
c 2
b 0
f 0
dl 0
loc 161
ccs 50
cts 50
cp 1
rs 10
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A doGet() 0 17 2
A doClear() 0 8 1
A doDelete() 0 9 1
A doHas() 0 11 1
A doSet() 0 25 2
A getAll() 0 17 2
1
<?php
2
/*
3
 * This file is part of the Shieldon Simple Cache package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Shieldon\SimpleCache\Driver;
14
15
use PDO;
16
use function serialize;
17
use function unserialize;
18
19
/**
20
 * A trait implements SQL-action methods.
21
 */
22
trait SqlTrait
23
{
24
    /**
25
     * The PDO instance.
26
     *
27
     * @var \PDO
28
     */
29
    protected $db;
30
    
31
    /**
32
     * Table name.
33
     *
34
     * @var string
35
     */
36
    protected $table = 'cache_data';
37
38
    /**
39
     * Rebuid a database table schema for SQL-like Cache driver.
40
     *
41
     * @return bool True for success, overwise.
42
     */
43
    abstract public function rebuild(): bool;
44
45
    /**
46
     * Fetch a cache by an extended Cache Driver.
47
     *
48
     * @param string $key The key of a cache.
49
     *
50
     * @return array
51
     */
52 18
    protected function doGet(string $key): array
53
    {
54 18
        $sql = 'SELECT * FROM ' . $this->table . '
55
            WHERE cache_key = :cache_key';
56
57 18
        $query = $this->db->prepare($sql);
58 18
        $query->bindValue(':cache_key', $key, PDO::PARAM_STR);
59 18
        $query->execute();
60 18
        $resultData = $query->fetch($this->db::FETCH_ASSOC);
61
62 18
        if (empty($resultData['cache_value'])) {
63 18
            return [];
64
        }
65
66 14
        $data = unserialize($resultData['cache_value']);
67
68 14
        return $data;
69
    }
70
71
    /**
72
     * Set a cache by an extended Cache Driver.
73
     *
74
     * @param string $key       The key of a cache.
75
     * @param mixed  $value     The value of a cache. (serialized)
76
     * @param int    $ttl       The time to live for a cache.
77
     * @param int    $timestamp The time to store a cache.
78
     *
79
     * @return bool
80
     */
81 18
    protected function doSet(string $key, $value, int $ttl, int $timestamp): bool
82
    {
83 18
        $cacheData = $this->get($key);
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

83
        /** @scrutinizer ignore-call */ 
84
        $cacheData = $this->get($key);
Loading history...
84
85 18
        $sql = 'INSERT INTO ' . $this->table . ' (cache_key, cache_value) 
86
            VALUES (:cache_key, :cache_value)';
87
88 18
        if (!empty($cacheData)) {
89 4
            $sql = 'UPDATE ' . $this->table . ' 
90
                SET cache_value = :cache_value 
91
                WHERE cache_key = :cache_key';
92
        }
93
94 18
        $query = $this->db->prepare($sql);
95
96
        $data = [
97 18
            'cache_key'   => $key,
98 18
            'cache_value' => serialize([
99 18
                'timestamp' => $timestamp,
100 18
                'ttl'       => $ttl,
101 18
                'value'     => $value,
102
            ]),
103
        ];
104
105 18
        return $query->execute($data);
106
    }
107
108
    /**
109
     * Delete a cache by an extended Cache Driver.
110
     *
111
     * @param string $key The key of a cache.
112
     *
113
     * @return bool
114
     */
115 10
    protected function doDelete(string $key): bool
116
    {
117 10
        $sql = 'DELETE FROM ' . $this->table . ' 
118
            WHERE cache_key = ?';
119
120 10
        $query = $this->db->prepare($sql);
121 10
        $result = $query->execute([$key]);
122
123 10
        return $result;
124
    }
125
126
    /**
127
     * Delete all caches by an extended Cache Driver.
128
     *
129
     * @return bool
130
     */
131 6
    protected function doClear(): bool
132
    {
133 6
        $sql = 'TRUNCATE TABLE ' . $this->table;
134
135 6
        $query = $this->db->prepare($sql);
136 6
        $result = $query->execute();
137
138 6
        return $result;
139
    }
140
141
    /**
142
     * Check if the cache exists or not.
143
     *
144
     * @param string $key The key of a cache.
145
     *
146
     * @return bool
147
     */
148 8
    protected function doHas(string $key): bool
149
    {
150 8
        $sql = 'SELECT COUNT(*) FROM ' . $this->table . '
151
            WHERE cache_key = :cache_key';
152
153 8
        $query = $this->db->prepare($sql);
154 8
        $query->bindValue(':cache_key', $key, PDO::PARAM_STR);
155 8
        $query->execute();
156 8
        $count = $query->fetchColumn();
157
158 8
        return $count > 0;
159
    }
160
161
    /**
162
     * Fetch all cache items.
163
     *
164
     * @return array
165
     */
166 6
    protected function getAll(): array
167
    {
168 6
        $list = [];
169
170 6
        $sql = 'SELECT * FROM ' . $this->table;
171
172 6
        $query = $this->db->prepare($sql);
173 6
        $query->execute();
174 6
        $results = $query->fetchAll($this->db::FETCH_ASSOC);
175
176 6
        foreach ($results as $row) {
177 6
            $key   = $row['cache_key'];
178 6
            $value = $row['cache_value'];
179
180 6
            $list[$key] = unserialize($value);
181
        }
182 6
        return $list;
183
    }
184
}
185