Passed
Push — master ( 7b565c...4193c1 )
by Ruben
02:15
created

UrlDAO::checkUrlTable()   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 UrlDAO
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 UrlDAO
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 checkUrlTable($pdo, $config) {
27
        try {
28
            $db_url_table = $config->getUrlTableName();
29
            $stmt = $pdo->prepare("SELECT * FROM $db_url_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 findHitIdsByUrl($pdo, $config, $url) {
43
        $resp = [];
44
        try {
45
            
46
            $dbtable = $config->getUrlTableName();
47
            $stmt = $pdo->prepare("SELECT id,url,count FROM $dbtable WHERE url = '$url'");
48
            if ($stmt->execute()) {
49
                while ($row = $stmt->fetch()) {
50
                    $resp[] = $row['id'];
51
                }
52
            }
53
        } catch (Exception $e) {
54
            throw new Exception("Error executing function 'findHitsByUrl'. ".$e->getMessage());
55
        }
56
        return $resp;        
57
    }
58
    
59
    /*
60
     * @param $pdo PDO
61
     * @param $config Configuration
62
     *
63
     */
64
    public static function findUrls($pdo, $config, $by = []) {
65
        $resp = [];
66
        try {
67
            $dbtable = $config->getUrlTableName();
68
            $qdata = [];
69
            $tquery = [];
70
            if (array_key_exists('id', $by)) {
71
                $qdata[] = $by['id'];
72
                $tquery[] = "id = ?";
73
            }
74
            
75
            if (array_key_exists('url', $by)) {
76
                $qdata[] = $by['url'];
77
                $tquery[] = "url = ?";
78
            }
79
            
80
            $sql = "SELECT id,url,count FROM $dbtable";
81
            if (count($tquery) > 0) {
82
                $sql = $sql." WHERE ".join(" AND ", $tquery);
83
            }
84
            
85
            $stmt = $pdo->prepare($sql);
86
            if ($stmt->execute($qdata)) {
87
                while ($row = $stmt->fetch()) {
88
                    $resp[] = [$row['id'], $row['url'], $row['count']];
89
                }
90
            }
91
            
92
        } catch (Exception $e) {
93
            throw new Exception("Error executing function 'getAllUrls'. ".$e->getMessage());
94
        }
95
        return $resp;
96
    }    
97
    
98
}
99