Passed
Push — master ( 21f337...1e101a )
by Ruben
01:59
created

FromDAO::countFrom()   B

Complexity

Conditions 7
Paths 30

Size

Total Lines 28
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 21
nc 30
nop 3
dl 0
loc 28
rs 8.6506
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
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
        $db_url_table = $config->getUrlTableName();
44
        $db_from_table = $config->getFromTableName();
45
        
46
        if (array_key_exists('id', $options)) {
47
            $id = $options['id'];
48
        } else {
49
            $id = $url;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $url seems to be never defined.
Loading history...
50
        }        
51
        
52
        if (array_key_exists('from_id', $options)) {
53
            $ids = [$options['from_id']];
54
        } else {
55
            $from_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'No referer info';
56
            $ids = HitDAO::findHitIdsByUrl($pdo, $config, $from_url); 
0 ignored issues
show
Bug introduced by
The method findHitIdsByUrl() does not exist on SiteAnalyzer\HitDAO. ( Ignorable by Annotation )

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

56
            /** @scrutinizer ignore-call */ 
57
            $ids = HitDAO::findHitIdsByUrl($pdo, $config, $from_url); 

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...
57
            if (count($ids)==0) {
58
                $stmt = $pdo->prepare("INSERT INTO $db_url_table (id, url, count) VALUES (?, ?, 1)");
59
                $stmt->execute([$from_url, $from_url]);
60
                $ids = [$from_url];
61
            }
62
        }
63
        
64
        foreach ($ids as $from_id) {
65
            $stmt = $pdo->prepare("UPDATE $db_from_table SET count = count + 1 WHERE id = ? and from_id = ?");
66
            $stmt->execute([$id, $from_id]);
67
            if ($stmt->rowCount()==0) {
68
                $stmt = $pdo->prepare("INSERT INTO $db_from_table (id, from_id, count) VALUES (?, ?, 1)");
69
                $stmt->execute([$id, $from_id]);
70
            }
71
        }
72
    }
73
        
74
    /*
75
     * @param $pdo \PDO
76
     * @param $config Configuration
77
     *
78
     */
79
    public static function findByFrom($pdo, $config, $by = []) {
80
        $resp = [];
81
        try {
82
            $dbFromtable = $config->getFromTableName();
83
            $dbUrltable = $config->getUrlTableName();
84
            $qdata = [];
85
            $tquery = [];
86
            
87
            if (array_key_exists('url', $by) && array_key_exists('id', $by)) {
88
                $qdata = [$by['url'], $by['id']];
89
                $tquery = "SELECT f.* FROM  $dbFromtable f,$dbUrltable u WHERE (f.from_id = u.id and f.url = ?) or f.from_id = ?";                
90
            } else if (array_key_exists('url', $by)) {
91
                $qdata = [$by['url']];
92
                $tquery = "SELECT f.* FROM $dbFromtable f,$dbUrltable u where f.from_id = u.id and u.url = ?";
93
            } else if (array_key_exists('id', $by)) {
94
                $qdata = [$by['id']];
95
                $tquery = "SELECT f.* FROM $dbFromtable f where f.from_id = ?";
96
            } else {
97
                $qdata = [];
98
                $tquery = "SELECT f.* FROM $dbFromtable f";
99
            }
100
                                    
101
            $stmt = $pdo->prepare($tquery);
102
            if ($stmt->execute($qdata)) {
103
                while ($row = $stmt->fetch()) {
104
                    $resp[] = [$row['id'], $row['from_id'], $row['count']];
105
                }
106
            }
107
            
108
        } catch (Exception $e) {
109
            throw new Exception("Error executing function 'findByFrom'. ".$e->getMessage());
110
        }
111
        return $resp;
112
    }
113
    
114
}
115