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

DatabaseLog::close()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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