Passed
Push — master ( 8835cf...b0bd7c )
by Ruben
02:07
created

SiteAnalyzer::transform()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 2
dl 0
loc 14
rs 9.9666
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
11
use Exception;
12
13
/**
14
 * class SiteAnalyzer
15
 *
16
 * @package   SiteAnalyzer
17
 * @author    Ruben Dorado <[email protected]>
18
 * @copyright 2018 Ruben Dorado
19
 * @license   http://www.opensource.org/licenses/MIT The MIT License
20
 */
21
class SiteAnalyzer
22
{
23
    
24
    /*
25
     * @param 
26
     */
27
    public static function count($options = [])
28
    {	
29
        $config = SiteAnalyzer::loadConfig(array_key_exists('pdo', $options));
30
        
31
        if (array_key_exists('pdo', $options)) {            
32
            $pdo = $options['pdo'];	
33
        } else {            
34
            $pdo = Persistence::getPDO($config);
35
        }
36
        
37
        try {
38
            return Persistence::updateCount($pdo, $config, $options);
39
        } catch (Exception $e) {
40
            try {
41
                Persistence::crateDatabase($pdo, $config);
42
                return Persistence::updateCount($pdo, $config, $options);
43
            } catch (Exception $e) {
44
                throw new Exception("Site Analyzer could connect to the database.".$e->getMessage());
45
            };
46
            
47
        };
48
            
49
    }
50
    
51
    /*
52
     * @param $format string, one of [php-array, xml, json, txt-csv]
53
     */
54
    public static function loadConfig($pdoProvided = FALSE)
55
    {
56
        try {
57
            $config = new Configuration("../../../../site-analyzer.ini", $pdoProvided);
58
        } catch (Exception $e) {
59
            try {
60
                $config = new Configuration("site-analyzer.ini", $pdoProvided); 
61
            } catch (Exception $e) {
62
                throw new Exception("Config file not found.");
63
            }
64
        }        
65
        return $config;
66
    }
67
68
    /*
69
     * @param $format string, one of [php-array, xml, json, txt-csv]
70
     */
71
    public static function getStats($pdo = null)
72
    {   
73
        $config = SiteAnalyzer::loadConfig();
74
        //*$config = new Configuration("site-analyzer.ini", isset($pdo));
75
        if ($pdo==null) {
76
            $pdo = Persistence::getPDO($config);
77
        }
78
        
79
        $data = Persistence::getCounts($pdo, $config);
80
        return $data;
81
    } 
82
83
    /*
84
     * @param
85
     */
86
    public static function groupHitsByTime($options, $pdo = null)
87
    {
88
        $config = SiteAnalyzer::loadConfig();
89
        if ($pdo==null) {
90
            $pdo = Persistence::getPDO($config);
91
        }
92
        $data = OptionsDAO::getHitsWithOptions($pdo, $config);
93
        $resp = [];
94
        foreach ($data as $row) {
95
            $tmp = [$row['id']];
96
            $tmp = array_merge($tmp, getdate($row['time']));
97
            $resp[] = $tmp;
98
        }        
99
        return $resp;        
100
    }
101
    
102
    /*
103
     * @param
104
     */
105
    public static function groupHitsByUser($pdo = null)
106
    {
107
        $config = new Configuration("site-analyzer.ini", isset($pdo));
108
        if ($pdo==null) {
109
            $pdo = Persistence::getPDO($config);
110
        }
111
        $data = OptionsDAO::getHitsWithOptions($pdo, $config);        
112
        $count = [];
113
        foreach ($data as $row) {
114
            if (array_key_exists($row['user'], $count)) {
115
                $count[$row['user']]++;
116
            } else {
117
                $count[$row['user']] = 1;
118
            }            
119
        }
120
        
121
        $resp = [];
122
        foreach ($count as $user => $count) {
123
            $resp[] = [$user, $count];
124
        }
125
        return $resp;        
126
    }
127
    
128
    /*
129
     * @param $format string, one of [php-array, xml, json, txt-csv]
130
     */
131
    public static function transform($data, $format)
132
    {
133
        if ($format=="html") {
134
            $resp = "<table style='border-collapse: collapse;border: 1px solid black;'>";
135
            foreach ($data as $row) {
136
                $resp .= "<tr style='border: 1px solid black;'>";
137
                foreach ($row as $cell) {
138
                    $resp .= "<td style='border: 1px solid black;'>$cell</td>";
139
                }  
140
                $resp .= "</tr>";
141
            }
142
            return $resp."</table>";
143
        }
144
        return $data; 
145
    }
146
147
    /*
148
     * @param
149
     */
150
    public static function getTransitionMatrix($options)
151
    { 
152
           
153
    } 
154
155
    /*
156
     * @param
157
     */
158
    public static function performABTest($tests)
159
    {
160
        $testCounts = Persistence::getCountsByIds(array_keys($tests));
161
        $pairs = toPairs($tests);
162
        $data = Persistence::getFromByIds($pairs);
163
        
164
    } 
165
    
166
    /*
167
     * @param
168
     */
169
    public static function findVisitTimeProfiles($nprofiles, $options)
170
    {
171
        $config = SiteAnalyzer::loadConfig();
172
        $pdo = SiteAnalyzer::getPDO($config, $pdo);
173
        $data = Persistence::getOptions($pdo, $options);
174
        $data = Matrix::submatrix($data, [1]);
175
        $clusters = ML:kmeans($data, $nprofiles);
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected ':' on line 175 at column 22
Loading history...
176
        return $clusters;
177
    }
178
}
179
180