Test Failed
Branch master (c50bb7)
by Terry
15:06 queued 10:08
created

SqlTrait::doGet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 17
ccs 10
cts 10
cp 1
crap 2
rs 9.9666
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 12
    protected function doGet(string $key): array
53
    {
54 12
        $sql = 'SELECT * FROM ' . $this->table . '
55
            WHERE cache_key = :cache_key';
56
57 12
        $query = $this->db->prepare($sql);
58 12
        $query->bindValue(':cache_key', $key, PDO::PARAM_STR);
59 12
        $query->execute();
60 12
        $resultData = $query->fetch($this->db::FETCH_ASSOC);
61
62 12
        if (empty($resultData['cache_value'])) {
63 12
            return [];
64
        }
65
66 12
        $data = unserialize($resultData['cache_value']);
67
68 12
        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 12
    protected function doSet(string $key, $value, int $ttl, int $timestamp): bool
82
    {
83 12
        $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 12
        $sql = 'INSERT INTO ' . $this->table . ' (cache_key, cache_value) 
86
            VALUES (:cache_key, :cache_value)';
87
88 12
        if (!empty($cacheData)) {
89 4
            $sql = 'UPDATE ' . $this->table . ' 
90
                SET cache_value = :cache_value 
91
                WHERE cache_key = :cache_key';
92
        }
93
94 12
        $query = $this->db->prepare($sql);
95
96
        $data = [
97 12
            'cache_key'   => $key,
98 12
            'cache_value' => serialize([
99 12
                'timestamp' => $timestamp,
100 12
                'ttl'       => $ttl,
101 12
                'value'     => $value
102
            ]),
103
        ];
104
105 12
        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 8
    protected function doDelete(string $key): bool
116
    {
117 8
        $sql = 'DELETE FROM ' . $this->table . ' 
118
            WHERE cache_key = ?';
119
120 8
        $query = $this->db->prepare($sql);
121 8
        $result = $query->execute([$key]);
122
123 8
        return $result;
124
    }
125
126
    /**
127
     * Delete all caches by an extended Cache Driver.
128
     * 
129
     * @return bool
130
     */
131 2
    protected function doClear(): bool
132
    {
133 2
        $sql = 'TRUNCATE TABLE ' . $this->table;
134
135 2
        $query = $this->db->prepare($sql);
136 2
        $result = $query->execute();
137
138 2
        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
}