Completed
Push — master ( 2b9193...abd76f )
by James Ekow Abaka
03:07
created

DriverAdapter   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 77.55%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 5
dl 0
loc 111
rs 10
c 0
b 0
f 0
ccs 38
cts 49
cp 0.7755

14 Methods

Rating   Name   Duplication   Size   Complexity  
A setData() 0 3 1
mapDataTypes() 0 1 ?
A select() 0 12 3
A count() 0 6 1
A initInsert() 0 4 1
A initUpdate() 0 3 1
A insert() 0 6 2
A update() 0 6 2
A bulkUpdate() 0 5 1
A delete() 0 5 1
A describe() 0 7 1
A getDefaultInstance() 0 3 1
A getQueryEngine() 0 7 2
A setModel() 0 3 1
1
<?php
2
3
namespace ntentan\nibii;
4
5
use ntentan\utils\Text;
6
use ntentan\atiaa\Db;
7
8
/**
9
 * A DriverAdaptr is a generic database adapter.
10
 * This adapter implements a lot of its operations through the atiaa library.
11
 * Driver specific implementation of this class only handle the conversion of
12
 * data types from the native datatypes of the database to the generic types
13
 * used in the nibii library.
14
 */
15
abstract class DriverAdapter {
16
17
    protected $data;
18
    private $insertQuery;
19
    private $updateQuery;
20
    private $modelInstance;
21
    protected $queryEngine;
22
23
    public function setData($data) {
24
        $this->data = $data;
25
    }
26
27
    /**
28
     * Convert datatypes from the database system's native type to a generic type
29
     * supported by nibii.
30
     *
31
     * @param string $nativeType The native datatype
32
     * @return string The generic datatype for use in nibii.
33
     */
34
    abstract public function mapDataTypes($nativeType);
35
36
    /**
37
     * 
38
     * 
39
     * @param type $parameters
40
     * @return type
41
     */
42 26
    public function select($parameters) {
43 26
        $result = Db::getDriver()->query(
44 26
            $this->getQueryEngine()->getSelectQuery($parameters), 
45 26
            $parameters->getBoundData()
46
        );
47
48 24
        if ($parameters->getFirstOnly() && isset($result[0])) {
49 20
            $result = $result[0];
50
        }
51
52 24
        return $result;
53
    }
54
55
    public function count($parameters) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
56
        $result = Db::getDriver()->query(
57
                $this->getQueryEngine()->getCountQuery($parameters), $parameters->getBoundData()
58
        );
59
        return $result[0]['count'];
60
    }
61
62 4
    private function initInsert() {
63 4
        $this->insertQuery = $this->getQueryEngine()
64 4
                ->getInsertQuery($this->modelInstance);
65 4
    }
66
67 2
    private function initUpdate() {
68 2
        $this->updateQuery = $this->getQueryEngine()->getUpdateQuery($this->modelInstance);
69 2
    }
70
71 4
    public function insert($record) {
72 4
        if ($this->insertQuery === null) {
73 4
            $this->initInsert();
74
        }
75 4
        return Db::getDriver()->query($this->insertQuery, $record);
76
    }
77
78 2
    public function update($record) {
79 2
        if ($this->updateQuery === null) {
80 2
            $this->initUpdate();
81
        }
82 2
        return Db::getDriver()->query($this->updateQuery, $record);
83
    }
84
85 6
    public function bulkUpdate($data, $parameters) {
86 6
        return Db::getDriver()->query(
87 6
                        $this->getQueryEngine()->getBulkUpdateQuery($data, $parameters), array_merge($data, $parameters->getBoundData())
88
        );
89
    }
90
91 2
    public function delete($parameters) {
92 2
        return Db::getDriver()->query(
93 2
                        $this->getQueryEngine()->getDeleteQuery($parameters), $parameters->getBoundData()
94
        );
95
    }
96
97
    public function describe($model, $relationships) {
0 ignored issues
show
Unused Code introduced by
The parameter $model is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
98
        return new ModelDescription(
99
                Db::getDriver()->describeTable($table)[$table], $relationships, function($type) {
0 ignored issues
show
Bug introduced by
The variable $table does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Unused Code introduced by
The call to ModelDescription::__construct() has too many arguments starting with $relationships.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
100
            return $this->mapDataTypes($type);
101
        }
102
        );
103
    }
104
105 38
    public static function getDefaultInstance() {
106 38
        return \ntentan\panie\InjectionContainer::resolve(DriverAdapter::class);
107
    }
108
109
    /**
110
     *
111
     * @return \ntentan\nibii\QueryEngine
112
     */
113 34
    private function getQueryEngine() {
114 34
        if ($this->queryEngine === null) {
115 34
            $this->queryEngine = new QueryEngine();
116 34
            $this->queryEngine->setDriver(Db::getDriver());
117
        }
118 34
        return $this->queryEngine;
119
    }
120
121 38
    public function setModel($model) {
122 38
        $this->modelInstance = $model;
123 38
    }
124
125
}
126