Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.45% covered (success)
95.45%
42 / 44
83.33% covered (success)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Database
95.45% covered (success)
95.45%
42 / 44
83.33% covered (success)
83.33%
5 / 6
11
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 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
 writeLog
92.31% covered (success)
92.31%
24 / 26
0.00% covered (danger)
0.00%
0 / 1
5.01
 createTable
100.00% covered (success)
100.00%
10 / 10
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-2024 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\Log\Writer;
15
16use Pop\Db\Adapter\AbstractAdapter;
17
18/**
19 * Database log writer class
20 *
21 * @category   Pop
22 * @package    Pop\Log
23 * @author     Nick Sagona, III <dev@nolainteractive.com>
24 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
25 * @license    http://www.popphp.org/license     New BSD License
26 * @version    4.0.0
27 */
28class Database extends AbstractWriter
29{
30
31    /**
32     * DB adapter
33     * @var ?AbstractAdapter
34     */
35    protected ?AbstractAdapter $db = null;
36
37    /**
38     * Table
39     * @var string
40     */
41    protected string $table = 'pop_log';
42
43    /**
44     * Constructor
45     *
46     * Instantiate the DB writer object
47     *
48     * The DB table requires the following fields at a minimum:
49     *     timestamp  DATETIME
50     *     level      INT
51     *     name       VARCHAR
52     *     message    TEXT, VARCHAR, etc.
53     *     context    TEXT, VARCHAR, etc.
54     *
55     * @param  AbstractAdapter $db
56     * @param  string          $table
57     */
58    public function __construct(AbstractAdapter $db, string $table = 'pop_log')
59    {
60        $this->db = $db;
61        $this->setTable($table);
62
63        if (!$db->hasTable($this->table)) {
64            $this->createTable();
65        }
66    }
67
68    /**
69     * Get DB
70     * @return AbstractAdapter
71     */
72    public function getDb(): AbstractAdapter
73    {
74        return $this->db;
75    }
76
77    /**
78     * Get table
79     * @return string
80     */
81    public function getTable(): string
82    {
83        return $this->table;
84    }
85
86    /**
87     * Set table
88     * @parma  string $table
89     * @return Database
90     */
91    public function setTable(string $table): Database
92    {
93        $this->table = $table;
94        return $this;
95    }
96
97    /**
98     * Write to the log
99     *
100     * @param  mixed  $level
101     * @param  string $message
102     * @param  array  $context
103     * @return Database
104     */
105    public function writeLog(mixed $level, string $message, array $context = []): Database
106    {
107        if ($this->isWithinLogLimit($level)) {
108            $sql    = $this->db->createSql();
109            $fields = [
110                'timestamp' => $context['timestamp'],
111                'level'     => $level,
112                'name'      => $context['name'],
113                'message'   => $message,
114                'context'   => $this->getContext($context)
115            ];
116
117            $columns = [];
118            $params  = [];
119
120            $i = 1;
121            foreach ($fields as $column => $value) {
122                $placeholder = $sql->getPlaceholder();
123
124                if ($placeholder == ':') {
125                    $placeholder .= $column;
126                } else if ($placeholder == '$') {
127                    $placeholder .= $i;
128                }
129                $columns[$column] = $placeholder;
130                $params[$column]  = $value;
131                $i++;
132            }
133
134            $sql->insert($this->table)->values($columns);
135
136            $this->db->prepare((string)$sql)
137                ->bindParams($params)
138                ->execute();
139        }
140
141        return $this;
142    }
143
144    /**
145     * Create table in database
146     *
147     * @return void
148     */
149    protected function createTable(): void
150    {
151        $schema = $this->db->createSchema();
152        $schema->create($this->table)
153            ->int('id')->increment()
154            ->datetime('timestamp')
155            ->int('level', 1)
156            ->varchar('name', 255)
157            ->text('message')
158            ->text('context')
159            ->primary('id');
160
161        $this->db->query($schema);
162    }
163
164}