Completed
Push — master ( 2d50ed...6bb3cd )
by James Ekow Abaka
10:18
created

DriverAdapter   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 79.1%

Importance

Changes 23
Bugs 3 Features 5
Metric Value
wmc 19
c 23
b 3
f 5
lcom 1
cbo 5
dl 0
loc 134
ccs 53
cts 67
cp 0.791
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
mapDataTypes() 0 1 ?
A initInsert() 0 5 1
A bulkUpdate() 0 7 1
A setModel() 0 4 1
A setData() 0 4 1
A select() 0 13 3
A count() 0 8 1
A initUpdate() 0 5 1
A insert() 0 7 2
A update() 0 7 2
A delete() 0 7 1
A describe() 0 7 1
A getDefaultInstance() 0 11 2
A getQueryEngine() 0 8 2
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
18
    protected $data;
19
    private $insertQuery;
20
    private $updateQuery;
21
    private $modelInstance;
22
    protected $queryEngine;
23
24
    public function setData($data)
25
    {
26
        $this->data = $data;
27
    }
28
29
    /**
30
     * Convert datatypes from the database system's native type to a generic type
31
     * supported by nibii.
32
     *
33
     * @param string $nativeType The native datatype
34
     * @return string The generic datatype for use in nibii.
35
     */
36
    abstract public function mapDataTypes($nativeType);
37
38
    /**
39
     * 
40
     * 
41
     * @param type $parameters
42
     * @return type
43
     */
44 24
    public function select($parameters)
45
    {
46 24
        $result = Db::getDriver()->query(
47 24
            $this->getQueryEngine()->getSelectQuery($parameters), 
48 24
            $parameters->getBoundData()
49 24
        );
50
        
51 24
        if ($parameters->getFirstOnly() && isset($result[0])) {
52 20
            $result = $result[0];
53 20
        }
54
55 24
        return $result;
56
    }
57
    
58
    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...
59
    {
60
        $result = Db::getDriver()->query(
61
            $this->getQueryEngine()->getCountQuery($parameters),
62
            $parameters->getBoundData()
63
        );
64
        return $result[0]['count'];
65
    }
66
67 4
    private function initInsert()
68
    {
69 4
        $this->insertQuery = $this->getQueryEngine()
70 4
            ->getInsertQuery($this->modelInstance);
71 4
    }
72
73 2
    private function initUpdate()
74
    {
75 2
        $this->updateQuery = $this->getQueryEngine()
76 2
            ->getUpdateQuery($this->modelInstance);
77 2
    }
78
79 4
    public function insert($record)
80
    {
81 4
        if($this->insertQuery === null) {
82 4
            $this->initInsert();
83 4
        }
84 4
        return Db::getDriver()->query($this->insertQuery, $record);
85
    }
86
87 2
    public function update($record)
88
    {
89 2
        if($this->updateQuery === null) {
90 2
            $this->initUpdate();
91 2
        }
92 2
        return Db::getDriver()->query($this->updateQuery, $record);
93
    }
94
95 6
    public function bulkUpdate($data, $parameters)
96
    {
97 6
        return Db::getDriver()->query(
98 6
            $this->getQueryEngine()->getBulkUpdateQuery($data, $parameters),
99 6
            array_merge($data, $parameters->getBoundData())
100 6
        );
101
    }
102
103 2
    public function delete($parameters)
104
    {
105 2
        return Db::getDriver()->query(
106 2
            $this->getQueryEngine()->getDeleteQuery($parameters),
107 2
            $parameters->getBoundData()
108 2
        );
109
    }
110
111
    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...
112
    {
113
        return new ModelDescription(
114
            Db::getDriver()->describeTable($table)[$table],
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...
115
            $relationships, function($type) { return $this->mapDataTypes($type); }
0 ignored issues
show
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...
116
        );
117
    }
118
119 34
    public static function getDefaultInstance()
120
    {
121 34
        $driver = Db::getDefaultSettings()['driver'];
122 34
        if ($driver) {
123 34
            $class = "\\ntentan\\nibii\\adapters\\" . Text::ucamelize($driver) . "Adapter";
124 34
            $instance = new $class();
125 34
        } else {
126
            throw new \Exception("No datastore specified");
127
        }
128 34
        return $instance;
129
    }
130
131
    /**
132
     *
133
     * @return \ntentan\nibii\QueryEngine
134
     */
135 32
    private function getQueryEngine()
136
    {
137 32
        if ($this->queryEngine === null) {
138 32
            $this->queryEngine = new QueryEngine();
139 32
            $this->queryEngine->setDriver(Db::getDriver());
140 32
        }
141 32
        return $this->queryEngine;
142
    }
143
144 10
    public function setModel($model)
145
    {
146 10
        $this->modelInstance = $model;
147 10
    }
148
}
149