Passed
Push — master ( 90d032...420e1f )
by Ruben
05:39 queued 03:45
created

OptionsDAO::checkOptionsTable()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 4
nop 2
dl 0
loc 9
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
use Exception;
11
/**
12
 * class OptionsDAO
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 OptionsDAO
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 checkOptionsTable($pdo, $config) {
27
        try {
28
            $db_options_table = $config->getOptionsTableName();
29
            $stmt = $pdo->prepare("SELECT * FROM $db_options_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 countOptions($pdo, $config, $options = []) {
0 ignored issues
show
Unused Code introduced by
The parameter $config 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 countOptions($pdo, /** @scrutinizer ignore-unused */ $config, $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
        $user = null;
44
        if ($store_user) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $store_user seems to be never defined.
Loading history...
45
            if (array_key_exists('user', $options)) {
46
                $user = $options['user'];
47
            }    
48
        }
49
        
50
        if ($store_time || $store_user) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $store_time seems to be never defined.
Loading history...
51
            $stmt = $pdo->prepare("INSERT INTO $db_options_table (id, time, user) VALUES (?, ?, ?)");
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $db_options_table does not exist. Did you maybe mean $options?
Loading history...
52
            $stmt->execute([$id, time(), $user]);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $id seems to be never defined.
Loading history...
53
        }        
54
    }
55
    
56
    /*
57
     * @param $pdo PDO
58
     * @param $config Configuration
59
     *
60
     */
61
    public static function findIdByTimeUser($pdo, $config, $by = []) {
62
        $resp = [];
63
        try {
64
            $dbtable = $config->getOptionsTableName();
65
            $qdata = [];
66
            $tquery = [];
67
            if (array_key_exists('from', $by)) {
68
                $qdata[] = $by['from'];
69
                $tquery[] = "time >= ?";
70
            }
71
            
72
            if (array_key_exists('to', $by)) {
73
                $qdata[] = $by['to'];
74
                $tquery[] = "time <= ?";
75
            }
76
            
77
            if (array_key_exists('user', $by)) {
78
                $qdata[] = $by['user'];
79
                $tquery[] = "user = ?";
80
            }
81
            
82
            $sql = "SELECT id,time,user FROM $dbtable";
83
            if (count($tquery) > 0) {
84
                $sql = $sql." WHERE ".join(" AND ", $tquery);
85
            }
86
            
87
            $stmt = $pdo->prepare($sql);
88
            if ($stmt->execute($qdata)) {
89
                while ($row = $stmt->fetch()) {
90
                    $resp[] = [$row['id'], $row['time'], $row['user']];
91
                }
92
            }
93
            
94
        } catch (Exception $e) {
95
            throw new Exception("Error executing function 'getAllUrls'. ".$e->getMessage());
96
        }
97
        return $resp;
98
    }
99
    
100
    /*
101
     * @param $pdo PDO
102
     * @param $config Configuration
103
     *
104
     */
105
    public static function getHitsWithOptions($pdo, $config) {
106
        $resp = [];
107
        try {
108
            
109
            $dbOptionsTable = $config->getOptionsTableName();
110
            $stmt = $pdo->prepare("SELECT o.id, o.time, o.user FROM $dbOptionsTable o");
111
            if ($stmt->execute()) {
112
                while ($row = $stmt->fetch()) {
113
                    $resp[] = ['id'=>$row[0], 'time'=>$row[1], 'user'=>$row[2]];
114
                }
115
            }
116
            
117
        } catch (Exception $e) {
118
            throw new Exception("Error reading the database. Method getCounts().".$e->getMessage());
119
        }
120
        return $resp;
121
    }
122
    
123
}
124