Completed
Pull Request — master (#2)
by James Ekow Abaka
03:07
created

DriverAdapter   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 76%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 6
dl 0
loc 140
ccs 38
cts 50
cp 0.76
rs 10
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
mapDataTypes() 0 1 ?
A setContext() 0 4 1
A setData() 0 4 1
A select() 0 13 3
A count() 0 5 1
A initInsert() 0 4 1
A initUpdate() 0 4 1
A insert() 0 7 2
A update() 0 7 2
A bulkUpdate() 0 6 1
A delete() 0 6 1
A describe() 0 8 1
A getQueryEngine() 0 8 2
A setModel() 0 4 1
A getDriver() 0 4 1
1
<?php
2
3
namespace ntentan\nibii;
4
5
/**
6
 * DriverAdapter provides a generic interface through which specific database operations can be performed.
7
 * It
8
 */
9
abstract class DriverAdapter
10
{
11
12
    protected $data;
13
    private $insertQuery;
14
    private $updateQuery;
15
    private $modelInstance;
16
    protected $queryEngine;
17
    private $driver;
18
19 36
    public function setContext(ORMContext $context)
20
    {
21 36
        $this->driver = $context->getDbContext()->getDriver();
22 36
    }
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 QueryParameters $parameters
42
     * @return type
43
     */
44 24
    public function select($parameters)
45
    {
46 24
        $result = $this->driver->query(
47 24
            $this->getQueryEngine()->getSelectQuery($parameters),
48 24
            $parameters->getBoundData()
0 ignored issues
show
Bug introduced by
It seems like $parameters->getBoundData() targeting ntentan\nibii\QueryParameters::getBoundData() can also be of type boolean; however, ntentan\atiaa\Driver::query() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
49
        );
50
51 24
        if ($parameters->getFirstOnly() && isset($result[0])) {
52 20
            $result = $result[0];
53
        }
54
55 24
        return $result;
56
    }
57
58
    /**
59
     * @param QueryParameters $parameters
60
     */
61
    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...
62
    {
63
        $result = $this->driver->query($this->getQueryEngine()->getCountQuery($parameters), $parameters->getBoundData());
0 ignored issues
show
Bug introduced by
It seems like $parameters->getBoundData() targeting ntentan\nibii\QueryParameters::getBoundData() can also be of type boolean; however, ntentan\atiaa\Driver::query() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
64
        return $result[0]['count'];
65
    }
66
67 4
    private function initInsert()
68
    {
69 4
        $this->insertQuery = $this->getQueryEngine()->getInsertQuery($this->modelInstance);
70 4
    }
71
72 2
    private function initUpdate()
73
    {
74 2
        $this->updateQuery = $this->getQueryEngine()->getUpdateQuery($this->modelInstance);
75 2
    }
76
77 4
    public function insert($record)
78
    {
79 4
        if ($this->insertQuery === null) {
80 4
            $this->initInsert();
81
        }
82 4
        return $this->driver->query($this->insertQuery, $record);
83
    }
84
85 2
    public function update($record)
86
    {
87 2
        if ($this->updateQuery === null) {
88 2
            $this->initUpdate();
89
        }
90 2
        return $this->driver->query($this->updateQuery, $record);
91
    }
92
93
    /**
94
     * @param QueryParameters $parameters
95
     */
96 6
    public function bulkUpdate($data, $parameters)
97
    {
98 6
        return $this->driver->query(
99 6
            $this->getQueryEngine()->getBulkUpdateQuery($data, $parameters), array_merge($data, $parameters->getBoundData())
100
        );
101
    }
102
103
    /**
104
     * @param QueryParameters $parameters
105
     */
106 2
    public function delete($parameters)
107
    {
108 2
        return $this->driver->query(
109 2
            $this->getQueryEngine()->getDeleteQuery($parameters), $parameters->getBoundData()
0 ignored issues
show
Bug introduced by
It seems like $parameters->getBoundData() targeting ntentan\nibii\QueryParameters::getBoundData() can also be of type boolean; however, ntentan\atiaa\Driver::query() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
110
        );
111
    }
112
113
    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...
114
    {
115
        return new ModelDescription(
116
            $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 $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...
117
            return $this->mapDataTypes($type);
118
        }
119
        );
120
    }
121
122
    /**
123
     *
124
     * @return \ntentan\nibii\QueryEngine
125
     */
126 32
    private function getQueryEngine()
127
    {
128 32
        if ($this->queryEngine === null) {
129 32
            $this->queryEngine = new QueryEngine();
130 32
            $this->queryEngine->setDriver($this->driver);
131
        }
132 32
        return $this->queryEngine;
133
    }
134
135
    /**
136
     * @param RecordWrapper $model
137
     */
138 36
    public function setModel($model)
139
    {
140 36
        $this->modelInstance = $model;
141 36
    }
142
143
    public function getDriver()
144
    {
145
        return $this->driver;
146
    }
147
148
}
149