DatabaseAbstract   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 56
rs 10
c 2
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 2
A setLogger() 0 4 1
1
<?php
2
/**
3
 * Class DatabaseAbstract
4
 *
5
 * @filesource   DatabaseAbstract.php
6
 * @created      20.01.2018
7
 * @package      chillerlan\Database
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2018 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Database;
14
15
use chillerlan\Database\{
16
	Drivers\DriverInterface, Query\QueryBuilder
17
};
18
use chillerlan\Settings\SettingsContainerInterface;
19
use Psr\Log\{
20
	LoggerAwareInterface, LoggerAwareTrait, LoggerInterface, NullLogger
21
};
22
use Psr\SimpleCache\CacheInterface;
23
24
abstract class DatabaseAbstract implements LoggerAwareInterface{
25
	use LoggerAwareTrait;
26
27
	/**
28
	 * @var \chillerlan\Database\DatabaseOptions
29
	 */
30
	protected $options;
31
32
	/**
33
	 * @var \Psr\SimpleCache\CacheInterface
34
	 */
35
	protected $cache;
36
37
	/**
38
	 * @var \chillerlan\Database\Drivers\DriverInterface
39
	 */
40
	protected $driver;
41
42
	/**
43
	 * @var \chillerlan\Database\Query\QueryBuilder
44
	 */
45
	protected $query;
46
47
	/**
48
	 * Database constructor.
49
	 *
50
	 * @param \chillerlan\Settings\SettingsContainerInterface $options
51
	 * @param \Psr\SimpleCache\CacheInterface|null $cache
52
	 * @param \Psr\Log\LoggerInterface|null        $logger
53
	 *
54
	 * @throws \chillerlan\Database\DatabaseException
55
	 */
56
	public function __construct(SettingsContainerInterface $options, CacheInterface $cache = null, LoggerInterface $logger = null){
57
		$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\Database\DatabaseOptions. 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...
58
		$this->cache   = $cache;
59
60
		// set a default logger
61
		$this->logger  = $logger ?? new NullLogger;
62
		$this->driver  = new $this->options->driver($this->options, $this->cache, $this->logger);
63
64
		if(!$this->driver instanceof DriverInterface){
65
			throw new DatabaseException('invalid driver interface');
66
		}
67
68
		$this->query   = new QueryBuilder($this->driver, $this->logger);
69
	}
70
71
	/**
72
	 * @param \Psr\Log\LoggerInterface $logger
73
	 *
74
	 * @return void
75
	 */
76
	public function setLogger(LoggerInterface $logger):void{
77
		$this->logger = $logger;
78
		$this->driver->setLogger($logger);
0 ignored issues
show
Bug introduced by
The method setLogger() does not exist on chillerlan\Database\Drivers\DriverInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to chillerlan\Database\Drivers\DriverInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

78
		$this->driver->/** @scrutinizer ignore-call */ 
79
                 setLogger($logger);
Loading history...
79
		$this->query->setLogger($logger);
80
	}
81
82
}
83