Passed
Push — master ( 4052d5...8cbf73 )
by Ruben
09:06 queued 07:04
created

Persistence::findHitIdsByUrl()   A

Complexity

Conditions 4
Paths 9

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 9
nop 3
dl 0
loc 15
rs 9.9332
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
use PDO;
13
14
/**
15
 * class Persistence
16
 *
17
 * @package   SiteAnalyzer
18
 * @author    Ruben Dorado <[email protected]>
19
 * @copyright 2018 Ruben Dorado
20
 * @license   http://www.opensource.org/licenses/MIT The MIT License
21
 */
22
class Persistence
23
{
24
25
    /*
26
     * @param Configuration $config
27
     *
28
     * @return PDO
29
     */
30
    public static function getPDO($config) {
31
        $options = array(
32
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
33
        );
34
        
35
        if ($config->getDsn()) {
36
            try {
37
                return new PDO($config->getDsn(), $config->getUser(), $config->getPassword(), $options);
38
            } catch (Exception $e) {                
39
                if (!$config->getUseOnMemoryDB()) {
40
                    throw new Exception("Could not create a db connection. Check permissions, configuration, and documentation. ".$e->getMessage());
41
                }
42
            }
43
        }
44
        
45
        if ($config->getUseOnMemoryDB()) {
46
            try {
47
                return new PDO("sqlite::memory:", null, null, $options);
48
            } catch (Exception $e) {
49
                throw new Exception("Could not create a db connection. Check permissions, configuration, and documentation. ".$e->getMessage());                
50
            }
51
        }
52
        throw new Exception("Error when trying to obtain a connection to a database. Check the configuration. ");
53
54
    }
55
56
    /*
57
     * @param $pdo PDO
58
     * @param $config Configuration
59
     *
60
     */
61
    public static function crateDatabase($pdo, $config) {
62
        try {
63
            $db_hit_table = $config->getHitTableName();
64
            $db_options_table = $config->getOptionsTableName();           
65
            $db_from_table = $config->getFromTableName();
66
            $db_url_table = $config->getUrlTableName();
67
68
            $stmt = $pdo->prepare("CREATE TABLE $db_hit_table (id VARCHAR(255), count INT)");
69
            $stmt->execute();
70
            $stmt = $pdo->prepare("CREATE TABLE $db_options_table (id VARCHAR(255), time TIMESTAMP, user VARCHAR(255))");
71
            $stmt->execute();
72
            $stmt = $pdo->prepare("CREATE TABLE $db_from_table (id VARCHAR(255), from_id VARCHAR(255), count INT)");
73
            $stmt->execute();
74
            $stmt = $pdo->prepare("CREATE TABLE $db_url_table (id VARCHAR(255), url VARCHAR(255), count INT)");
75
            $stmt->execute();
76
        } catch (Exception $e) {
77
            throw new Exception("Could not create the database. ".$e->getMessage());
78
        }        
79
        return true;
80
    }
81
82
    /*
83
     * @param $pdo PDO
84
     * @param $config Configuration
85
     *
86
     */
87
    public static function deleteDatabase($pdo, $config) {
88
        $resp = true;
89
        
90
        $db_hit_table = $config->getHitTableName();
91
        $db_options_table = $config->getOptionsTableName();
92
        $db_from_table = $config->getFromTableName();
93
        $db_url_table = $config->getUrlTableName();
94
        
95
        $resp = $resp && Persistence::dropTable($pdo, $db_hit_table);
96
        $resp = $resp && Persistence::dropTable($pdo, $db_options_table);
97
        $resp = $resp && Persistence::dropTable($pdo, $db_from_table);
98
        $resp = $resp && Persistence::dropTable($pdo, $db_url_table);
99
        
100
        return $resp;
101
    }
102
103
    /*
104
     * @param $pdo PDO
105
     * @param $config Configuration
106
     *
107
     */
108
    private static function dropTable($pdo, $tableName) {
109
        try {            
110
            $stmt = $pdo->prepare("DROP TABLE $tableName");
111
            $stmt->execute();
112
           
113
        } catch (Exception $e) {
114
            throw new Exception("Problem deleting the table $tableName. ".$e->getMessage());
115
        }
116
        return true;
117
    }
118
    
119
    /*
120
     * @param $pdo PDO
121
     * @param $config Configuration
122
     *
123
     */
124
    public static function checkTables($pdo, $config) {
125
        $resp = true;      
126
        try {
127
            
128
            $resp = $resp && Persistence::checkHitTable($pdo, $config);
0 ignored issues
show
Bug introduced by
The method checkHitTable() does not exist on SiteAnalyzer\Persistence. Did you maybe mean checkTables()? ( Ignorable by Annotation )

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

128
            $resp = $resp && Persistence::/** @scrutinizer ignore-call */ checkHitTable($pdo, $config);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
129
            $resp = $resp && Persistence::checkOptionsTable($pdo, $config);
0 ignored issues
show
Bug introduced by
The method checkOptionsTable() does not exist on SiteAnalyzer\Persistence. ( Ignorable by Annotation )

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

129
            $resp = $resp && Persistence::/** @scrutinizer ignore-call */ checkOptionsTable($pdo, $config);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
130
            $resp = $resp && Persistence::checkFromTable($pdo, $config);
0 ignored issues
show
Bug introduced by
The method checkFromTable() does not exist on SiteAnalyzer\Persistence. Did you maybe mean checkTables()? ( Ignorable by Annotation )

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

130
            $resp = $resp && Persistence::/** @scrutinizer ignore-call */ checkFromTable($pdo, $config);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
131
            $resp = $resp && Persistence::checkUrlTable($pdo, $config);
132
        } catch (Exception $e) {
133
            return false;
134
        }        
135
        return $resp;
136
137
    }
138
    
139
    /*
140
     * @param $pdo PDO
141
     * @param $config Configuration
142
     *
143
     */
144
    public static function updateCount($pdo, $config, $options = []) {
145
146
        $db_hit_table = $config->getHitTableName();
0 ignored issues
show
Unused Code introduced by
The assignment to $db_hit_table is dead and can be removed.
Loading history...
147
        $db_options_table = $config->getOptionsTableName();
0 ignored issues
show
Unused Code introduced by
The assignment to $db_options_table is dead and can be removed.
Loading history...
148
        $db_from_table = $config->getFromTableName();
0 ignored issues
show
Unused Code introduced by
The assignment to $db_from_table is dead and can be removed.
Loading history...
149
        $db_url_table = $config->getUrlTableName();
0 ignored issues
show
Unused Code introduced by
The assignment to $db_url_table is dead and can be removed.
Loading history...
150
151
        $store_from = true;
0 ignored issues
show
Unused Code introduced by
The assignment to $store_from is dead and can be removed.
Loading history...
152
        $store_time = true;
0 ignored issues
show
Unused Code introduced by
The assignment to $store_time is dead and can be removed.
Loading history...
153
        $store_user = true;
0 ignored issues
show
Unused Code introduced by
The assignment to $store_user is dead and can be removed.
Loading history...
154
        
155
        Persistence::countHit($pdo, $config, $options);
0 ignored issues
show
Bug introduced by
The method countHit() does not exist on SiteAnalyzer\Persistence. ( Ignorable by Annotation )

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

155
        Persistence::/** @scrutinizer ignore-call */ 
156
                     countHit($pdo, $config, $options);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
156
        Persistence::countFrom($pdo, $config, $options);           
0 ignored issues
show
Bug introduced by
The method countFrom() does not exist on SiteAnalyzer\Persistence. ( Ignorable by Annotation )

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

156
        Persistence::/** @scrutinizer ignore-call */ 
157
                     countFrom($pdo, $config, $options);           

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
157
        Persistence::countOptions($pdo, $config, $options); 
0 ignored issues
show
Bug introduced by
The method countOptions() does not exist on SiteAnalyzer\Persistence. ( Ignorable by Annotation )

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

157
        Persistence::/** @scrutinizer ignore-call */ 
158
                     countOptions($pdo, $config, $options); 

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
158
        
159
        return true;
160
    }
161
162
    /*
163
     * @param $pdo PDO
164
     * @param $config Configuration
165
     *
166
     */
167
    public static function getCounts($pdo, $config)
168
    {
169
        $resp = [];
170
        try {
171
172
            $dbHitTable = $config->getHitTableName();
173
            $dbUrlTable = $config->getUrlTableName();
174
            $stmt = $pdo->prepare("SELECT h.id, u.url, h.count FROM $dbHitTable h, $dbUrlTable u WHERE h.id=u.id");
175
            if ($stmt->execute()) {
176
                while ($row = $stmt->fetch()) {
177
                    $resp[] = [$row[0], $row[1], $row[2]];
178
                }
179
            }
180
            
181
        } catch (Exception $e) {
182
            throw new Exception("Error reading the database. Method getCounts().".$e->getMessage());
183
        }        
184
        return $resp;
185
    }
186
187
    /*
188
     * @param $pdo PDO
189
     * @param $config Configuration
190
     */
191
    public static function checkUrlTable($pdo, $config) {
192
        return UrlDAO::checkUrlTable($pdo, $config);
193
    }
194
    
195
    /*
196
     * @param $pdo PDO
197
     * @param $config Configuration
198
     *
199
     */    
200
    public static function getCountsById($pdo, $config) {
0 ignored issues
show
Unused Code introduced by
The parameter $pdo 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

200
    public static function getCountsById(/** @scrutinizer ignore-unused */ $pdo, $config) {

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...
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

200
    public static function getCountsById($pdo, /** @scrutinizer ignore-unused */ $config) {

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...
201
        return "Works";
202
    }
203
    
204
}
205
206
207
            
208