Passed
Push — master ( 1e101a...0f89a1 )
by Ruben
02:31 queued 55s
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
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 = 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

50
            /** @scrutinizer ignore-call */ 
51
            $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...
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 findByFrom($pdo, $config, $by = []) {
74
        $resp = [];
75
        try {
76
            $dbFromtable = $config->getFromTableName();
77
            $dbUrltable = $config->getUrlTableName();
78
            $qdata = [];
79
            $tquery = [];
80
            
81
            if (array_key_exists('url', $by) && array_key_exists('id', $by)) {
82
                $qdata = [$by['url'], $by['id']];
83
                $tquery = "SELECT f.* FROM  $dbFromtable f,$dbUrltable u WHERE (f.from_id = u.id and f.url = ?) or f.from_id = ?";                
84
            } else if (array_key_exists('url', $by)) {
85
                $qdata = [$by['url']];
86
                $tquery = "SELECT f.* FROM $dbFromtable f,$dbUrltable u where f.from_id = u.id and u.url = ?";
87
            } else if (array_key_exists('id', $by)) {
88
                $qdata = [$by['id']];
89
                $tquery = "SELECT f.* FROM $dbFromtable f where f.from_id = ?";
90
            } else {
91
                $qdata = [];
92
                $tquery = "SELECT f.* FROM $dbFromtable f";
93
            }
94
                                    
95
            $stmt = $pdo->prepare($tquery);
96
            if ($stmt->execute($qdata)) {
97
                while ($row = $stmt->fetch()) {
98
                    $resp[] = [$row['id'], $row['from_id'], $row['count']];
99
                }
100
            }
101
            
102
        } catch (Exception $e) {
103
            throw new Exception("Error executing function 'findByFrom'. ".$e->getMessage());
104
        }
105
        return $resp;
106
    }
107
    
108
}
109