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