DatabaseLog::__log()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 12
ccs 0
cts 1
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
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\Database;
0 ignored issues
show
Bug introduced by
The type chillerlan\Database\Database was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use chillerlan\Settings\SettingsContainerInterface;
17
18
class DatabaseLog extends LogOutputAbstract{
19
20
	/**
21
	 * @var \chillerlan\Database\Database
22
	 */
23
	protected $db;
24
25
	/** @noinspection PhpMissingParentConstructorInspection */
26
	/**
27
	 * DatabaseLog constructor.
28
	 *
29
	 * @param \chillerlan\Settings\SettingsContainerInterface|null $options
30
	 * @param \chillerlan\Database\Database    $db
31
	 */
32
	public function __construct(SettingsContainerInterface $options, Database $db){
33
		$this->options = $options;
0 ignored issues
show
Documentation Bug introduced by
$options is of type chillerlan\Settings\SettingsContainerInterface, but the property $options was declared to be of type chillerlan\Logger\LogOptions. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
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
			->query()
54
		;
55
	}
56
57
	/**
58
	 * @inheritdoc
59
	 */
60
	public function close():LogOutputInterface{
61
		$this->db->disconnect();
62
63
		return $this;
64
	}
65
66
}
67