Completed
Push — master ( be821e...418a4d )
by James Ekow Abaka
03:23
created

DriverAdapter::setModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
eloc 2
nc 1
nop 1
crap 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
    private $driver;
23
    
24 37
    public function setContext(Context $context) {
25 37
        $this->driver = $context->getDbContext()->getDriver();
26 37
    }
27
28
    public function setData($data) {
29
        $this->data = $data;
30
    }
31
32
    /**
33
     * Convert datatypes from the database system's native type to a generic type
34
     * supported by nibii.
35
     *
36
     * @param string $nativeType The native datatype
37
     * @return string The generic datatype for use in nibii.
38
     */
39
    abstract public function mapDataTypes($nativeType);
40
41
    /**
42
     * 
43
     * 
44
     * @param type $parameters
45
     * @return type
46
     */
47 25
    public function select($parameters) {
48 25
        $result = $this->driver->query(
49 25
            $this->getQueryEngine()->getSelectQuery($parameters), 
50 25
            $parameters->getBoundData()
51
        );
52
53 25
        if ($parameters->getFirstOnly() && isset($result[0])) {
54 20
            $result = $result[0];
55
        }
56
57 25
        return $result;
58
    }
59
60
    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...
61
        $result = $this->driver->query(
62
            $this->getQueryEngine()->getCountQuery($parameters), $parameters->getBoundData()
63
        );
64
        return $result[0]['count'];
65
    }
66
67 4
    private function initInsert() {
68 4
        $this->insertQuery = $this->getQueryEngine()
69 4
            ->getInsertQuery($this->modelInstance);
70 4
    }
71
72 2
    private function initUpdate() {
73 2
        $this->updateQuery = $this->getQueryEngine()->getUpdateQuery($this->modelInstance);
74 2
    }
75
76 4
    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...
77 4
        if ($this->insertQuery === null) {
78 4
            $this->initInsert();
79
        }
80 4
        return $this->driver->query($this->insertQuery, $record);
81
    }
82
83 2
    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...
84 2
        if ($this->updateQuery === null) {
85 2
            $this->initUpdate();
86
        }
87 2
        return $this->driver->query($this->updateQuery, $record);
88
    }
89
90 6
    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...
91 6
        return $this->driver->query(
92 6
            $this->getQueryEngine()->getBulkUpdateQuery($data, $parameters), array_merge($data, $parameters->getBoundData())
93
        );
94
    }
95
96 2
    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...
97 2
        return $this->driver->query(
98 2
            $this->getQueryEngine()->getDeleteQuery($parameters), $parameters->getBoundData()
99
        );
100
    }
101
102
    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...
103
        return new ModelDescription(
104
            $this->driver->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 function ($type) { r...>mapDataTypes($type); }.

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...
105
                return $this->mapDataTypes($type);
106
            }
107
        );
108
    }
109
110
    /**
111
     *
112
     * @return \ntentan\nibii\QueryEngine
113
     */
114 33
    private function getQueryEngine() {
115 33
        if ($this->queryEngine === null) {
116 33
            $this->queryEngine = new QueryEngine();
117 33
            $this->queryEngine->setDriver($this->driver);
118
        }
119 33
        return $this->queryEngine;
120
    }
121
122 37
    public function setModel($model) {
123 37
        $this->modelInstance = $model;
124 37
    }
125
126
}
127