Passed
Push — master ( 31fead...1101eb )
by Ruben
02:09
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 resetDatabase($options = [])
55
    {
56
        $config = SiteAnalyzer::loadConfig();
57
        $pdo = SiteAnalyzer::getPDO($config, $options);
58
        
59
        Persistence::deleteDatabase($pdo, $config);
60
        Persistence::crateDatabase($pdo, $config);
61
    }
62
    
63
    /*
64
     * @param $format string, one of [php-array, xml, json, txt-csv]
65
     */
66
    public static function loadConfig($pdoProvided = FALSE)
67
    {
68
        try {
69
            $config = new Configuration("../../../../site-analyzer.ini", $pdoProvided);
70
        } catch (Exception $e) {
71
            try {
72
                $config = new Configuration("site-analyzer.ini", $pdoProvided); 
73
            } catch (Exception $e) {
74
                throw new Exception("Config file not found.");
75
            }
76
        }        
77
        return $config;
78
    }
79
80
    /*
81
     * @param 
82
     */
83
    public static function getPDO($config, $options)
84
    {
85
        if (array_key_exists("pdo",$options)) {
86
            return $options["pdo"];
87
        }         
88
        return  Persistence::getPDO($config);
89
    }
90
    
91
    /*
92
     * @param $format string, one of [php-array, xml, json, txt-csv]
93
     */
94
    public static function getStats($options = [])
95
    {   
96
        $config = SiteAnalyzer::loadConfig();
97
        $pdo = SiteAnalyzer::getPDO($config, $options);
98
        
99
        $data = Persistence::getCounts($pdo, $config);
100
        return $data;
101
    } 
102
103
    /*
104
     * @param
105
     */
106
    public static function groupHitsByTime($options = [])
107
    {
108
        $config = SiteAnalyzer::loadConfig();
109
        $pdo = SiteAnalyzer::getPDO($config, $options);
110
        
111
        $data = OptionsDAO::getHitsWithOptions($pdo, $config);
112
        $resp = [];
113
        foreach ($data as $row) {
114
            $tmp = [$row['id']];
115
            $tmp = array_merge($tmp, getdate($row['time']));
116
            $resp[] = $tmp;
117
        }        
118
        return $resp;        
119
    }
120
    
121
    /*
122
     * @param
123
     */
124
    public static function groupHitsByUser($options = [])
125
    {
126
        $config = SiteAnalyzer::loadConfig();
127
        $pdo = SiteAnalyzer::getPDO($config, $options);
128
        
129
        $data = OptionsDAO::getHitsWithOptions($pdo, $config);        
130
        $count = [];
131
        foreach ($data as $row) {
132
            if (array_key_exists($row['user'], $count)) {
133
                $count[$row['user']]++;
134
            } else {
135
                $count[$row['user']] = 1;
136
            }            
137
        }
138
        
139
        $resp = [];
140
        foreach ($count as $user => $count) {
141
            $resp[] = [$user, $count];
142
        }
143
        return $resp;        
144
    }
145
    
146
    /*
147
     * @param $format string, one of [php-array, xml, json, txt-csv]
148
     */
149
    public static function transform($data, $format)
150
    {
151
        if ($format=="html") {
152
            $resp = "<table style='border-collapse: collapse;border: 1px solid black;'>";
153
            foreach ($data as $row) {
154
                $resp .= "<tr style='border: 1px solid black;'>";
155
                foreach ($row as $cell) {
156
                    $resp .= "<td style='border: 1px solid black;'>$cell</td>";
157
                }  
158
                $resp .= "</tr>";
159
            }
160
            return $resp."</table>";
161
        }
162
        return $data; 
163
    }
164
165
    /*
166
     * @param
167
     */
168
    public static function getTransitionMatrix($options = [])
169
    { 
170
        $config = SiteAnalyzer::loadConfig();
171
        $pdo = SiteAnalyzer::getPDO($config, $options);
172
        
173
        $targetCounts = Persistence::findByFrom($pdo, $config);
174
        
175
        $data = Matrix::submatrix($targetCounts, [1, 0, 2]);        
176
        $data = Matrix::toSquareMatrix($data, 0, 1, 2);
177
        $labels = Matrix::arrayToMatrix($data["labels"]);
178
        $data = $data["data"];
179
        $data = Matrix::toBinary($data);
180
        
181
        if (array_key_exists("level", $options)) {
182
            $level = $options["level"];
183
        }
184
        else {
185
            $level = count($labels) - 1;
186
        }
187
        
188
        $result = $data;
189
        for ($i=1;$i<$level;$i++) {
190
            $tmp = Matrix::multiply($result, $data);
191
            $result = Matrix::sum($result, $tmp);
192
        }
193
        
194
        $result = Matrix::toBinary($result);
195
        return ["labels"=>$labels, "data"=>$result];
196
    } 
197
198
    /*
199
     * @param
200
     */
201
    public static function getTransitionCounts($options = [])
202
    {
203
        $config = SiteAnalyzer::loadConfig();
204
        $pdo = SiteAnalyzer::getPDO($config, $options);
205
        
206
        $targetCounts = Persistence::findByFrom($pdo, $config);
207
        
208
        $data = Matrix::submatrix($targetCounts, [1, 0, 2]);
209
        $data = Matrix::toSquareMatrix($data, 0, 1, 2);
210
        $labels = Matrix::arrayToMatrix($data["labels"]);
211
        
212
        return ["data"=>$data["data"], "labels"=>$labels];
213
    } 
214
    
215
    /*
216
     * @param
217
     */
218
    public static function performABTest($tests, $options)
219
    {
220
        $config = SiteAnalyzer::loadConfig();
221
        $pdo = SiteAnalyzer::getPDO($config, $options);
222
        
223
        $testIds = getTestIds($tests);
0 ignored issues
show
Bug introduced by
The function getTestIds was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

223
        $testIds = /** @scrutinizer ignore-call */ getTestIds($tests);
Loading history...
224
        $testCounts = Persistence::getCountsByIds($pdo, $testIds);                
0 ignored issues
show
Bug introduced by
The method getCountsByIds() does not exist on SiteAnalyzer\Persistence. Did you maybe mean getCounts()? ( Ignorable by Annotation )

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

224
        /** @scrutinizer ignore-call */ 
225
        $testCounts = Persistence::getCountsByIds($pdo, $testIds);                

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...
225
        $targetCounts = Persistence::getFromByIds($pdo, $pairs);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pairs seems to be never defined.
Loading history...
Bug introduced by
The method getFromByIds() does not exist on SiteAnalyzer\Persistence. ( Ignorable by Annotation )

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

225
        /** @scrutinizer ignore-call */ 
226
        $targetCounts = Persistence::getFromByIds($pdo, $pairs);

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...
226
        $result = Statistics::ABtest($testCounts, $targetCounts, $options);
0 ignored issues
show
Bug Best Practice introduced by
The method SiteAnalyzer\Statistics::ABtest() is not static, but was called statically. ( Ignorable by Annotation )

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

226
        /** @scrutinizer ignore-call */ 
227
        $result = Statistics::ABtest($testCounts, $targetCounts, $options);
Loading history...
227
        
228
        return $result;
229
    } 
230
    
231
    /*
232
     * @param
233
     */
234
    public static function findVisitTimeProfiles($nprofiles, $options)
235
    {
236
        $config = SiteAnalyzer::loadConfig();
237
        $pdo = SiteAnalyzer::getPDO($config, $pdo);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pdo seems to be never defined.
Loading history...
238
        $data = Persistence::getOptions($pdo, $options);
0 ignored issues
show
Bug introduced by
The method getOptions() does not exist on SiteAnalyzer\Persistence. ( Ignorable by Annotation )

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

238
        /** @scrutinizer ignore-call */ 
239
        $data = Persistence::getOptions($pdo, $options);

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...
239
        $data = Matrix::submatrix($data, [1]);
240
        $clusters = ML::kmeans($data, $nprofiles);
0 ignored issues
show
Bug Best Practice introduced by
The method SiteAnalyzer\ML::kmeans() is not static, but was called statically. ( Ignorable by Annotation )

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

240
        /** @scrutinizer ignore-call */ 
241
        $clusters = ML::kmeans($data, $nprofiles);
Loading history...
Bug introduced by
Are you sure the assignment to $clusters is correct as SiteAnalyzer\ML::kmeans($data, $nprofiles) targeting SiteAnalyzer\ML::kmeans() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
241
        return $clusters;
242
    }
243
}
244
245