Completed
Push — master ( 978c91...2d50ed )
by James Ekow Abaka
15:58
created

DriverAdapter::reset()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4286
cc 2
eloc 4
nc 2
nop 0
crap 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 34
    public function select($parameters)
45
    {
46 34
        $result = Db::getDriver()->query(
47 34
            $this->getQueryEngine()->getSelectQuery($parameters), 
48 34
            $parameters->getBoundData()
49
        );
50
        
51 34
        if ($parameters->getFirstOnly() && isset($result[0])) {
52 34
            $result = $result[0];
53
        }
54
55 34
        return $result;
56 34
    }
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 24
    }
66
67 24
    private function initInsert()
68 24
    {
69 24
        $this->insertQuery = $this->getQueryEngine()
70 24
            ->getInsertQuery($this->modelInstance);
71
    }
72 24
73 20
    private function initUpdate()
74 20
    {
75
        $this->updateQuery = $this->getQueryEngine()
76 24
            ->getUpdateQuery($this->modelInstance);
77
    }
78
79
    public function insert($record)
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...
80
    {
81
        if($this->insertQuery === null) {
82
            $this->initInsert();
83
        }
84
        return Db::getDriver()->query($this->insertQuery, $record);
85
    }
86
87
    public function update($record)
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...
88 4
    {
89
        if($this->updateQuery === null) {
90 4
            $this->initUpdate();
91 4
        }
92 4
        return Db::getDriver()->query($this->updateQuery, $record);
93
    }
94 2
95 2
    public function bulkUpdate($data, $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...
96 2
    {
97 2
        return Db::getDriver()->query(
98 2
            $this->getQueryEngine()->getBulkUpdateQuery($data, $parameters),
99
            array_merge($data, $parameters->getBoundData())
100 4
        );
101
    }
102 4
103 4
    public function delete($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...
104 4
    {
105 4
        return Db::getDriver()->query(
106
            $this->getQueryEngine()->getDeleteQuery($parameters),
107
            $parameters->getBoundData()
108 2
        );
109
    }
110 2
111 2
    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 2
    {
113 2
        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 6
        );
117
    }
118 6
119 6
    public static function getDefaultInstance()
120 6
    {
121 6
        $driver = Db::getDefaultSettings()['driver'];
122
        if ($driver) {
123
            $class = "\\ntentan\\nibii\\adapters\\" . Text::ucamelize($driver) . "Adapter";
124 2
            $instance = new $class();
125
        } else {
126 2
            throw new \Exception("No datastore specified");
127 2
        }
128 2
        return $instance;
129 2
    }
130
131
    /**
132
     *
133
     * @return \ntentan\nibii\QueryEngine
134
     */
135
    private function getQueryEngine()
136
    {
137
        if ($this->queryEngine === null) {
138
            $this->queryEngine = new QueryEngine();
139
            $this->queryEngine->setDriver(Db::getDriver());
140
        }
141
        return $this->queryEngine;
142
    }
143
144 36
    public function setModel($model)
145
    {
146 36
        $this->modelInstance = $model;
147 36
    }
148
}
149