Passed
Push — master ( a92af6...3728cc )
by Ruben
02:26
created

HitDAO::getAllHits()   A

Complexity

Conditions 4
Paths 9

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 9
nop 2
dl 0
loc 15
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * (c) Ruben Dorado <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
namespace SiteAnalyzer;
10
use Exception;
11
/**
12
 * class HitDAO
13
 *
14
 * @package   SiteAnalyzer
15
 * @author    Ruben Dorado <[email protected]>
16
 * @copyright 2018 Ruben Dorado
17
 * @license   http://www.opensource.org/licenses/MIT The MIT License
18
 */
19
class HitDAO
20
{
21
22
    /*
23
     * @param $pdo PDO
0 ignored issues
show
Bug introduced by
The type SiteAnalyzer\PDO was not found. Did you mean PDO? If so, make sure to prefix the type with \.
Loading history...
24
     * @param $config Configuration
25
     */
26
    public static function checkHitTable($pdo, $config) {
27
        try {
28
            $db_hit_table = $config->getHitTableName();
29
            $stmt = $pdo->prepare("SELECT * FROM $db_hit_table WHERE 1==0");
30
            $stmt->execute();            
31
        } catch (Exception $e) {
32
            return false;
33
        }
34
        return true;        
35
    }
36
    
37
    /*
38
     * @param $pdo PDO
39
     * @param $config Configuration
40
     *
41
     */
42
    public static function countHit($pdo, $config, $options = []) {
43
        if (array_key_exists('url', $options)) {
44
            $url = $options['url'];
45
        } else if (array_key_exists('HTTP_HOST', $_SERVER)) {
46
            $url = "http://".$_SERVER['HTTP_HOST'];
47
            if (array_key_exists('REQUEST_URI', $_SERVER)) {
48
                $url = $url.$_SERVER['REQUEST_URI'];
49
            }               
50
        } else {
51
            $url = "No Info";
52
        }
53
54
        if ($config->getRemoveQueryString()) {
55
            $url = preg_replace('/\?.*/', '', $url);
56
        }
57
        
58
        if (array_key_exists('id', $options)) {
59
            $id = $options['id'];
60
        } else {
61
            $id = $url;
62
        }
63
64
        $stmt = $pdo->prepare("UPDATE $db_hit_table SET count = count + 1 WHERE id = ?");
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $db_hit_table seems to be never defined.
Loading history...
65
        $stmt->execute([$id]);
66
        if ($stmt->rowCount()==0) {
67
            $stmt = $pdo->prepare("INSERT INTO $db_hit_table (id, count) VALUES (?, 1)");
68
            $stmt->execute([$id]);
69
        }
70
71
        $stmt = $pdo->prepare("UPDATE $db_url_table SET count = count + 1 WHERE id = ? and url = ?");
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $db_url_table seems to be never defined.
Loading history...
72
        $stmt->execute([$id, $url]);
73
        if ($stmt->rowCount()==0) {
74
            $stmt = $pdo->prepare("INSERT INTO $db_url_table (id, url, count) VALUES (?, ?, 1)");
75
            $stmt->execute([$id, $url]);
76
        }
77
        
78
    }
79
    
80
        
81
    /*
82
     * @param $pdo PDO
83
     * @param $config Configuration
84
     *
85
     */
86
    public static function getAllHits($pdo, $config) {
87
        $resp = [];
88
        try {
89
            $dbtable = $config->getHitTableName();
90
            $stmt = $pdo->prepare("SELECT id,count FROM $dbtable");
91
            if ($stmt->execute()) {
92
                while ($row = $stmt->fetch()) {
93
                    $resp[] = [$row['id'], $row['count']];
94
                }
95
            }
96
            
97
        } catch (Exception $e) {
98
            throw new Exception("Error executing function 'getAllHits'. ".$e->getMessage());
99
        }
100
        return $resp;        
101
    }
102
103
104
}
105