-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTracker.php
99 lines (85 loc) · 2.13 KB
/
Tracker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
/**
* Created by PhpStorm.
* User: akatavic
* Date: 1/23/17
* Time: 11:44 PM
*/
namespace Econtext;
use Econtext\Session;
class Tracker
{
protected $session;
public static $defaultVars = [
'solved_captcha' => false,
'usage_search' => 0,
'usage_text' => 0,
'usage_url' => 0,
'usage_user' => 0,
];
public static $defaultLimits = [
'usage_search' => 10,
'usage_text' => 10,
'usage_url' => 10,
'usage_user' => 10,
];
public function __construct(Session $session)
{
$this->session = $session;
$this->boot();
}
protected function boot()
{
foreach (self::$defaultVars as $key => $default) {
if (!$this->session->get($key)) {
$this->session->add($key, $default);
}
}
}
public function add($key, $value)
{
$this->session->add($key, $value);
}
public function increment($key, $hits = 1)
{
$hits = (int)$hits;
if (false == ($val = $this->session->get($key))) {
return $this->add($key, $hits);
}
return $this->session->update($key, $val + $hits);
}
public function update($key, $value)
{
return $this->session->update($key, $value);
}
public function log($key, $value = 1)
{
if (is_int($value)) {
return $this->increment($key, $value);
}
return $this->update($key, $value);
}
public function get($key)
{
return $this->session->get($key);
}
public function all()
{
$output = [];
foreach (static::$defaultVars as $key => $default) {
$output[$key] = $this->session->get($key);
}
return $output;
}
public function hasReachedLimit($key)
{
if (!isset(static::$defaultLimits[$key])) {
throw new \Exception('There is no limit for the key: '.$key);
}
$currentValue = $this->session->get($key, 0);
if ($currentValue >= static::$defaultLimits[$key]) {
return true;
}
return false;
}
}