FromDAO   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 8
Bugs 0 Features 1
Metric Value
wmc 20
eloc 67
c 8
b 0
f 1
dl 0
loc 131
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A findByFromById() 0 17 3
A countFrom() 0 22 6
A checkFromTable() 0 9 2
A findByFrom() 0 27 5
A findAll() 0 18 4
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 FromDAO
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 FromDAO
20
{
21
22
    /*
23
     * @param $pdo \PDO
24
     * @param $config Configuration
25
     */
26
    public static function checkFromTable($pdo, $config) {
27
        try {
28
            $db_from_table = $config->getFromTableName();
29
            $stmt = $pdo->prepare("SELECT * FROM $db_from_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 countFrom($pdo, $config, $id, $options = []) {
43
        $db_url_table = $config->getUrlTableName();
44
        $db_from_table = $config->getFromTableName(); 
45
        
46
        if (array_key_exists('from_id', $options)) {
47
            $ids = [$options['from_id']];
48
        } else {
49
            $from_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'No referer info';
50
            $ids = UrlDAO::findHitIdsByUrl($pdo, $config, $from_url); 
51
            if (count($ids)==0) {
52
                $stmt = $pdo->prepare("INSERT INTO $db_url_table (id, url, count) VALUES (?, ?, 1)");
53
                $stmt->execute([$from_url, $from_url]);
54
                $ids = [$from_url];
55
            }
56
        }
57
        
58
        foreach ($ids as $from_id) {
59
            $stmt = $pdo->prepare("UPDATE $db_from_table SET count = count + 1 WHERE id = ? and from_id = ?");
60
            $stmt->execute([$id, $from_id]);
61
            if ($stmt->rowCount()==0) {
62
                $stmt = $pdo->prepare("INSERT INTO $db_from_table (id, from_id, count) VALUES (?, ?, 1)");
63
                $stmt->execute([$id, $from_id]);
64
            }
65
        }
66
    }
67
68
    /*
69
     * @param $pdo \PDO
70
     * @param $config Configuration
71
     *
72
     */
73
    public static function findByFromById($pdo, $config, $id, $from_id = null) {
74
        $resp = [];
75
        
76
        $dbFromtable = $config->getFromTableName();
77
        $qdata = [$id];
78
        $tquery = "SELECT f.* FROM $dbFromtable f WHERE f.id = ?";
79
        if ($from_id != null) {
80
            $tquery = $tquery." AND f.from_id = ?";
81
            $qdata[] = $from_id;
82
        }
83
        $stmt = $pdo->prepare($tquery);
84
        $stmt->execute($qdata);
85
        while ($row = $stmt->fetch()) {
86
            $resp[] = [$row['id'], $row['from_id'], $row['count']];
87
        }
88
        
89
        return $resp;
90
    }
91
    
92
    /*
93
     * @param $pdo \PDO
94
     * @param $config Configuration
95
     *
96
     */
97
    public static function findByFrom($pdo, $config, $by = []) {
98
        $resp = [];
99
100
        $dbFromtable = $config->getFromTableName();
101
        $dbUrltable = $config->getUrlTableName();
102
        $qdata = [];
103
        $tquery = "SELECT f.* FROM $dbFromtable f";
104
            
105
        if (array_key_exists('url', $by)) {
106
            $qdata = [$by['url']];
107
            $tquery = "SELECT f.* FROM $dbFromtable f,$dbUrltable u where f.from_id = u.id and u.url = ?";            
108
            if (array_key_exists('id', $by)) {
109
                $qdata = [$by['url'], $by['id']];
110
                $tquery = "SELECT f.* FROM  $dbFromtable f,$dbUrltable u WHERE (f.from_id = u.id and f.url = ?) or f.from_id = ?";                                
111
            }
112
        } else if (array_key_exists('id', $by)) {
113
            $qdata = [$by['id']];
114
            $tquery = "SELECT f.* FROM $dbFromtable f where f.from_id = ?";
115
        } 
116
                                    
117
        $stmt = $pdo->prepare($tquery);
118
        $stmt->execute($qdata);        
119
        while ($row = $stmt->fetch()) {
120
            $resp[] = [$row['id'], $row['from_id'], $row['count']];            
121
        }
122
            
123
        return $resp;
124
    }
125
126
    
127
    /*
128
     * @param $pdo \PDO
129
     * @param $config Configuration
130
     *
131
     */
132
    public static function findAll($pdo, $config) {
133
        $resp = [];
134
        try {
135
            $dbFromtable = $config->getFromTableName();
136
            $query = "SELECT f.* FROM  $dbFromtable";
137
            
138
            
139
            $stmt = $pdo->prepare($query);
140
            if ($stmt->execute()) {
141
                while ($row = $stmt->fetch()) {
142
                    $resp[] = [$row['id'], $row['from_id'], $row['count']];
143
                }
144
            }
145
            
146
        } catch (Exception $e) {
147
            throw new Exception("Error executing function 'findAll'. ".$e->getMessage());
148
        }
149
        return $resp;
150
    }
151
    
152
    
153
}
154