HitDAO   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 9
eloc 30
c 6
b 0
f 0
dl 0
loc 62
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A checkHitTable() 0 9 2
A countHit() 0 16 3
A getAllHits() 0 15 4
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
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, $id, $url) {
43
        $db_hit_table = $config->getHitTableName();
44
        $db_url_table = $config->getUrlTableName();
45
46
        $stmt = $pdo->prepare("UPDATE $db_hit_table SET count = count + 1 WHERE id = ?");
47
        $stmt->execute([$id]);
48
        if ($stmt->rowCount()==0) {
49
            $stmt = $pdo->prepare("INSERT INTO $db_hit_table (id, count) VALUES (?, 1)");
50
            $stmt->execute([$id]);
51
        }
52
53
        $stmt = $pdo->prepare("UPDATE $db_url_table SET count = count + 1 WHERE id = ? and url = ?");
54
        $stmt->execute([$id, $url]);
55
        if ($stmt->rowCount()==0) {
56
            $stmt = $pdo->prepare("INSERT INTO $db_url_table (id, url, count) VALUES (?, ?, 1)");
57
            $stmt->execute([$id, $url]);
58
        }
59
    }
60
    
61
    /*
62
     * @param $pdo \PDO
63
     * @param $config Configuration
64
     *
65
     */
66
    public static function getAllHits($pdo, $config) {        
67
        $resp = [];
68
        try {
69
            $dbtable = $config->getHitTableName();
70
            $stmt = $pdo->prepare("SELECT id,count FROM $dbtable");
71
            if ($stmt->execute()) {
72
                while ($row = $stmt->fetch()) {
73
                    $resp[] = [$row['id'], $row['count']];
74
                }
75
            }
76
            
77
        } catch (Exception $e) {
78
            throw new Exception("Error executing function 'getAllHits'. ".$e->getMessage());
79
        }
80
        return $resp;        
81
    }
82
83
}
84