Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
80 / 80
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
Database
100.00% covered (success)
100.00%
80 / 80
100.00% covered (success)
100.00%
8 / 8
12
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
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
100.00% covered (success)
100.00%
42 / 42
100.00% covered (success)
100.00%
1 / 1
4
 clear
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 createTable
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <dev@noladev.com>
8 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Debug\Storage;
16
17use Pop\Db\Adapter\AbstractAdapter;
18use Pop\Debug\Handler\AbstractHandler;
19
20/**
21 * Debug database storage class
22 *
23 * @category   Pop
24 * @package    Pop\Debug
25 * @author     Nick Sagona, III <dev@noladev.com>
26 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
27 * @license    https://www.popphp.org/license     New BSD License
28 * @version    4.0.0
29 */
30class Database extends AbstractStorage
31{
32
33    /**
34     * DB adapter
35     * @var ?AbstractAdapter
36     */
37    protected ?AbstractAdapter $db = null;
38
39    /**
40     * Table
41     * @var string
42     */
43    protected string $table = 'pop_debug';
44
45    /**
46     * Constructor
47     *
48     * Instantiate the DB writer object
49     *
50     * The DB table requires the following fields at a minimum:
51
52     *     id    INT
53     *     value TEXT, VARCHAR, etc.
54     *
55     * @param  AbstractAdapter $db
56     * @param  string          $table
57     */
58    public function __construct(AbstractAdapter $db, string $table = 'pop_debug')
59    {
60        $this->setDb($db);
61        $this->setTable($table);
62
63        if (!$db->hasTable($this->table)) {
64            $this->createTable();
65        }
66    }
67
68    /**
69     * Set the current debug db adapter.
70     *
71     * @param  AbstractAdapter $db
72     * @return Database
73     */
74    public function setDb(AbstractAdapter $db): Database
75    {
76        $this->db = $db;
77        return $this;
78    }
79
80    /**
81     * Get the current debug db adapter.
82     *
83     * @return ?AbstractAdapter
84     */
85    public function getDb(): ?AbstractAdapter
86    {
87        return $this->db;
88    }
89
90    /**
91     * Get the current debug db table.
92     *
93     * @return string
94     */
95    public function getTable(): string
96    {
97        return $this->table;
98    }
99
100    /**
101     * Set the debug db table
102     *
103     * @param  string $table
104     * @return Database
105     */
106    public function setTable(string $table): Database
107    {
108        $this->table = $table;
109        return $this;
110    }
111
112    /**
113     * Save debug data
114     *
115     * @param  string          $id
116     * @param  string          $name
117     * @param  AbstractHandler $handler
118     * @return void
119     */
120    public function save(string $id, string $name, AbstractHandler $handler): void
121    {
122        $sql         = $this->db->createSql();
123        $placeholder = $sql->getPlaceholder();
124
125        if ($placeholder == ':') {
126            $placeholders = [
127                'key'     => ':key',
128                'handler' => ':handler',
129                'start'   => ':start',
130                'end'     => ':end',
131                'elapsed' => ':elapsed',
132                'type'    => ':type',
133                'message' => ':message',
134                'context' => ':context',
135            ];
136        } else if ($placeholder == '$') {
137            $placeholders = [
138                'key'     => '$1',
139                'handler' => '$2',
140                'start'   => '$3',
141                'end'     => '$4',
142                'elapsed' => '$5',
143                'type'    => '$6',
144                'message' => '$7',
145                'context' => '$8',
146            ];
147        } else {
148            $placeholders = [
149                'key'     => '?',
150                'handler' => '?',
151                'start'   => '?',
152                'end'     => '?',
153                'elapsed' => '?',
154                'type'    => '?',
155                'message' => '?',
156                'context' => '?',
157            ];
158        }
159
160        $events = $this->prepareEvents($id, $name, $handler);
161
162        foreach ($events as $event) {
163            $sql->reset()
164                ->insert($this->table)
165                ->values($placeholders);
166
167            // Save value
168            $this->db->prepare($sql)
169                ->bindParams($event)
170                ->execute();
171        }
172    }
173
174    /**
175     * Clear all debug data
176     *
177     * @return void
178     */
179    public function clear(): void
180    {
181        $sql = $this->db->createSql();
182        $sql->delete($this->table);
183        $this->db->query($sql);
184    }
185
186    /**
187     * Create table in database
188     *
189     * @return void
190     */
191    protected function createTable(): void
192    {
193        $schema = $this->db->createSchema();
194        $schema->create($this->table)
195            ->int('id')->increment()
196            ->varchar('key', 255)
197            ->varchar('handler', 255)
198            ->decimal('start', 16, 6)->nullable()
199            ->decimal('end', 16, 6)->nullable()
200            ->decimal('elapsed', 16, 6)->nullable()
201            ->varchar('type', 255)
202            ->text('message')->nullable()
203            ->text('context')->nullable()
204            ->primary('id');
205
206        $schema->execute();
207
208        $schema->alter($this->table)->index('key');
209        $schema->execute();
210
211        $schema->alter($this->table)->index('handler');
212        $schema->execute();
213
214        $schema->alter($this->table)->index('start');
215        $schema->execute();
216
217        $schema->alter($this->table)->index('end');
218        $schema->execute();
219
220        $schema->alter($this->table)->index('elapsed');
221        $schema->execute();
222
223        $schema->alter($this->table)->index('type');
224        $schema->execute();
225    }
226
227}