DriverAdapter   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 76%

Importance

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

15 Methods

Rating   Name   Duplication   Size   Complexity  
A setContext() 0 4 1
A setData() 0 4 1
mapDataTypes() 0 1 ?
A select() 0 13 3
A count() 0 6 1
A initInsert() 0 4 1
A initUpdate() 0 4 1
A insert() 0 8 2
A update() 0 8 2
A bulkUpdate() 0 6 1
A delete() 0 6 1
A describe() 0 8 1
A getQueryEngine() 0 9 2
A setModel() 0 4 1
A getDriver() 0 4 1
1
<?php
2
3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2014-2018 James Ekow Abaka Ainooson
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
namespace ntentan\nibii;
28
29
/**
30
 * DriverAdapter provides a generic interface through which specific database operations can be performed.
31
 * It.
32
 */
33
abstract class DriverAdapter
34
{
35
    protected $data;
36
    private $insertQuery;
37
    private $updateQuery;
38
    private $modelInstance;
39
    protected $queryEngine;
40
    private $driver;
41
42 36
    public function setContext(ORMContext $context)
43
    {
44 36
        $this->driver = $context->getDbContext()->getDriver();
45 36
    }
46
47
    public function setData($data)
48
    {
49
        $this->data = $data;
50
    }
51
52
    /**
53
     * Convert datatypes from the database system's native type to a generic type
54
     * supported by nibii.
55
     *
56
     * @param string $nativeType The native datatype
57
     *
58
     * @return string The generic datatype for use in nibii.
59
     */
60
    abstract public function mapDataTypes($nativeType);
61
62
    /**
63
     * @param QueryParameters $parameters
64
     *
65
     * @return type
66
     */
67 24
    public function select($parameters)
68
    {
69 24
        $result = $this->driver->query(
70 24
            $this->getQueryEngine()->getSelectQuery($parameters),
71 24
            $parameters->getBoundData()
72
        );
73
74 24
        if ($parameters->getFirstOnly() && isset($result[0])) {
75 20
            $result = $result[0];
76
        }
77
78 24
        return $result;
79
    }
80
81
    /**
82
     * @param QueryParameters $parameters
83
     */
84
    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...
85
    {
86
        $result = $this->driver->query($this->getQueryEngine()->getCountQuery($parameters), $parameters->getBoundData());
87
88
        return $result[0]['count'];
89
    }
90
91 4
    private function initInsert()
92
    {
93 4
        $this->insertQuery = $this->getQueryEngine()->getInsertQuery($this->modelInstance);
94 4
    }
95
96 2
    private function initUpdate()
97
    {
98 2
        $this->updateQuery = $this->getQueryEngine()->getUpdateQuery($this->modelInstance);
99 2
    }
100
101 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...
102
    {
103 4
        if ($this->insertQuery === null) {
104 4
            $this->initInsert();
105
        }
106
107 4
        return $this->driver->query($this->insertQuery, $record);
108
    }
109
110 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...
111
    {
112 2
        if ($this->updateQuery === null) {
113 2
            $this->initUpdate();
114
        }
115
116 2
        return $this->driver->query($this->updateQuery, $record);
117
    }
118
119
    /**
120
     * @param QueryParameters $parameters
121
     */
122 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...
123
    {
124 6
        return $this->driver->query(
125 6
            $this->getQueryEngine()->getBulkUpdateQuery($data, $parameters), array_merge($data, $parameters->getBoundData())
126
        );
127
    }
128
129
    /**
130
     * @param QueryParameters $parameters
131
     */
132 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...
133
    {
134 2
        return $this->driver->query(
135 2
            $this->getQueryEngine()->getDeleteQuery($parameters), $parameters->getBoundData()
136
        );
137
    }
138
139
    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...
140
    {
141
        return new ModelDescription(
142
            $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...
143
                return $this->mapDataTypes($type);
144
            }
145
        );
146
    }
147
148
    /**
149
     * @return \ntentan\nibii\QueryEngine
150
     */
151 32
    private function getQueryEngine()
152
    {
153 32
        if ($this->queryEngine === null) {
154 32
            $this->queryEngine = new QueryEngine();
155 32
            $this->queryEngine->setDriver($this->driver);
156
        }
157
158 32
        return $this->queryEngine;
159
    }
160
161
    /**
162
     * @param RecordWrapper $model
163
     */
164 36
    public function setModel($model)
165
    {
166 36
        $this->modelInstance = $model;
167 36
    }
168
169
    public function getDriver()
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...
170
    {
171
        return $this->driver;
172
    }
173
}
174