Passed
Branch master (dfd3a5)
by Ruben
03:28 queued 01:25
created

OptionsDAO::countOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 4
dl 0
loc 10
rs 10
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
11
use Exception;
12
/**
13
 * class OptionsDAO
14
 *
15
 * @package   SiteAnalyzer
16
 * @author    Ruben Dorado <[email protected]>
17
 * @copyright 2018 Ruben Dorado
18
 * @license   http://www.opensource.org/licenses/MIT The MIT License
19
 */
20
class OptionsDAO
21
{
22
23
    /*
24
     * @param $pdo \PDO
25
     * @param $config Configuration
26
     */
27
    public static function checkOptionsTable($pdo, $config) {
28
        try {
29
            $db_options_table = $config->getOptionsTableName();
30
            $stmt = $pdo->prepare("SELECT * FROM $db_options_table WHERE 1==0");
31
            $stmt->execute();
32
        } catch (Exception $e) {
33
            return false;
34
        }
35
        return true;
36
    }
37
    
38
    /*
39
     * @param $pdo \PDO
40
     * @param $config Configuration
41
     *
42
     */
43
    public static function countOptions($pdo, $config, $id, $options = []) {
44
        $user = null;
45
        $db_options_table = $config->getOptionsTableName();
46
                
47
        if (array_key_exists('user', $options)) {
48
            $user = $options['user'];   
49
        }
50
                
51
        $stmt = $pdo->prepare("INSERT INTO $db_options_table (id, time, user) VALUES (?, ?, ?)");
52
        $stmt->execute([$id, time(), $user]);       
53
    }
54
    
55
    /*
56
     * @param $pdo \PDO
57
     * @param $config Configuration
58
     *
59
     */
60
    public static function findIdByTimeUser($pdo, $config, $by = []) {
61
        $resp = [];
62
        $dbtable = $config->getOptionsTableName();
63
        $qdata = [];
64
        $tquery = [];
65
        $keySql = {'from'=>"time >= ?", 
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected '{' on line 65 at column 18
Loading history...
66
                   'to'=>"time <= ?", 
67
                   'user'=>"user = ?" };
68
        
69
        for ($keySql as $key => $sql){
70
            if (array_key_exists($key, $by)) {
71
                $qdata[] = $by[$key];
72
                $tquery[] = $sql;
73
            }
74
        }
75
                        
76
        $sql = "SELECT id,time,user FROM $dbtable";
77
        if (count($tquery) > 0) {
78
            $sql = $sql." WHERE ".join(" AND ", $tquery);
79
        }
80
            
81
        $stmt = $pdo->prepare($sql);
82
        $stmt->execute($qdata);
83
        while ($row = $stmt->fetch()) {
84
            $resp[] = [$row['id'], $row['time'], $row['user']];
85
        }
86
            
87
        return $resp;
88
    }
89
    
90
    /*
91
     * @param $pdo \PDO
92
     * @param $config Configuration
93
     *
94
     */
95
    public static function getHitsWithOptions($pdo, $config) {
96
        $resp = [];
97
            
98
        $dbOptionsTable = $config->getOptionsTableName();
99
        $stmt = $pdo->prepare("SELECT o.id, o.time, o.user FROM $dbOptionsTable o");
100
        $stmt->execute();
101
        while ($row = $stmt->fetch()) {
102
            $resp[] = ['id'=>$row[0], 'time'=>$row[1], 'user'=>$row[2]];
103
        }
104
            
105
        return $resp;
106
    }
107
    
108
}
109