Completed
Push — master ( a2888f...6d16ff )
by Joao Gilberto
04:13
created

DBDataset::__construct()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 18
ccs 0
cts 15
cp 0
rs 8.8571
cc 5
eloc 11
nc 9
nop 1
crap 30
1
<?php
2
3
namespace ByJG\AnyDataset\Repository;
4
5
use ByJG\AnyDataset\ConnectionManagement;
6
use ByJG\AnyDataset\Database\DBDriverInterface;
7
use ByJG\AnyDataset\Database\DBFunctionsInterface;
8
use ByJG\AnyDataset\Database\DBOci8Driver;
9
use ByJG\AnyDataset\Database\DBPDODriver;
10
use ByJG\AnyDataset\Database\DBSQLRelayDriver;
11
use ByJG\AnyDataset\Exception\NotAvailableException;
12
use ByJG\Cache\CacheEngineInterface;
13
use PDO;
14
15
class DBDataset
16
{
17
18
    /**
19
     * Enter description here...
20
     *
21
     * @var ConnectionManagement
22
     */
23
    protected $_connectionManagement;
24
25
    /**
26
     *
27
     * @var DBDriverInterface
28
     */
29
    private $_dbDriver = null;
30
31
32
    /**
33
     * @var CacheEngineInterface
34
     */
35
    protected $_cacheEngine;
36
37
    /**
38
     * @param ConnectionManagement|string $dbname Name of file without '_db' and extention '.xml'.
39
     */
40
    public function __construct($dbname)
41
    {
42
        // Create the object ConnectionManagement
43
        if (is_string($dbname)) {
44
            $this->_connectionManagement = new ConnectionManagement($dbname);
45
        } elseif ($dbname instanceof ConnectionManagement) {
46
            $this->_connectionManagement = $dbname;
47
        }
48
49
        // Create the proper driver
50
        if ($this->_connectionManagement->getDriver() == "sqlrelay") {
51
            $this->_dbDriver = new DBSQLRelayDriver($this->_connectionManagement);
52
        } elseif ($this->_connectionManagement->getDriver() == "oci8") {
53
            $this->_dbDriver = new DBOci8Driver($this->_connectionManagement);
54
        } else {
55
            $this->_dbDriver = DBPDODriver::factory($this->_connectionManagement);
56
        }
57
    }
58
59
    /**
60
     * @return ConnectionManagement
61
     */
62
    public function getConnectionManagement() 
63
    {
64
        return $this->_connectionManagement;
65
    }
66
67
    public function testConnection()
68
    {
69
        return true;
70
    }
71
72
73
    public function setCacheEngine(CacheEngineInterface $cache)
74
    {
75
        $this->_cacheEngine = $cache;
76
    }
77
78
    /**
79
     * @return CacheEngineInterface
80
     * @throws NotAvailableException
81
     */
82
    public function getCacheEngine()
83
    {
84
        if (is_null($this->_cacheEngine)) {
85
            throw new NotAvailableException('Cache Engine not available');
86
        }
87
        return $this->_cacheEngine;
88
    }
89
90
    /**
91
     * Get the DBDriver
92
     * @return DBDriverInterface
93
     */
94
    public function getDbDriver()
95
    {
96
        return $this->_dbDriver;
97
    }
98
99
    /**
100
     * @access public
101
     * @param string $sql
102
     * @param array $params
103
     * @param int $ttl
104
     * @return IteratorInterface
105
     * @throws NotAvailableException
106
     */
107
    public function getIterator($sql, $params = null, $ttl = null)
108
    {
109
        // If there is no TTL query, return the LIVE iterator
110
        if (empty($ttl)) {
111
            return $this->getDbDriver()->getIterator($sql, $params);
112
        }
113
114
        // Otherwise try to get from cache
115
        $key = $this->getQueryKey($sql, $params);
116
117
        // Get the CACHE
118
        $cache = $this->getCacheEngine()->get($key, $ttl);
119
        if ($cache === false) {
120
            $cache = array();
121
            $it = $this->getDbDriver()->getIterator($sql, $params);
122
            foreach ($it as $value) {
123
                $cache[] = $value->toArray();
124
            }
125
126
            $this->getCacheEngine()->set($key, $cache, $ttl);
0 ignored issues
show
Documentation introduced by Joao Gilberto Magalhaes
$cache is of type array, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
127
        }
128
129
        $arrayDS = new ArrayDataset($cache);
0 ignored issues
show
Bug introduced by Joao Gilberto Magalhaes
It seems like $cache defined by $this->getCacheEngine()->get($key, $ttl) on line 118 can also be of type object; however, ByJG\AnyDataset\Reposito...yDataset::__construct() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
130
        return $arrayDS->getIterator();
131
    }
132
133
    protected function getQueryKey($sql, $array)
134
    {
135
        $key1 = md5($sql);
136
137
        // Check which parameter exists in the SQL
138
        $arKey2 = array();
139
        if (is_array($array)) {
140
            foreach ($array as $key => $value) {
141
                if (preg_match("/\[\[$key\]\]/", $sql)) {
142
                    $arKey2[$key] = $value;
143
                }
144
            }
145
        }
146
147
        // Define the query key
148
        if (is_array($arKey2) && count($arKey2) > 0) {
149
            $key2 = ":" . md5(json_encode($arKey2));
150
        } else {
151
            $key2 = "";
152
        }
153
154
        return  "qry:" . $key1 . $key2;
155
    }
156
157
    public function getScalar($sql, $array = null)
158
    {
159
        return $this->getDbDriver()->getScalar($sql, $array);
160
    }
161
162
    /**
163
     * @access public
164
     * @param string $tablename
165
     * @return array
166
     */
167
    public function getAllFields($tablename)
168
    {
169
        return $this->getDbDriver()->getAllFields($tablename);
170
    }
171
172
    /**
173
     * @access public
174
     * @param string $sql
175
     * @param array $array
176
     * @return Resource
177
     */
178
    public function execSQL($sql, $array = null)
179
    {
180
        $this->getDbDriver()->executeSql($sql, $array);
181
    }
182
183
    public function beginTransaction()
184
    {
185
        $this->getDbDriver()->beginTransaction();
186
    }
187
188
    public function commitTransaction()
189
    {
190
        $this->getDbDriver()->commitTransaction();
191
    }
192
193
    public function rollbackTransaction()
194
    {
195
        $this->getDbDriver()->rollbackTransaction();
196
    }
197
198
    /**
199
     * @access public
200
     * @param IteratorInterface $it
201
     * @param string $fieldPK
202
     * @param string $fieldName
203
     * @return Resource
204
     */
205 View Code Duplication
    public function getArrayField(IteratorInterface $it, $fieldPK, $fieldName)
0 ignored issues
show
Duplication introduced by João Gilberto Magalhães
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
206
    {
207
        $result = array();
208
        while ($it->hasNext()) {
209
            $registro = $it->moveNext();
210
            $result [$registro->getField($fieldPK)] = $registro->getField($fieldName);
211
        }
212
        return $result;
213
    }
214
215
    /**
216
     * @access public
217
     * @return PDO
218
     */
219
    public function getDBConnection()
220
    {
221
        return $this->getDbDriver()->getDbConnection();
222
    }
223
224
    /**
225
     *
226
     * @var DBFunctionsInterface
227
     */
228
    protected $_dbFunction = null;
229
230
    /**
231
     * Get a IDbFunctions class to execute Database specific operations.
232
     * @return DBFunctionsInterface
233
     */
234
    public function getDbFunctions()
235
    {
236
        if (is_null($this->_dbFunction)) {
237
            $dbFunc = "\\ByJG\\AnyDataset\\Database\\DB" . ucfirst($this->_connectionManagement->getDriver()) . "Functions";
238
            $this->_dbFunction = new $dbFunc();
239
        }
240
241
        return $this->_dbFunction;
242
    }
243
244
    public function setDriverAttribute($name, $value)
245
    {
246
        return $this->getDbDriver()->setAttribute($name, $value);
247
    }
248
249
    public function getDriverAttribute($name)
250
    {
251
        return $this->getDbDriver()->getAttribute($name);
252
    }
253
}
254