Passed
Pull Request — master (#3)
by Joao
04:02
created

Route::addDbDriverInterface()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 11
cts 11
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 8
nop 2
crap 4
1
<?php
2
3
namespace ByJG\AnyDataset\Store;
4
5
use ByJG\AnyDataset\DbDriverInterface;
6
use ByJG\AnyDataset\Exception\NotImplementedException;
7
use ByJG\AnyDataset\Exception\RouteNotFoundException;
8
use ByJG\AnyDataset\Exception\RouteNotMatchedException;
9
use ByJG\AnyDataset\Factory;
10
11
class Route implements DbDriverInterface
12
{
13
14
    /**
15
     * @var DbDriverInterface[]
16
     */
17
    protected $dbDriverInterface = [];
18
19
    /**
20
     * @var string[]
21
     */
22
    protected $routes;
23
24
    /**
25
     * Route constructor.
26
     */
27 15
    public function __construct()
28
    {
29 15
    }
30
31
    //<editor-fold desc="Route Methods">
32
33
    /**
34
     * @param string $routeName
35
     * @param DbDriverInterface[]|DbDriverInterface|string|string[] $dbDriver
36
     * @return \ByJG\AnyDataset\Store\Route
37
     */
38 15
    public function addDbDriverInterface($routeName, $dbDriver)
39
    {
40 15
        if (!isset($this->dbDriverInterface[$routeName])) {
41 15
            $this->dbDriverInterface[$routeName] = [];
42 15
        }
43
44 15
        if (!is_array($dbDriver)) {
45 15
            $dbDriver = [$dbDriver];
46 15
        }
47
48 15
        foreach ($dbDriver as $item) {
49 15
            $this->dbDriverInterface[$routeName][] = $item;
50 15
        }
51
52 15
        return $this;
53
    }
54
55
    /**
56
     * @param $routeName
57
     * @param null $table
58
     * @return \ByJG\AnyDataset\Store\Route
59
     * @throws \ByJG\AnyDataset\Exception\RouteNotFoundException
60
     */
61 5 View Code Duplication
    public function addRouteForSelect($routeName, $table = null)
0 ignored issues
show
Duplication introduced by
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...
62
    {
63 5
        if (empty($table)) {
64 2
            $table = '\w+';
65 2
        }
66 5
        return $this->addCustomRoute($routeName, '^select.*from\s+([`]?' . $table . '[`]?)\s');
67
    }
68
69
    /**
70
     * @param $routeName
71
     * @param null $table
72
     * @return \ByJG\AnyDataset\Store\Route
73
     * @throws \ByJG\AnyDataset\Exception\RouteNotFoundException
74
     */
75 5 View Code Duplication
    public function addRouteForInsert($routeName, $table = null)
0 ignored issues
show
Duplication introduced by
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...
76
    {
77 5
        if (empty($table)) {
78 2
            $table = '\w+';
79 2
        }
80 5
        return $this->addCustomRoute($routeName, '^insert\s+into\s+([`]?' . $table . '[`]?)\s+\(');
81
    }
82
83
    /**
84
     * @param $routeName
85
     * @param null $table
86
     * @return \ByJG\AnyDataset\Store\Route
87
     * @throws \ByJG\AnyDataset\Exception\RouteNotFoundException
88
     */
89 5 View Code Duplication
    public function addRouteForUpdate($routeName, $table = null)
0 ignored issues
show
Duplication introduced by
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...
90
    {
91 5
        if (empty($table)) {
92 2
            $table = '\w+';
93 2
        }
94 5
        return $this->addCustomRoute($routeName, '^update\s+([`]?' . $table . '[`]?)\s+set');
95
    }
96
97
    /**
98
     * @param $routeName
99
     * @param null $table
100
     * @return \ByJG\AnyDataset\Store\Route
101
     * @throws \ByJG\AnyDataset\Exception\RouteNotFoundException
102
     */
103 5 View Code Duplication
    public function addRouteForDelete($routeName, $table = null)
0 ignored issues
show
Duplication introduced by
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...
104
    {
105 5
        if (empty($table)) {
106 2
            $table = '\w+';
107 2
        }
108 5
        return $this->addCustomRoute($routeName, '^delete\s+(from\s+)?([`]?' . $table . '[`]?)\s');
109
    }
110
111
    /**
112
     * @param $routeName
113
     * @param $table
114
     * @return \ByJG\AnyDataset\Store\Route
115
     * @throws \ByJG\AnyDataset\Exception\RouteNotFoundException
116
     */
117 1
    public function addRouteForTable($routeName, $table)
118
    {
119 1
        $this->addRouteForRead($routeName, $table);
120 1
        $this->addRouteForWrite($routeName, $table);
121 1
        return $this;
122
    }
123
124
    /**
125
     * @param $routeName
126
     * @param null $table
127
     * @return \ByJG\AnyDataset\Store\Route
128
     * @throws \ByJG\AnyDataset\Exception\RouteNotFoundException
129
     */
130 3
    public function addRouteForWrite($routeName, $table = null)
131
    {
132 3
        $this->addRouteForInsert($routeName, $table);
133 3
        $this->addRouteForUpdate($routeName, $table);
134 3
        $this->addRouteForDelete($routeName, $table);
135 3
        return $this;
136
    }
137
138
    /**
139
     * @param $routeName
140
     * @param null $table
141
     * @return \ByJG\AnyDataset\Store\Route
142
     * @throws \ByJG\AnyDataset\Exception\RouteNotFoundException
143
     */
144 3
    public function addRouteForRead($routeName, $table = null)
145
    {
146 3
        return $this->addRouteForSelect($routeName, $table);
147
    }
148
149
    /**
150
     * @param $routeName
151
     * @param $field
152
     * @param $value
153
     * @return \ByJG\AnyDataset\Store\Route
154
     * @throws \ByJG\AnyDataset\Exception\RouteNotFoundException
155
     */
156 2
    public function addRouteForFilter($routeName, $field, $value)
157
    {
158 2
        return $this->addCustomRoute($routeName, "\\s`?$field`?\\s*=\\s*'?$value'?\s");
159
    }
160
161
    /**
162
     * @param $routeName
163
     * @return \ByJG\AnyDataset\Store\Route
164
     * @throws \ByJG\AnyDataset\Exception\RouteNotFoundException
165
     */
166 1
    public function addDefaultRoute($routeName)
167
    {
168 1
        return $this->addCustomRoute($routeName, '.');
169
    }
170
171
    /**
172
     * @param $routeName
173
     * @param $regEx
174
     * @return \ByJG\AnyDataset\Store\Route
175
     * @throws \ByJG\AnyDataset\Exception\RouteNotFoundException
176
     */
177 13
    public function addCustomRoute($routeName, $regEx)
178
    {
179 13
        if (!isset($this->dbDriverInterface[$routeName])) {
180
            throw new RouteNotFoundException("Invalid route $routeName");
181
        }
182 13
        $this->routes[$regEx] = $routeName;
183 13
        return $this;
184
    }
185
186
    /**
187
     * @param $sql
188
     * @return DbDriverInterface
189
     * @throws \ByJG\AnyDataset\Exception\RouteNotMatchedException
190
     */
191 13
    public function matchRoute($sql)
192
    {
193 13
        $sql = trim(strtolower(str_replace("\n", " ", $sql))) . ' ';
194 13
        foreach ($this->routes as $pattern => $routeName) {
195 13
            if (!preg_match("/$pattern/", $sql)) {
196 12
                continue;
197
            }
198
199 8
            $dbDriver = $this->dbDriverInterface[$routeName][rand(0, count($this->dbDriverInterface[$routeName])-1)];
200 8
            if (is_string($dbDriver)) {
201 8
                return Factory::getDbRelationalInstance($dbDriver);
202
            }
203
204 5
            return $dbDriver;
205 5
        }
206
207 5
        throw new RouteNotMatchedException('Route not matched');
208
    }
209
    //</editor-fold>
210
211
    //<editor-fold desc="DbDriverInterface">
212
213
    /**
214
     * @param string $sql
215
     * @param null $params
216
     * @return \ByJG\AnyDataset\Dataset\GenericIterator
217
     * @throws \ByJG\AnyDataset\Exception\RouteNotMatchedException
218
     */
219
    public function getIterator($sql, $params = null)
220
    {
221
        $dbDriver = $this->matchRoute($sql);
222
        return $dbDriver->getIterator($sql, $params);
223
    }
224
225
    /**
226
     * @param $sql
227
     * @param null $array
228
     * @return mixed
229
     * @throws \ByJG\AnyDataset\Exception\RouteNotMatchedException
230
     */
231
    public function getScalar($sql, $array = null)
232
    {
233
        $dbDriver = $this->matchRoute($sql);
234
        return $dbDriver->getScalar($sql, $array);
235
    }
236
237
    /**
238
     * @param $tablename
239
     * @throws \ByJG\AnyDataset\Exception\NotImplementedException
240
     */
241
    public function getAllFields($tablename)
242
    {
243
        throw new NotImplementedException('Feature not available');
244
    }
245
246
    /**
247
     * @param $sql
248
     * @param null $array
249
     * @return mixed
250
     * @throws \ByJG\AnyDataset\Exception\RouteNotMatchedException
251
     */
252
    public function execute($sql, $array = null)
253
    {
254
        $dbDriver = $this->matchRoute($sql);
255
        return $dbDriver->execute($sql, $array);
256
    }
257
258
    /**
259
     * @throws \ByJG\AnyDataset\Exception\NotImplementedException
260
     */
261
    public function beginTransaction()
262
    {
263
        throw new NotImplementedException('Feature not available');
264
    }
265
266
    /**
267
     * @throws \ByJG\AnyDataset\Exception\NotImplementedException
268
     */
269
    public function commitTransaction()
270
    {
271
        throw new NotImplementedException('Feature not available');
272
    }
273
274
    /**
275
     * @throws \ByJG\AnyDataset\Exception\NotImplementedException
276
     */
277
    public function rollbackTransaction()
278
    {
279
        throw new NotImplementedException('Feature not available');
280
    }
281
282
    /**
283
     * @return \PDO|void
284
     * @throws \ByJG\AnyDataset\Exception\NotImplementedException
285
     */
286
    public function getDbConnection()
287
    {
288
        throw new NotImplementedException('Feature not available');
289
    }
290
291
    /**
292
     * @param $name
293
     * @param $value
294
     * @throws \ByJG\AnyDataset\Exception\NotImplementedException
295
     */
296
    public function setAttribute($name, $value)
297
    {
298
        throw new NotImplementedException('Feature not available');
299
    }
300
301
    /**
302
     * @param $name
303
     * @throws \ByJG\AnyDataset\Exception\NotImplementedException
304
     */
305
    public function getAttribute($name)
306
    {
307
        throw new NotImplementedException('Feature not available');
308
    }
309
310
    /**
311
     * @param $sql
312
     * @param null $array
313
     * @return mixed
314
     * @throws \ByJG\AnyDataset\Exception\RouteNotMatchedException
315
     */
316
    public function executeAndGetId($sql, $array = null)
317
    {
318
        $dbDriver = $this->matchRoute($sql);
319
        return $dbDriver->executeAndGetId($sql, $array);
320
    }
321
322
    /**
323
     * @return \ByJG\AnyDataset\DbFunctionsInterface|void
324
     * @throws \ByJG\AnyDataset\Exception\NotImplementedException
325
     */
326
    public function getDbHelper()
327
    {
328
        throw new NotImplementedException('Feature not available');
329
    }
330
331
    /**
332
     * @return void
333
     * @throws \ByJG\AnyDataset\Exception\NotImplementedException
334
     */
335
    public function getUri()
336
    {
337
        throw new NotImplementedException('Feature not available');
338
    }
339
340
    /**
341
     * @throws \ByJG\AnyDataset\Exception\NotImplementedException
342
     */
343
    public function isSupportMultRowset()
344
    {
345
        throw new NotImplementedException('Feature not available');
346
    }
347
348
    /**
349
     * @param $multipleRowSet
350
     * @throws \ByJG\AnyDataset\Exception\NotImplementedException
351
     */
352
    public function setSupportMultRowset($multipleRowSet)
353
    {
354
        throw new NotImplementedException('Feature not available');
355
    }
356
    //</editor-fold>
357
}
358