UrlDAO::findHitIdsByUrl()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 7
c 2
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 3
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
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
        $dbtable = $config->getUrlTableName();
45
        $stmt = $pdo->prepare("SELECT id,url,count FROM $dbtable WHERE url = '$url'");
46
        $stmt->execute();
47
        while ($row = $stmt->fetch()) {
48
            $resp[] = $row['id'];
49
        }
50
        return $resp;        
51
    }
52
    
53
    /*
54
     * @param $pdo \PDO
55
     * @param $config Configuration
56
     *
57
     */
58
    public static function findUrls($pdo, $config, $by = []) {
59
        $resp = [];
60
        $dbtable = $config->getUrlTableName();
61
        $qdata = [];
62
        $tquery = [];
63
        if (array_key_exists('id', $by)) {
64
            $qdata[] = $by['id'];
65
            $tquery[] = "id = ?";
66
        }
67
            
68
        if (array_key_exists('url', $by)) {
69
            $qdata[] = $by['url'];
70
            $tquery[] = "url = ?";
71
        }
72
            
73
        $sql = "SELECT id,url,count FROM $dbtable";
74
        if (count($tquery) > 0) {
75
            $sql = $sql." WHERE ".join(" AND ", $tquery);
76
        }
77
            
78
        $stmt = $pdo->prepare($sql);
79
        $stmt->execute($qdata);
80
        while ($row = $stmt->fetch()) {
81
            $resp[] = [$row['id'], $row['url'], $row['count']];           
82
        }
83
         
84
        return $resp;
85
    }    
86
    
87
}
88