Passed
Push — master ( 4193c1...90d032 )
by Ruben
04:00 queued 02:02
created

FromDAO::findByFrom()   B

Complexity

Conditions 8
Paths 40

Size

Total Lines 33
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 25
nc 40
nop 3
dl 0
loc 33
rs 8.4444
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 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
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 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, $options = []) {
43
        if (array_key_exists('from_id', $options)) {
44
            $ids = [$options['from_id']];
45
        } else {
46
            $from_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'No referer info';
47
            $ids = Persistence::findHitIdsByUrl($pdo, $config, $from_url); 
48
            if (count($ids)==0) {
49
                $stmt = $pdo->prepare("INSERT INTO $db_url_table (id, url, count) VALUES (?, ?, 1)");
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $db_url_table seems to be never defined.
Loading history...
50
                $stmt->execute([$from_url, $from_url]);
51
                $ids = [$from_url];
52
            }
53
        }
54
        foreach ($ids as $from_id) {
55
            $stmt = $pdo->prepare("UPDATE $db_from_table SET count = count + 1 WHERE id = ? and from_id = ?");
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $db_from_table seems to be never defined.
Loading history...
56
            $stmt->execute([$id, $from_id]);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $id does not exist. Did you maybe mean $ids?
Loading history...
57
            if ($stmt->rowCount()==0) {
58
                $stmt = $pdo->prepare("INSERT INTO $db_from_table (id, from_id, count) VALUES (?, ?, 1)");
59
                $stmt->execute([$id, $from_id]);
60
            }
61
        }
62
    }
63
        
64
    /*
65
     * @param $pdo PDO
66
     * @param $config Configuration
67
     *
68
     */
69
    public static function findByFrom($pdo, $config, $by = []) {
70
        $resp = [];
71
        try {
72
            $dbFromtable = $config->getFromTableName();
73
            $dbUrltable = $config->getUrlTableName();
74
            $qdata = [];
75
            $tquery = [];
76
            
77
            if (array_key_exists('url', $by) && array_key_exists('id', $by)) {
78
                $qdata = [$by['url'], $by['id']];
79
                $tquery = "SELECT f.* FROM  $dbFromtable f,$dbUrltable u WHERE (f.from_id = u.id and f.url = ?) or f.from_id = ?";                
80
            } else if (array_key_exists('url', $by)) {
81
                $qdata = [$by['url']];
82
                $tquery = "SELECT f.* FROM $dbFromtable f,$dbUrltable u where f.from_id = u.id and u.url = ?";
83
            } else if (array_key_exists('id', $by)) {
84
                $qdata = [$by['id']];
85
                $tquery = "SELECT f.* FROM $dbFromtable f where f.from_id = ?";
86
            } else {
87
                $qdata = [];
88
                $tquery = "SELECT f.* FROM $dbFromtable f";
89
            }
90
                                    
91
            $stmt = $pdo->prepare($tquery);
92
            if ($stmt->execute($qdata)) {
93
                while ($row = $stmt->fetch()) {
94
                    $resp[] = [$row['id'], $row['from_id'], $row['count']];
95
                }
96
            }
97
            
98
        } catch (Exception $e) {
99
            throw new Exception("Error executing function 'findByFrom'. ".$e->getMessage());
100
        }
101
        return $resp;
102
    }
103
    
104
}
105