Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.64% covered (success)
83.64%
92 / 110
69.23% covered (warning)
69.23%
9 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
Db
83.64% covered (success)
83.64%
92 / 110
69.23% covered (warning)
69.23%
9 / 13
40.37
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 setDb
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getDb
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTable
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setTable
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 save
70.00% covered (success)
70.00%
28 / 40
0.00% covered (danger)
0.00%
0 / 1
9.73
 get
86.67% covered (success)
86.67%
13 / 15
0.00% covered (danger)
0.00%
0 / 1
5.06
 has
83.33% covered (success)
83.33%
10 / 12
0.00% covered (danger)
0.00%
0 / 1
4.07
 delete
80.00% covered (success)
80.00%
8 / 10
0.00% covered (danger)
0.00%
0 / 1
3.07
 clear
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 encodeValue
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 decodeValue
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 createTable
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Debug\Storage;
15
16use Pop\Db\Adapter\AbstractAdapter;
17
18/**
19 * Debug database storage class
20 *
21 * @category   Pop
22 * @package    Pop\Debug
23 * @author     Nick Sagona, III <dev@nolainteractive.com>
24 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
25 * @license    http://www.popphp.org/license     New BSD License
26 * @version    1.3.0
27 */
28class Db extends AbstractStorage
29{
30
31    /**
32     * DB adapter
33     * @var AbstractAdapter
34     */
35    protected $db = null;
36
37    /**
38     * Table
39     * @var string
40     */
41    protected $table = 'pop_debug';
42
43    /**
44     * Constructor
45     *
46     * Instantiate the DB writer object
47     *
48     * The DB table requires the following fields at a minimum:
49
50     *     id    INT
51     *     value TEXT, VARCHAR, etc.
52     *
53     * @param  AbstractAdapter $db
54     * @param  string          $format
55     * @param  string          $table
56     */
57    public function __construct(AbstractAdapter $db, $format = 'text', $table = 'pop_debug')
58    {
59        parent::__construct($format);
60
61        $this->setDb($db);
62        $this->setTable($table);
63
64        if (!$db->hasTable($this->table)) {
65            $this->createTable();
66        }
67    }
68
69    /**
70     * Set the current debug db adapter.
71     *
72     * @param  string $db
73     * @return Db
74     */
75    public function setDb($db)
76    {
77        $this->db = $db;
78        return $this;
79    }
80
81    /**
82     * Get the current debug db adapter.
83     *
84     * @return AbstractAdapter
85     */
86    public function getDb()
87    {
88        return $this->db;
89    }
90
91    /**
92     * Get the current debug db table.
93     *
94     * @return string
95     */
96    public function getTable()
97    {
98        return $this->table;
99    }
100
101    /**
102     * Set the debug db table
103     *
104     * @param  string $table
105     * @return Db
106     */
107    public function setTable($table)
108    {
109        $this->table = $table;
110        return $this;
111    }
112
113    /**
114     * Save debug data
115     *
116     * @param  string $id
117     * @param  mixed  $value
118     * @return Db
119     */
120    public function save($id, $value)
121    {
122        // Determine if the value already exists.
123        $sql         = $this->db->createSql();
124        $placeholder = $sql->getPlaceholder();
125
126        if ($placeholder == ':') {
127            $placeholder .= 'key';
128        } else if ($placeholder == '$') {
129            $placeholder .= '1';
130        }
131
132        $sql->select()->from($this->table)->where('key = ' . $placeholder);
133
134        $this->db->prepare($sql)
135            ->bindParams(['key' => $id])
136            ->execute();
137
138        $rows = $this->db->fetchAll();
139
140        $sql->reset();
141        $placeholder = $sql->getPlaceholder();
142
143        // Insert new value
144        if (count($rows) == 0) {
145            if ($placeholder == ':') {
146                $placeholder1 = ':key';
147                $placeholder2 = ':value';
148            } else if ($placeholder == '$') {
149                $placeholder1 = '$1';
150                $placeholder2 = '$2';
151            } else {
152                $placeholder1 = $placeholder;
153                $placeholder2 = $placeholder;
154            }
155            $sql->insert($this->table)->values(['key' => $placeholder1, 'value' => $placeholder2]);
156            $params = [
157                'key'   => $id,
158                'value' => $this->encodeValue($value)
159            ];
160        // Else, update it.
161        } else {
162            if ($placeholder == ':') {
163                $placeholder1 = ':value';
164                $placeholder2 = ':key';
165            } else if ($placeholder == '$') {
166                $placeholder1 = '$1';
167                $placeholder2 = '$2';
168            } else {
169                $placeholder1 = $placeholder;
170                $placeholder2 = $placeholder;
171            }
172            $sql->update($this->table)->values(['value' => $placeholder1])->where('key = ' . $placeholder2);
173            $params = [
174                'value' => $this->encodeValue($value),
175                'key'   => $id
176            ];
177        }
178
179        // Save value
180        $this->db->prepare($sql)
181            ->bindParams($params)
182            ->execute();
183
184        return $this;
185    }
186
187    /**
188     * Get debug data
189     *
190     * @param  string $id
191     * @return mixed
192     */
193    public function get($id)
194    {
195        $sql         = $this->db->createSql();
196        $placeholder = $sql->getPlaceholder();
197        $value       = false;
198
199        if ($placeholder == ':') {
200            $placeholder .= 'key';
201        } else if ($placeholder == '$') {
202            $placeholder .= '1';
203        }
204
205        $sql->select()->from($this->table)->where('key = ' . $placeholder);
206
207        $this->db->prepare($sql)
208            ->bindParams(['key' => $id])
209            ->execute();
210
211        $rows = $this->db->fetchAll();
212
213        // If the value is found, return.
214        if (isset($rows[0]) && isset($rows[0]['value'])) {
215            $value = $this->decodeValue($rows[0]['value']);
216        }
217
218        return $value;
219    }
220
221    /**
222     * Determine if debug data exists
223     *
224     * @param  string $id
225     * @return mixed
226     */
227    public function has($id)
228    {
229        $sql         = $this->db->createSql();
230        $placeholder = $sql->getPlaceholder();
231
232        if ($placeholder == ':') {
233            $placeholder .= 'key';
234        } else if ($placeholder == '$') {
235            $placeholder .= '1';
236        }
237
238        $sql->select()->from($this->table)->where('key = ' . $placeholder);
239
240        $this->db->prepare($sql)
241            ->bindParams(['key' => $id])
242            ->execute();
243
244        $rows = $this->db->fetchAll();
245
246        return (isset($rows[0]) && isset($rows[0]['value']));
247    }
248
249    /**
250     * Delete debug data
251     *
252     * @param  string $id
253     * @return void
254     */
255    public function delete($id)
256    {
257        $sql         = $this->db->createSql();
258        $placeholder = $sql->getPlaceholder();
259
260        if ($placeholder == ':') {
261            $placeholder .= 'key';
262        } else if ($placeholder == '$') {
263            $placeholder .= '1';
264        }
265
266        $sql->delete($this->table)->where('key = ' . $placeholder);
267
268        $this->db->prepare($sql)
269            ->bindParams(['key' => $id])
270            ->execute();
271    }
272
273    /**
274     * Clear all debug data
275     *
276     * @return void
277     */
278    public function clear()
279    {
280        $sql = $this->db->createSql();
281        $sql->delete($this->table);
282        $this->db->query($sql);
283    }
284
285    /**
286     * Encode the value based on the format
287     *
288     * @param  mixed  $value
289     * @throws Exception
290     * @return string
291     */
292    public function encodeValue($value)
293    {
294        if ($this->format == self::JSON) {
295            $value = json_encode($value, JSON_PRETTY_PRINT);
296        } else if ($this->format == self::PHP) {
297            $value = serialize($value);
298        } else if (!is_string($value)) {
299            throw new Exception('Error: The value must be a string if storing in text format.');
300        }
301
302        return $value;
303    }
304
305    /**
306     * Decode the value based on the format
307     *
308     * @param  mixed  $value
309     * @return mixed
310     */
311    public function decodeValue($value)
312    {
313        if ($this->format == self::JSON) {
314            $value = json_decode($value, true);
315        } else if ($this->format == self::PHP) {
316            $value = unserialize($value);
317        }
318
319        return $value;
320    }
321
322    /**
323     * Create table in database
324     *
325     * @return void
326     */
327    protected function createTable()
328    {
329        $schema = $this->db->createSchema();
330        $schema->create($this->table)
331            ->int('id')->increment()
332            ->varchar('key', 255)
333            ->text('value')
334            ->primary('id');
335
336        $this->db->query($schema);
337    }
338
339}