OptionsDAO::checkOptionsTable()   A
last analyzed

Complexity

Conditions 2
Paths 4

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 4
nop 2
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 >= ?", 
66
                    'to'=>"time <= ?", 
67
                    'user'=>"user = ?" 
68
        ];
69
        
70
        foreach ($keySql as $key => $sql) {
71
            if (array_key_exists($key, $by)) {
72
                $qdata[] = $by[$key];
73
                $tquery[] = $sql;
74
            }
75
        }
76
                        
77
        $sql = "SELECT id,time,user FROM $dbtable";
78
        if (count($tquery) > 0) {
79
            $sql = $sql." WHERE ".join(" AND ", $tquery);
80
        }
81
            
82
        $stmt = $pdo->prepare($sql);
83
        $stmt->execute($qdata);
84
        while ($row = $stmt->fetch()) {
85
            $resp[] = [$row['id'], $row['time'], $row['user']];
86
        }
87
            
88
        return $resp;
89
    }
90
    
91
    /*
92
     * @param $pdo \PDO
93
     * @param $config Configuration
94
     *
95
     */
96
    public static function getHitsWithOptions($pdo, $config) {
97
        $resp = [];
98
            
99
        $dbOptionsTable = $config->getOptionsTableName();
100
        $stmt = $pdo->prepare("SELECT o.id, o.time, o.user FROM $dbOptionsTable o");
101
        $stmt->execute();
102
        while ($row = $stmt->fetch()) {
103
            $resp[] = [$row[0], $row[1], $row[2]];
104
        }
105
            
106
        return $resp;
107
    }
108
    
109
}
110