Completed
Push — master ( 22d0de...5ae499 )
by smiley
01:45
created

DatabaseLog   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 46
ccs 0
cts 7
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __log() 0 12 1
A close() 0 4 1
1
<?php
2
/**
3
 * Class DatabaseLog
4
 *
5
 * @filesource   DatabaseLog.php
6
 * @created      04.01.2018
7
 * @package      chillerlan\Logger\Output
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2018 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Logger\Output;
14
15
use chillerlan\Database\Connection;
16
use chillerlan\Logger\LogOptions;
17
18
class DatabaseLog extends LogOutputAbstract{
19
20
	/**
21
	 * @var \chillerlan\Database\Connection
22
	 */
23
	protected $db;
24
25
	/** @noinspection PhpMissingParentConstructorInspection */
26
	/**
27
	 * DatabaseLog constructor.
28
	 *
29
	 * @param \chillerlan\Logger\LogOptions $options
30
	 * @param \chillerlan\Database\Connection    $db
31
	 */
32
	public function __construct(LogOptions $options, Connection $db){
33
		$this->options = $options;
34
		$this->db      = $db;
35
36
		$this->db->connect();
37
	}
38
39
	/**
40
	 * @inheritdoc
41
	 */
42
	protected function __log(string $level, string $message, array $context = []){
43
44
		// TODO: $context, tests
45
		$this->db->insert
46
			->into($this->options->dbLogTable)
47
			->values([
48
				'level'   => $level,
49
				'message' => $message,
50
				'context' => json_encode($context),
51
				'time'    => time(),
52
			])
53
			->execute()
54
		;
55
	}
56
57
	/**
58
	 * @inheritdoc
59
	 */
60
	public function close():LogOutputInterface{
61
		$this->db->disconnect();
62
63
		return $this;
64
	}
65
66
}
67