Passed
Pull Request — master (#70)
by Christian
02:13 queued 50s
created

Database::getColumns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
namespace Maphper\DataSource;
3
class Database implements \Maphper\DataSource {
4
	const EDIT_STRUCTURE = 1;
5
	const EDIT_INDEX = 2;
6
	const EDIT_OPTIMISE = 4;
7
8
	private $primaryKey;
9
	private $table;
10
11
	private $fields = '*';
12
    private $databaseSelect;
13
    private $databaseCrud;
14
15
	public function __construct($db, $table, $primaryKey = 'id', array $options = []) {
16
		$options = new DatabaseOptions($db, $options);
17
		$adapter = $options->getAdapter();
18
19
		$this->primaryKey = is_array($primaryKey) ? $primaryKey : [$primaryKey];
20
		$this->table = $table;
21
22
		$this->fields = implode(',', array_map([$adapter, 'quote'], (array) $options->read('fields')));
23
24
		$defaultSort = $options->read('defaultSort') !== false ? $options->read('defaultSort')  : implode(', ', $this->primaryKey);
25
26
        $databaseModify = new DatabaseModify($adapter, $options->getEditMode(), $table);
0 ignored issues
show
Bug introduced by
It seems like $adapter can also be of type Maphper\DataSource; however, parameter $adapter of Maphper\DataSource\DatabaseModify::__construct() does only seem to accept Maphper\DataSource\DatabaseAdapter, maybe add an additional type check? ( Ignorable by Annotation )

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

26
        $databaseModify = new DatabaseModify(/** @scrutinizer ignore-type */ $adapter, $options->getEditMode(), $table);
Loading history...
27
28
        $this->databaseSelect = new DatabaseSelect($adapter, $databaseModify, $table, $defaultSort, $options->getCacheMode());
0 ignored issues
show
Bug introduced by
It seems like $adapter can also be of type Maphper\DataSource; however, parameter $adapter of Maphper\DataSource\DatabaseSelect::__construct() does only seem to accept Maphper\DataSource\DatabaseAdapter, maybe add an additional type check? ( Ignorable by Annotation )

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

28
        $this->databaseSelect = new DatabaseSelect(/** @scrutinizer ignore-type */ $adapter, $databaseModify, $table, $defaultSort, $options->getCacheMode());
Loading history...
29
        $this->databaseCrud = new DatabaseCrud($adapter, $databaseModify, $this->databaseSelect, $table, $this->primaryKey);
0 ignored issues
show
Bug introduced by
It seems like $adapter can also be of type Maphper\DataSource; however, parameter $adapter of Maphper\DataSource\DatabaseCrud::__construct() does only seem to accept Maphper\DataSource\DatabaseAdapter, maybe add an additional type check? ( Ignorable by Annotation )

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

29
        $this->databaseCrud = new DatabaseCrud(/** @scrutinizer ignore-type */ $adapter, $databaseModify, $this->databaseSelect, $table, $this->primaryKey);
Loading history...
30
31
		$databaseModify->optimizeColumns();
32
	}
33
34
	public function getPrimaryKey() {
35
		return $this->primaryKey;
36
	}
37
38
	public function getColumns() {
39
		return $this->databaseSelect->getColumns($this->table);
40
	}
41
42
	public function deleteById($id) {
43
		$this->databaseCrud->deleteById($id);
44
	}
45
46
	public function findById($id) {
47
		return $this->databaseSelect->findById($id, $this->getPrimaryKey()[0]);
48
	}
49
50
	public function findAggregate($function, $field, $group = null, array $criteria = [], array $options = []) {
51
		return $this->databaseSelect->findAggregate($function, $field, $group, $criteria, $options);
52
	}
53
54
	public function findByField(array $fields, $options = []) {
55
        return $this->databaseSelect->findByField($fields, $options);
56
	}
57
58
	public function deleteByField(array $fields, array $options = []) {
59
		$this->databaseCrud->deleteByField($fields, $options);
60
	}
61
62
	public function save($data) {
63
        $this->databaseCrud->save($data, true);
64
	}
65
}
66