Passed
Push — master ( 0f89a1...b5c6f1 )
by Ruben
03:31 queued 01:38
created

HitDAO::countHit()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 4
nop 4
dl 0
loc 17
rs 9.8666
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
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, $options = []) {
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

42
    public static function countHit($pdo, $config, $id, /** @scrutinizer ignore-unused */ $options = []) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
43
        $db_hit_table = $config->getHitTableName();
44
        $db_url_table = $config->getUrlTableName();
45
46
47
        $stmt = $pdo->prepare("UPDATE $db_hit_table SET count = count + 1 WHERE id = ?");
48
        $stmt->execute([$id]);
49
        if ($stmt->rowCount()==0) {
50
            $stmt = $pdo->prepare("INSERT INTO $db_hit_table (id, count) VALUES (?, 1)");
51
            $stmt->execute([$id]);
52
        }
53
54
        $stmt = $pdo->prepare("UPDATE $db_url_table SET count = count + 1 WHERE id = ? and url = ?");
55
        $stmt->execute([$id, $url]);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $url seems to be never defined.
Loading history...
56
        if ($stmt->rowCount()==0) {
57
            $stmt = $pdo->prepare("INSERT INTO $db_url_table (id, url, count) VALUES (?, ?, 1)");
58
            $stmt->execute([$id, $url]);
59
        }
60
    }
61
    
62
    /*
63
     * @param $pdo \PDO
64
     * @param $config Configuration
65
     *
66
     */
67
    public static function getAllHits($pdo, $config) {        
68
        $resp = [];
69
        try {
70
            $dbtable = $config->getHitTableName();
71
            $stmt = $pdo->prepare("SELECT id,count FROM $dbtable");
72
            if ($stmt->execute()) {
73
                while ($row = $stmt->fetch()) {
74
                    $resp[] = [$row['id'], $row['count']];
75
                }
76
            }
77
            
78
        } catch (Exception $e) {
79
            throw new Exception("Error executing function 'getAllHits'. ".$e->getMessage());
80
        }
81
        return $resp;        
82
    }
83
84
}
85