Passed
Branch master (ebaa1a)
by Ruben
03:45
created
src/Persistence.php 2 patches
Spacing   +108 added lines, -108 removed lines patch added patch discarded remove patch
@@ -20,34 +20,34 @@  discard block
 block discarded – undo
20 20
  * @copyright 2018 Ruben Dorado
21 21
  * @license   http://www.opensource.org/licenses/MIT The MIT License
22 22
  */
23
-class Persistence{
23
+class Persistence {
24 24
 
25 25
     /*
26 26
      * @param Configuration $config
27 27
      *
28 28
      * @return PDO
29 29
      */
30
-    public static function getPDO($config){
30
+    public static function getPDO($config) {
31 31
         $options = array(
32 32
             PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
33 33
         );
34 34
         
35
-        if($config->getDsn()){
36
-            try{
37
-                return new PDO($config->getDsn(),$config->getUser(),$config->getPassword(),$options);
35
+        if ($config->getDsn()) {
36
+            try {
37
+                return new PDO($config->getDsn(), $config->getUser(), $config->getPassword(), $options);
38 38
             }
39
-            catch(Exception $e){                
40
-                if(!$config->getUseOnMemoryDB()){
39
+            catch (Exception $e) {                
40
+                if (!$config->getUseOnMemoryDB()) {
41 41
                     throw new Exception("Could not create a db connection. Check permissions, configuration, and documentation. ".$e->getMessage());
42 42
                 }
43 43
             }
44 44
         }
45 45
         
46
-        if($config->getUseOnMemoryDB()){
47
-            try{
48
-                return new PDO("sqlite::memory:",$options);
46
+        if ($config->getUseOnMemoryDB()) {
47
+            try {
48
+                return new PDO("sqlite::memory:", $options);
49 49
             }
50
-            catch(Exception $e){
50
+            catch (Exception $e) {
51 51
                 throw new Exception("Could not create a db connection. Check permissions, configuration, and documentation. ".$e->getMessage());                
52 52
             }
53 53
         }
@@ -61,8 +61,8 @@  discard block
 block discarded – undo
61 61
      * @param $config Configuration
62 62
      *
63 63
      */
64
-    public static function crateDatabase($pdo, $config){
65
-        try{
64
+    public static function crateDatabase($pdo, $config) {
65
+        try {
66 66
 
67 67
             $db_hit_table = $config->getHitTableName();
68 68
             $db_options_table = $config->getOptionsTableName();           
@@ -78,7 +78,7 @@  discard block
 block discarded – undo
78 78
             $stmt = $pdo->prepare("CREATE TABLE $db_url_table (id VARCHAR(255), url VARCHAR(255), count INT)");
79 79
             $stmt->execute();
80 80
         }
81
-        catch(Exception $e){
81
+        catch (Exception $e) {
82 82
             throw new Exception("Could not create the database. ".$e->getMessage());
83 83
         }        
84 84
         return true;
@@ -91,7 +91,7 @@  discard block
 block discarded – undo
91 91
      * @param $config Configuration
92 92
      *
93 93
      */
94
-    public static function deleteDatabase($pdo, $config){
94
+    public static function deleteDatabase($pdo, $config) {
95 95
         $resp = true;
96 96
         
97 97
         $db_hit_table = $config->getHitTableName();
@@ -113,14 +113,14 @@  discard block
 block discarded – undo
113 113
      * @param $config Configuration
114 114
      *
115 115
      */
116
-    private static function dropTable($pdo, $tableName){
117
-        try{
116
+    private static function dropTable($pdo, $tableName) {
117
+        try {
118 118
             
119 119
             $stmt = $pdo->prepare("DROP TABLE $tableName");
120 120
             $stmt->execute();
121 121
            
122 122
         }
123
-        catch(Exception $e){
123
+        catch (Exception $e) {
124 124
             throw new Exception("Problem deleting the table $tableName. ".$e->getMessage());
125 125
         }
126 126
         return true;
@@ -132,16 +132,16 @@  discard block
 block discarded – undo
132 132
      * @param $config Configuration
133 133
      *
134 134
      */
135
-    public static function checkTables($pdo, $config){
135
+    public static function checkTables($pdo, $config) {
136 136
         $resp = true;      
137
-        try{
137
+        try {
138 138
             
139 139
             $resp = $resp && Persistence::checkHitTable($pdo, $config);
140 140
             $resp = $resp && Persistence::checkOptionsTable($pdo, $config);
141 141
             $resp = $resp && Persistence::checkFromTable($pdo, $config);
142 142
             $resp = $resp && Persistence::checkUrlTable($pdo, $config);
143 143
         }
144
-        catch(Exception $e){
144
+        catch (Exception $e) {
145 145
             return false;
146 146
         }        
147 147
         return $resp;
@@ -152,14 +152,14 @@  discard block
 block discarded – undo
152 152
      * @param $pdo PDO
153 153
      * @param $config Configuration
154 154
      */
155
-    public static function checkFromTable($pdo, $config){
156
-        try{
155
+    public static function checkFromTable($pdo, $config) {
156
+        try {
157 157
             $db_from_table = $config->getFromTableName();
158 158
             $stmt = $pdo->prepare("SELECT * FROM $db_from_table WHERE 1==0");
159 159
             $stmt->execute();
160 160
             
161 161
         }
162
-        catch(Exception $e){
162
+        catch (Exception $e) {
163 163
             return false;
164 164
         }
165 165
         return true;
@@ -169,13 +169,13 @@  discard block
 block discarded – undo
169 169
      * @param $pdo PDO
170 170
      * @param $config Configuration
171 171
      */
172
-    public static function checkUrlTable($pdo, $config){
173
-        try{
172
+    public static function checkUrlTable($pdo, $config) {
173
+        try {
174 174
             $db_url_table = $config->getUrlTableName();
175 175
             $stmt = $pdo->prepare("SELECT * FROM $db_url_table WHERE 1==0");
176 176
             $stmt->execute();            
177 177
         }
178
-        catch(Exception $e){
178
+        catch (Exception $e) {
179 179
             return false;
180 180
         }
181 181
         return true;
@@ -187,13 +187,13 @@  discard block
 block discarded – undo
187 187
      * @param $pdo PDO
188 188
      * @param $config Configuration
189 189
      */
190
-    public static function checkOptionsTable($pdo, $config){
191
-        try{
190
+    public static function checkOptionsTable($pdo, $config) {
191
+        try {
192 192
             $db_options_table = $config->getOptionsTableName();
193 193
             $stmt = $pdo->prepare("SELECT * FROM $db_options_table WHERE 1==0");
194 194
             $stmt->execute();
195 195
         }
196
-        catch(Exception $e){
196
+        catch (Exception $e) {
197 197
             return false;
198 198
         }
199 199
         return true;
@@ -203,13 +203,13 @@  discard block
 block discarded – undo
203 203
      * @param $pdo PDO
204 204
      * @param $config Configuration
205 205
      */
206
-    public static function checkHitTable($pdo, $config){
207
-        try{
206
+    public static function checkHitTable($pdo, $config) {
207
+        try {
208 208
             $db_hit_table = $config->getHitTableName();
209 209
             $stmt = $pdo->prepare("SELECT * FROM $db_hit_table WHERE 1==0");
210 210
             $stmt->execute();            
211 211
         }
212
-        catch(Exception $e){
212
+        catch (Exception $e) {
213 213
             return false;
214 214
         }
215 215
         return true;        
@@ -221,7 +221,7 @@  discard block
 block discarded – undo
221 221
      * @param $config Configuration
222 222
      *
223 223
      */
224
-    public static function updateCount($pdo, $config, $options=[]){
224
+    public static function updateCount($pdo, $config, $options = []) {
225 225
 
226 226
         $db_hit_table = $config->getHitTableName();
227 227
         $db_options_table = $config->getOptionsTableName();
@@ -232,54 +232,54 @@  discard block
 block discarded – undo
232 232
         $store_time = true;
233 233
         $store_user = true;
234 234
         
235
-        if(array_key_exists('url', $options)){
235
+        if (array_key_exists('url', $options)) {
236 236
             $url = $options['url'];
237 237
         }
238
-        else if(array_key_exists('HTTP_HOST',$_SERVER)){
238
+        else if (array_key_exists('HTTP_HOST', $_SERVER)) {
239 239
             $url = "http://".$_SERVER['HTTP_HOST'];
240
-            if(array_key_exists('REQUEST_URI',$_SERVER)){
241
-                $url=$url.$_SERVER['REQUEST_URI'];
240
+            if (array_key_exists('REQUEST_URI', $_SERVER)) {
241
+                $url = $url.$_SERVER['REQUEST_URI'];
242 242
             }               
243 243
         }
244
-        else{
244
+        else {
245 245
             $url = "No Info";
246 246
         }
247 247
 
248
-        if($config->getRemoveQueryString()){
248
+        if ($config->getRemoveQueryString()) {
249 249
             $url = preg_replace('/\?.*/', '', $url);
250 250
         }
251 251
         
252
-        if(array_key_exists('id', $options)){
252
+        if (array_key_exists('id', $options)) {
253 253
             $id = $options['id'];
254 254
         }
255
-        else{
255
+        else {
256 256
             $id = $url;
257 257
         }
258 258
 
259 259
         $stmt = $pdo->prepare("UPDATE $db_hit_table SET count = count + 1 WHERE id = ?");
260 260
         $stmt->execute([$id]);
261
-        if( $stmt->rowCount() == 0 ){
261
+        if ($stmt->rowCount()==0) {
262 262
             $stmt = $pdo->prepare("INSERT INTO $db_hit_table (id, count) VALUES (?, 1)");
263 263
             $stmt->execute([$id]);
264 264
         }
265 265
 
266 266
         $stmt = $pdo->prepare("UPDATE $db_url_table SET count = count + 1 WHERE id = ? and url = ?");
267 267
         $stmt->execute([$id, $url]);
268
-        if( $stmt->rowCount() == 0 ){
268
+        if ($stmt->rowCount()==0) {
269 269
             $stmt = $pdo->prepare("INSERT INTO $db_url_table (id, url, count) VALUES (?, ?, 1)");
270 270
             $stmt->execute([$id, $url]);
271 271
         }
272 272
         
273 273
         
274
-        if($store_from){
274
+        if ($store_from) {
275 275
                         
276
-            if(array_key_exists('from_id', $options)){
276
+            if (array_key_exists('from_id', $options)) {
277 277
                 $ids = [$options['from_id']];
278 278
             }
279
-            else{
279
+            else {
280 280
                 $from_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'No referer info';
281
-                $ids = Persistence::findHitIdsByUrl($pdo,$config,$from_url); 
282
-                if(count($ids)==0){
281
+                $ids = Persistence::findHitIdsByUrl($pdo, $config, $from_url); 
282
+                if (count($ids)==0) {
283 283
                     $stmt = $pdo->prepare("INSERT INTO $db_url_table (id, url, count) VALUES (?, ?, 1)");
284 284
                     $stmt->execute([$from_url, $from_url]);
285 285
                     $ids = [$from_url];
@@ -288,7 +288,7 @@  discard block
 block discarded – undo
288 288
             foreach ($ids as $from_id) {
289 289
                 $stmt = $pdo->prepare("UPDATE $db_from_table SET count = count + 1 WHERE id = ? and from_id = ?");
290 290
                 $stmt->execute([$id, $from_id]);
291
-                if( $stmt->rowCount() == 0 ){
291
+                if ($stmt->rowCount()==0) {
292 292
                     $stmt = $pdo->prepare("INSERT INTO $db_from_table (id, from_id, count) VALUES (?, ?, 1)");
293 293
                     $stmt->execute([$id, $from_id]);
294 294
                 }
@@ -296,13 +296,13 @@  discard block
 block discarded – undo
296 296
         }
297 297
                     
298 298
         $user = null;
299
-        if($store_user){
300
-            if(array_key_exists('user', $options)){
299
+        if ($store_user) {
300
+            if (array_key_exists('user', $options)) {
301 301
                 $user = $options['user'];
302 302
             }    
303 303
         }
304 304
         
305
-        if($store_time || $store_user){
305
+        if ($store_time || $store_user) {
306 306
             $stmt = $pdo->prepare("INSERT INTO $db_options_table (id, time, user) VALUES (?, ?, ?)");
307 307
             $stmt->execute([$id, time(), $user]);
308 308
         }
@@ -317,19 +317,19 @@  discard block
 block discarded – undo
317 317
      * @param $config Configuration
318 318
      *
319 319
      */
320
-    public static function findHitIdsByUrl($pdo, $config, $url){
320
+    public static function findHitIdsByUrl($pdo, $config, $url) {
321 321
         $resp = [];
322
-        try{
322
+        try {
323 323
             
324 324
             $dbtable = $config->getUrlTableName();
325 325
             $stmt = $pdo->prepare("SELECT id,url,count FROM $dbtable WHERE url = '$url'");
326
-            if($stmt->execute()){
327
-                while($row = $stmt->fetch()){
326
+            if ($stmt->execute()) {
327
+                while ($row = $stmt->fetch()) {
328 328
                     $resp[] = $row['id'];
329 329
                 }
330 330
             }
331 331
         }
332
-        catch(Exception $e){
332
+        catch (Exception $e) {
333 333
             throw new Exception("Error executing function 'findHitsByUrl'. ".$e->getMessage());
334 334
         }
335 335
         return $resp;
@@ -342,20 +342,20 @@  discard block
 block discarded – undo
342 342
      * @param $config Configuration
343 343
      *
344 344
      */
345
-    public static function getAllHits($pdo, $config){
345
+    public static function getAllHits($pdo, $config) {
346 346
         $resp = [];
347
-        try{
347
+        try {
348 348
             
349 349
             $dbtable = $config->getHitTableName();
350 350
             $stmt = $pdo->prepare("SELECT id,count FROM $dbtable");
351
-            if($stmt->execute()){
352
-                while($row = $stmt->fetch()){
353
-                    $resp[] = [$row['id'],$row['count']];
351
+            if ($stmt->execute()) {
352
+                while ($row = $stmt->fetch()) {
353
+                    $resp[] = [$row['id'], $row['count']];
354 354
                 }
355 355
             }
356 356
             
357 357
         }
358
-        catch(Exception $e){
358
+        catch (Exception $e) {
359 359
             throw new Exception("Error executing function 'getAllHits'. ".$e->getMessage());
360 360
         }
361 361
         return $resp;        
@@ -367,36 +367,36 @@  discard block
 block discarded – undo
367 367
      * @param $config Configuration
368 368
      *
369 369
      */
370
-    public static function findUrls($pdo, $config, $by=[]){
370
+    public static function findUrls($pdo, $config, $by = []) {
371 371
         $resp = [];
372
-        try{
372
+        try {
373 373
             $dbtable = $config->getUrlTableName();
374 374
             $qdata = [];
375 375
             $tquery = [];
376
-            if(array_key_exists('id',$by)){
376
+            if (array_key_exists('id', $by)) {
377 377
                 $qdata[] = $by['id'];
378 378
                 $tquery[] = "id = ?";
379 379
             }
380 380
             
381
-            if(array_key_exists('url',$by)){
381
+            if (array_key_exists('url', $by)) {
382 382
                 $qdata[] = $by['url'];
383 383
                 $tquery[] = "url = ?";
384 384
             }
385 385
             
386 386
             $sql = "SELECT id,url,count FROM $dbtable";
387
-            if(count($tquery) > 0){
388
-                $sql = $sql." WHERE ".join(" AND ",$tquery);
387
+            if (count($tquery) > 0) {
388
+                $sql = $sql." WHERE ".join(" AND ", $tquery);
389 389
             }
390 390
             
391 391
             $stmt = $pdo->prepare($sql);
392
-            if($stmt->execute($qdata)){
393
-                while($row = $stmt->fetch()){
394
-                    $resp[] = [$row['id'],$row['url'],$row['count']];
392
+            if ($stmt->execute($qdata)) {
393
+                while ($row = $stmt->fetch()) {
394
+                    $resp[] = [$row['id'], $row['url'], $row['count']];
395 395
                 }
396 396
             }
397 397
             
398 398
         }
399
-        catch(Exception $e){
399
+        catch (Exception $e) {
400 400
             throw new Exception("Error executing function 'getAllUrls'. ".$e->getMessage());
401 401
         }
402 402
         return $resp;
@@ -407,41 +407,41 @@  discard block
 block discarded – undo
407 407
      * @param $config Configuration
408 408
      *
409 409
      */
410
-    public static function findIdByTimeUser($pdo, $config, $by=[]){
411
-        $resp = [ ];
412
-        try{
410
+    public static function findIdByTimeUser($pdo, $config, $by = []) {
411
+        $resp = [];
412
+        try {
413 413
             $dbtable = $config->getOptionsTableName();
414 414
             $qdata = [];
415 415
             $tquery = [];
416
-            if(array_key_exists('from',$by)){
416
+            if (array_key_exists('from', $by)) {
417 417
                 $qdata[] = $by['from'];
418 418
                 $tquery[] = "time >= ?";
419 419
             }
420 420
             
421
-            if(array_key_exists('to',$by)){
421
+            if (array_key_exists('to', $by)) {
422 422
                 $qdata[] = $by['to'];
423 423
                 $tquery[] = "time <= ?";
424 424
             }
425 425
             
426
-            if(array_key_exists('user',$by)){
426
+            if (array_key_exists('user', $by)) {
427 427
                 $qdata[] = $by['user'];
428 428
                 $tquery[] = "user = ?";
429 429
             }
430 430
             
431 431
             $sql = "SELECT id,time,user FROM $dbtable";
432
-            if(count($tquery) > 0){
433
-                $sql = $sql." WHERE ".join(" AND ",$tquery);
432
+            if (count($tquery) > 0) {
433
+                $sql = $sql." WHERE ".join(" AND ", $tquery);
434 434
             }
435 435
             
436 436
             $stmt = $pdo->prepare($sql);
437
-            if($stmt->execute($qdata)){
438
-                while($row = $stmt->fetch()){
439
-                    $resp[] = [$row['id'],$row['time'],$row['user']];
437
+            if ($stmt->execute($qdata)) {
438
+                while ($row = $stmt->fetch()) {
439
+                    $resp[] = [$row['id'], $row['time'], $row['user']];
440 440
                 }
441 441
             }
442 442
             
443 443
         }
444
-        catch(Exception $e){
444
+        catch (Exception $e) {
445 445
             throw new Exception("Error executing function 'getAllUrls'. ".$e->getMessage());
446 446
         }
447 447
         return $resp;
@@ -453,40 +453,40 @@  discard block
 block discarded – undo
453 453
      * @param $config Configuration
454 454
      *
455 455
      */
456
-    public static function findByFrom($pdo, $config, $by=[]){
456
+    public static function findByFrom($pdo, $config, $by = []) {
457 457
         $resp = [];
458
-        try{
458
+        try {
459 459
             $dbFromtable = $config->getFromTableName();
460 460
             $dbUrltable = $config->getUrlTableName();
461 461
             $qdata = [];
462 462
             $tquery = [];
463 463
             
464
-            if(array_key_exists('url',$by) && array_key_exists('id',$by)){
464
+            if (array_key_exists('url', $by) && array_key_exists('id', $by)) {
465 465
                 $qdata = [$by['url'], $by['id']];
466 466
                 $tquery = "SELECT f.* FROM  $dbFromtable f,$dbUrltable u WHERE (f.from_id = u.id and f.url = ?) or f.from_id = ?";                
467 467
             }
468
-            else if(array_key_exists('url',$by)){
468
+            else if (array_key_exists('url', $by)) {
469 469
                 $qdata = [$by['url']];
470 470
                 $tquery = "SELECT f.* FROM $dbFromtable f,$dbUrltable u where f.from_id = u.id and u.url = ?";
471 471
             }
472
-            else if(array_key_exists('id',$by)){
472
+            else if (array_key_exists('id', $by)) {
473 473
                 $qdata = [$by['id']];
474 474
                 $tquery = "SELECT f.* FROM $dbFromtable f where f.from_id = ?";
475 475
             }
476
-            else{
476
+            else {
477 477
                 $qdata = [];
478 478
                 $tquery = "SELECT f.* FROM $dbFromtable f";
479 479
             }
480 480
                                     
481 481
             $stmt = $pdo->prepare($tquery);
482
-            if($stmt->execute($qdata)){
483
-                while($row = $stmt->fetch()){
484
-                    $resp[] = [$row['id'],$row['from_id'],$row['count']];
482
+            if ($stmt->execute($qdata)) {
483
+                while ($row = $stmt->fetch()) {
484
+                    $resp[] = [$row['id'], $row['from_id'], $row['count']];
485 485
                 }
486 486
             }
487 487
             
488 488
         }
489
-        catch(Exception $e){
489
+        catch (Exception $e) {
490 490
             throw new Exception("Error executing function 'findByFrom'. ".$e->getMessage());
491 491
         }
492 492
         return $resp;
@@ -502,20 +502,20 @@  discard block
 block discarded – undo
502 502
     public static function getCounts($pdo, $config)
503 503
     {
504 504
         $resp = [];
505
-        try{
505
+        try {
506 506
 
507 507
             $dbHitTable = $config->getHitTableName();
508 508
             $dbUrlTable = $config->getUrlTableName();
509 509
             $stmt = $pdo->prepare("SELECT h.id, u.url, h.count FROM $dbHitTable h, $dbUrlTable u WHERE h.id=u.id");
510
-            if($stmt->execute()){
511
-                while($row = $stmt->fetch()){
512
-                    $resp[] = [$row[0],$row[1],$row[2],];
510
+            if ($stmt->execute()) {
511
+                while ($row = $stmt->fetch()) {
512
+                    $resp[] = [$row[0], $row[1], $row[2], ];
513 513
                 }
514 514
             }
515 515
             
516 516
             $stmt = null;
517 517
         }
518
-        catch(Exception $e){
518
+        catch (Exception $e) {
519 519
             throw new Exception("Error reading the database. Method getCounts().".$e->getMessage());
520 520
         }        
521 521
         return $resp;
@@ -527,21 +527,21 @@  discard block
 block discarded – undo
527 527
      * @param $config Configuration
528 528
      *
529 529
      */
530
-    public static function getHitsWithOptions($pdo, $config){
530
+    public static function getHitsWithOptions($pdo, $config) {
531 531
         $resp = [];
532
-        try{
532
+        try {
533 533
             
534 534
             $dbOptionsTable = $config->getOptionsTableName();
535 535
             $stmt = $pdo->prepare("SELECT o.id, o.time, o.user FROM $dbOptionsTable o");
536
-            if($stmt->execute()){
537
-                while($row = $stmt->fetch()){
538
-                    $resp[] = ['id'=>$row[0],'time'=>$row[1],'user'=>$row[2],];
536
+            if ($stmt->execute()) {
537
+                while ($row = $stmt->fetch()) {
538
+                    $resp[] = ['id'=>$row[0], 'time'=>$row[1], 'user'=>$row[2], ];
539 539
                 }
540 540
             }
541 541
             
542 542
             $stmt = null;
543 543
         }
544
-        catch(Exception $e){
544
+        catch (Exception $e) {
545 545
             throw new Exception("Error reading the database. Method getCounts().".$e->getMessage());
546 546
         }
547 547
         return $resp;
@@ -552,7 +552,7 @@  discard block
 block discarded – undo
552 552
      * @param $config Configuration
553 553
      *
554 554
      */    
555
-    public static function getCountsById($pdo, $config){
555
+    public static function getCountsById($pdo, $config) {
556 556
         return "Works";
557 557
     }
558 558
 }
Please login to merge, or discard this patch.
Braces   +23 added lines, -46 removed lines patch added patch discarded remove patch
@@ -35,8 +35,7 @@  discard block
 block discarded – undo
35 35
         if($config->getDsn()){
36 36
             try{
37 37
                 return new PDO($config->getDsn(),$config->getUser(),$config->getPassword(),$options);
38
-            }
39
-            catch(Exception $e){                
38
+            } catch(Exception $e){                
40 39
                 if(!$config->getUseOnMemoryDB()){
41 40
                     throw new Exception("Could not create a db connection. Check permissions, configuration, and documentation. ".$e->getMessage());
42 41
                 }
@@ -46,8 +45,7 @@  discard block
 block discarded – undo
46 45
         if($config->getUseOnMemoryDB()){
47 46
             try{
48 47
                 return new PDO("sqlite::memory:",$options);
49
-            }
50
-            catch(Exception $e){
48
+            } catch(Exception $e){
51 49
                 throw new Exception("Could not create a db connection. Check permissions, configuration, and documentation. ".$e->getMessage());                
52 50
             }
53 51
         }
@@ -77,8 +75,7 @@  discard block
 block discarded – undo
77 75
             $stmt->execute();
78 76
             $stmt = $pdo->prepare("CREATE TABLE $db_url_table (id VARCHAR(255), url VARCHAR(255), count INT)");
79 77
             $stmt->execute();
80
-        }
81
-        catch(Exception $e){
78
+        } catch(Exception $e){
82 79
             throw new Exception("Could not create the database. ".$e->getMessage());
83 80
         }        
84 81
         return true;
@@ -119,8 +116,7 @@  discard block
 block discarded – undo
119 116
             $stmt = $pdo->prepare("DROP TABLE $tableName");
120 117
             $stmt->execute();
121 118
            
122
-        }
123
-        catch(Exception $e){
119
+        } catch(Exception $e){
124 120
             throw new Exception("Problem deleting the table $tableName. ".$e->getMessage());
125 121
         }
126 122
         return true;
@@ -140,8 +136,7 @@  discard block
 block discarded – undo
140 136
             $resp = $resp && Persistence::checkOptionsTable($pdo, $config);
141 137
             $resp = $resp && Persistence::checkFromTable($pdo, $config);
142 138
             $resp = $resp && Persistence::checkUrlTable($pdo, $config);
143
-        }
144
-        catch(Exception $e){
139
+        } catch(Exception $e){
145 140
             return false;
146 141
         }        
147 142
         return $resp;
@@ -158,8 +153,7 @@  discard block
 block discarded – undo
158 153
             $stmt = $pdo->prepare("SELECT * FROM $db_from_table WHERE 1==0");
159 154
             $stmt->execute();
160 155
             
161
-        }
162
-        catch(Exception $e){
156
+        } catch(Exception $e){
163 157
             return false;
164 158
         }
165 159
         return true;
@@ -174,8 +168,7 @@  discard block
 block discarded – undo
174 168
             $db_url_table = $config->getUrlTableName();
175 169
             $stmt = $pdo->prepare("SELECT * FROM $db_url_table WHERE 1==0");
176 170
             $stmt->execute();            
177
-        }
178
-        catch(Exception $e){
171
+        } catch(Exception $e){
179 172
             return false;
180 173
         }
181 174
         return true;
@@ -192,8 +185,7 @@  discard block
 block discarded – undo
192 185
             $db_options_table = $config->getOptionsTableName();
193 186
             $stmt = $pdo->prepare("SELECT * FROM $db_options_table WHERE 1==0");
194 187
             $stmt->execute();
195
-        }
196
-        catch(Exception $e){
188
+        } catch(Exception $e){
197 189
             return false;
198 190
         }
199 191
         return true;
@@ -208,8 +200,7 @@  discard block
 block discarded – undo
208 200
             $db_hit_table = $config->getHitTableName();
209 201
             $stmt = $pdo->prepare("SELECT * FROM $db_hit_table WHERE 1==0");
210 202
             $stmt->execute();            
211
-        }
212
-        catch(Exception $e){
203
+        } catch(Exception $e){
213 204
             return false;
214 205
         }
215 206
         return true;        
@@ -234,14 +225,12 @@  discard block
 block discarded – undo
234 225
         
235 226
         if(array_key_exists('url', $options)){
236 227
             $url = $options['url'];
237
-        }
238
-        else if(array_key_exists('HTTP_HOST',$_SERVER)){
228
+        } else if(array_key_exists('HTTP_HOST',$_SERVER)){
239 229
             $url = "http://".$_SERVER['HTTP_HOST'];
240 230
             if(array_key_exists('REQUEST_URI',$_SERVER)){
241 231
                 $url=$url.$_SERVER['REQUEST_URI'];
242 232
             }               
243
-        }
244
-        else{
233
+        } else{
245 234
             $url = "No Info";
246 235
         }
247 236
 
@@ -251,8 +240,7 @@  discard block
 block discarded – undo
251 240
         
252 241
         if(array_key_exists('id', $options)){
253 242
             $id = $options['id'];
254
-        }
255
-        else{
243
+        } else{
256 244
             $id = $url;
257 245
         }
258 246
 
@@ -275,8 +263,7 @@  discard block
 block discarded – undo
275 263
                         
276 264
             if(array_key_exists('from_id', $options)){
277 265
                 $ids = [$options['from_id']];
278
-            }
279
-            else{
266
+            } else{
280 267
                 $from_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'No referer info';
281 268
                 $ids = Persistence::findHitIdsByUrl($pdo,$config,$from_url); 
282 269
                 if(count($ids)==0){
@@ -328,8 +315,7 @@  discard block
 block discarded – undo
328 315
                     $resp[] = $row['id'];
329 316
                 }
330 317
             }
331
-        }
332
-        catch(Exception $e){
318
+        } catch(Exception $e){
333 319
             throw new Exception("Error executing function 'findHitsByUrl'. ".$e->getMessage());
334 320
         }
335 321
         return $resp;
@@ -354,8 +340,7 @@  discard block
 block discarded – undo
354 340
                 }
355 341
             }
356 342
             
357
-        }
358
-        catch(Exception $e){
343
+        } catch(Exception $e){
359 344
             throw new Exception("Error executing function 'getAllHits'. ".$e->getMessage());
360 345
         }
361 346
         return $resp;        
@@ -395,8 +380,7 @@  discard block
 block discarded – undo
395 380
                 }
396 381
             }
397 382
             
398
-        }
399
-        catch(Exception $e){
383
+        } catch(Exception $e){
400 384
             throw new Exception("Error executing function 'getAllUrls'. ".$e->getMessage());
401 385
         }
402 386
         return $resp;
@@ -440,8 +424,7 @@  discard block
 block discarded – undo
440 424
                 }
441 425
             }
442 426
             
443
-        }
444
-        catch(Exception $e){
427
+        } catch(Exception $e){
445 428
             throw new Exception("Error executing function 'getAllUrls'. ".$e->getMessage());
446 429
         }
447 430
         return $resp;
@@ -464,16 +447,13 @@  discard block
 block discarded – undo
464 447
             if(array_key_exists('url',$by) && array_key_exists('id',$by)){
465 448
                 $qdata = [$by['url'], $by['id']];
466 449
                 $tquery = "SELECT f.* FROM  $dbFromtable f,$dbUrltable u WHERE (f.from_id = u.id and f.url = ?) or f.from_id = ?";                
467
-            }
468
-            else if(array_key_exists('url',$by)){
450
+            } else if(array_key_exists('url',$by)){
469 451
                 $qdata = [$by['url']];
470 452
                 $tquery = "SELECT f.* FROM $dbFromtable f,$dbUrltable u where f.from_id = u.id and u.url = ?";
471
-            }
472
-            else if(array_key_exists('id',$by)){
453
+            } else if(array_key_exists('id',$by)){
473 454
                 $qdata = [$by['id']];
474 455
                 $tquery = "SELECT f.* FROM $dbFromtable f where f.from_id = ?";
475
-            }
476
-            else{
456
+            } else{
477 457
                 $qdata = [];
478 458
                 $tquery = "SELECT f.* FROM $dbFromtable f";
479 459
             }
@@ -485,8 +465,7 @@  discard block
 block discarded – undo
485 465
                 }
486 466
             }
487 467
             
488
-        }
489
-        catch(Exception $e){
468
+        } catch(Exception $e){
490 469
             throw new Exception("Error executing function 'findByFrom'. ".$e->getMessage());
491 470
         }
492 471
         return $resp;
@@ -514,8 +493,7 @@  discard block
 block discarded – undo
514 493
             }
515 494
             
516 495
             $stmt = null;
517
-        }
518
-        catch(Exception $e){
496
+        } catch(Exception $e){
519 497
             throw new Exception("Error reading the database. Method getCounts().".$e->getMessage());
520 498
         }        
521 499
         return $resp;
@@ -540,8 +518,7 @@  discard block
 block discarded – undo
540 518
             }
541 519
             
542 520
             $stmt = null;
543
-        }
544
-        catch(Exception $e){
521
+        } catch(Exception $e){
545 522
             throw new Exception("Error reading the database. Method getCounts().".$e->getMessage());
546 523
         }
547 524
         return $resp;
Please login to merge, or discard this patch.
src/Configuration.php 2 patches
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -164,18 +164,18 @@  discard block
 block discarded – undo
164 164
     /*
165 165
      * @param configFileName
166 166
      */
167
-    public function __construct($configFileName, $pdoProvided=FALSE)
167
+    public function __construct($configFileName, $pdoProvided = FALSE)
168 168
     {    
169 169
         $config = parse_ini_file($configFileName, TRUE); 
170
-        if(!$pdoProvided){
171
-            $this->dsn = $this->loadMandatoryVariable($config,"database","dsn");
170
+        if (!$pdoProvided) {
171
+            $this->dsn = $this->loadMandatoryVariable($config, "database", "dsn");
172 172
         }
173 173
         
174
-        $this->hitTableName = $this->loadMandatoryVariable($config,"database","db_hit_table");
175
-        $this->fromTableName = $this->loadMandatoryVariable($config,"database","db_from_table");        
176
-        $this->optionsTableName = $this->loadMandatoryVariable($config,"database","db_options_table");
177
-        $this->urlTableName = $this->loadMandatoryVariable($config,"database","db_url_table");
178
-        $this->useOnMemoryDB = $this->loadMandatoryVariable($config,"database","use_onmemorydb");
174
+        $this->hitTableName = $this->loadMandatoryVariable($config, "database", "db_hit_table");
175
+        $this->fromTableName = $this->loadMandatoryVariable($config, "database", "db_from_table");        
176
+        $this->optionsTableName = $this->loadMandatoryVariable($config, "database", "db_options_table");
177
+        $this->urlTableName = $this->loadMandatoryVariable($config, "database", "db_url_table");
178
+        $this->useOnMemoryDB = $this->loadMandatoryVariable($config, "database", "use_onmemorydb");
179 179
         
180 180
         $this->storeTime = isset($config['options']['store_time']) ? strtolower($config['options']['store_time'])=="yes" : false;
181 181
         $this->storeUser = isset($config['options']['store_user']) ? strtolower($config['options']['store_user'])=="yes" : false;
@@ -194,13 +194,13 @@  discard block
 block discarded – undo
194 194
      *
195 195
      * @return string
196 196
      */
197
-    private function loadMandatoryVariable($configFile,$section,$varname)
197
+    private function loadMandatoryVariable($configFile, $section, $varname)
198 198
     {
199
-        try{
199
+        try {
200 200
             return $configFile[$section][$varname];
201 201
         }
202
-        catch(Exception $e){
203
-            throw new Exception( "Error loading config file. Variable $varname in section [$section] not found. Check the configuration file.");
202
+        catch (Exception $e) {
203
+            throw new Exception("Error loading config file. Variable $varname in section [$section] not found. Check the configuration file.");
204 204
         }
205 205
     }
206 206
 
Please login to merge, or discard this patch.
Braces   +1 added lines, -2 removed lines patch added patch discarded remove patch
@@ -198,8 +198,7 @@
 block discarded – undo
198 198
     {
199 199
         try{
200 200
             return $configFile[$section][$varname];
201
-        }
202
-        catch(Exception $e){
201
+        } catch(Exception $e){
203 202
             throw new Exception( "Error loading config file. Variable $varname in section [$section] not found. Check the configuration file.");
204 203
         }
205 204
     }
Please login to merge, or discard this patch.
src/SiteAnalyzer.php 2 patches
Spacing   +39 added lines, -39 removed lines patch added patch discarded remove patch
@@ -18,32 +18,32 @@  discard block
 block discarded – undo
18 18
  * @copyright 2018 Ruben Dorado
19 19
  * @license   http://www.opensource.org/licenses/MIT The MIT License
20 20
  */
21
-class SiteAnalyzer{
21
+class SiteAnalyzer {
22 22
 
23 23
     
24 24
     /*
25 25
      * @param 
26 26
      */
27
-    static function count($options=[])
27
+    static function count($options = [])
28 28
     {	
29
-        $config = SiteAnalyzer::loadConfig( array_key_exists('pdo',$options) );
29
+        $config = SiteAnalyzer::loadConfig(array_key_exists('pdo', $options));
30 30
         
31
-        if(array_key_exists('pdo',$options)){            
31
+        if (array_key_exists('pdo', $options)) {            
32 32
             $pdo = $options['pdo'];	
33 33
         }
34
-        else{            
34
+        else {            
35 35
             $pdo = Persistence::getPDO($config);
36 36
         }
37 37
         
38
-        try{
39
-            return Persistence::updateCount($pdo,$config,$options);
38
+        try {
39
+            return Persistence::updateCount($pdo, $config, $options);
40 40
         }
41
-        catch(Exception $e) {
42
-            try{
41
+        catch (Exception $e) {
42
+            try {
43 43
                 Persistence::crateDatabase($pdo, $config);
44
-                return Persistence::updateCount($pdo,$config,$options);
44
+                return Persistence::updateCount($pdo, $config, $options);
45 45
             }
46
-            catch(Exception $e) {
46
+            catch (Exception $e) {
47 47
                 throw new Exception("Site Analyzer could connect to the database.".$e->getMessage());
48 48
             };
49 49
             
@@ -55,17 +55,17 @@  discard block
 block discarded – undo
55 55
     /*
56 56
      * @param $format string, one of [php-array, xml, json, txt-csv]
57 57
      */
58
-    public static function loadConfig($pdoProvided=FALSE)
58
+    public static function loadConfig($pdoProvided = FALSE)
59 59
     {
60 60
         $config = NULL;
61
-        try{
62
-            $config = new Configuration("../../../../site-analyzer.ini",$pdoProvided);
61
+        try {
62
+            $config = new Configuration("../../../../site-analyzer.ini", $pdoProvided);
63 63
         }
64
-        catch(Exception $e){
65
-            try{
66
-                $config = new Configuration("site-analyzer.ini",$pdoProvided); 
64
+        catch (Exception $e) {
65
+            try {
66
+                $config = new Configuration("site-analyzer.ini", $pdoProvided); 
67 67
             }
68
-            catch(Exception $e){
68
+            catch (Exception $e) {
69 69
                 throw Exception("Config file not found.");
70 70
             }
71 71
         }
@@ -76,13 +76,13 @@  discard block
 block discarded – undo
76 76
     /*
77 77
      * @param $format string, one of [php-array, xml, json, txt-csv]
78 78
      */
79
-    public static function getStats($pdo=null, $renderer=null)
79
+    public static function getStats($pdo = null, $renderer = null)
80 80
     {         
81 81
         $config = new Configuration("site-analyzer.ini", isset($pdo));
82
-        if($pdo==null){
82
+        if ($pdo==null) {
83 83
             $pdo = Persistence::getPDO($config);
84 84
         }
85
-        $data = Persistence::getCounts($pdo,$config);
85
+        $data = Persistence::getCounts($pdo, $config);
86 86
         return $data;
87 87
     } 
88 88
 
@@ -92,15 +92,15 @@  discard block
 block discarded – undo
92 92
     /*
93 93
      * @param
94 94
      */
95
-    public static function groupHitsByTime($criteria, $pdo=null)
95
+    public static function groupHitsByTime($criteria, $pdo = null)
96 96
     {
97 97
         $config = new Configuration("site-analyzer.ini", isset($pdo));
98
-        if($pdo==null){
98
+        if ($pdo==null) {
99 99
             $pdo = Persistence::getPDO($config);
100 100
         }
101
-        $data = Persistence::getHitsWithOptions($pdo,$config);
101
+        $data = Persistence::getHitsWithOptions($pdo, $config);
102 102
         $resp = [];
103
-        foreach ($data as $row){
103
+        foreach ($data as $row) {
104 104
             $tmp = [$row['id']];
105 105
             $tmp = array_merge($tmp, getdate($row['time']));
106 106
             $resp[] = $tmp;
@@ -114,26 +114,26 @@  discard block
 block discarded – undo
114 114
     /*
115 115
      * @param
116 116
      */
117
-    public static function groupHitsByUser($pdo=null)
117
+    public static function groupHitsByUser($pdo = null)
118 118
     {
119 119
         $config = new Configuration("site-analyzer.ini", isset($pdo));
120
-        if($pdo==null){
120
+        if ($pdo==null) {
121 121
             $pdo = Persistence::getPDO($config);
122 122
         }
123
-        $data = Persistence::getHitsWithOptions($pdo,$config);
123
+        $data = Persistence::getHitsWithOptions($pdo, $config);
124 124
         
125 125
         $count = [];
126
-        foreach ($data as $row){
127
-            if(array_key_exists($row['user'],$count)){
126
+        foreach ($data as $row) {
127
+            if (array_key_exists($row['user'], $count)) {
128 128
                 $count[$row['user']]++;
129 129
             }
130
-            else{
131
-                $count[$row['user']]=1;
130
+            else {
131
+                $count[$row['user']] = 1;
132 132
             }            
133 133
         }
134 134
         
135 135
         $resp = [];
136
-        foreach ($count as $user => $count){
136
+        foreach ($count as $user => $count) {
137 137
             $resp[] = [$user, $count];
138 138
         }
139 139
         return $resp;
@@ -146,14 +146,14 @@  discard block
 block discarded – undo
146 146
      */
147 147
     public static function transform($data, $format)
148 148
     {
149
-        if($format=="html"){
149
+        if ($format=="html") {
150 150
             $resp = "<table style='border-collapse: collapse;border: 1px solid black;'>";
151
-            foreach($data as $row){
152
-                $resp.="<tr style='border: 1px solid black;'>";
153
-                foreach($row as $cell){
154
-                    $resp.="<td style='border: 1px solid black;'>$cell</td>";
151
+            foreach ($data as $row) {
152
+                $resp .= "<tr style='border: 1px solid black;'>";
153
+                foreach ($row as $cell) {
154
+                    $resp .= "<td style='border: 1px solid black;'>$cell</td>";
155 155
                 }  
156
-                $resp.="</tr>";
156
+                $resp .= "</tr>";
157 157
             }
158 158
             return $resp."</table>";
159 159
         }
Please login to merge, or discard this patch.
Braces   +6 added lines, -12 removed lines patch added patch discarded remove patch
@@ -30,20 +30,17 @@  discard block
 block discarded – undo
30 30
         
31 31
         if(array_key_exists('pdo',$options)){            
32 32
             $pdo = $options['pdo'];	
33
-        }
34
-        else{            
33
+        } else{            
35 34
             $pdo = Persistence::getPDO($config);
36 35
         }
37 36
         
38 37
         try{
39 38
             return Persistence::updateCount($pdo,$config,$options);
40
-        }
41
-        catch(Exception $e) {
39
+        } catch(Exception $e) {
42 40
             try{
43 41
                 Persistence::crateDatabase($pdo, $config);
44 42
                 return Persistence::updateCount($pdo,$config,$options);
45
-            }
46
-            catch(Exception $e) {
43
+            } catch(Exception $e) {
47 44
                 throw new Exception("Site Analyzer could connect to the database.".$e->getMessage());
48 45
             };
49 46
             
@@ -60,12 +57,10 @@  discard block
 block discarded – undo
60 57
         $config = NULL;
61 58
         try{
62 59
             $config = new Configuration("../../../../site-analyzer.ini",$pdoProvided);
63
-        }
64
-        catch(Exception $e){
60
+        } catch(Exception $e){
65 61
             try{
66 62
                 $config = new Configuration("site-analyzer.ini",$pdoProvided); 
67
-            }
68
-            catch(Exception $e){
63
+            } catch(Exception $e){
69 64
                 throw Exception("Config file not found.");
70 65
             }
71 66
         }
@@ -126,8 +121,7 @@  discard block
 block discarded – undo
126 121
         foreach ($data as $row){
127 122
             if(array_key_exists($row['user'],$count)){
128 123
                 $count[$row['user']]++;
129
-            }
130
-            else{
124
+            } else{
131 125
                 $count[$row['user']]=1;
132 126
             }            
133 127
         }
Please login to merge, or discard this patch.