1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace awssat\Visits; |
4
|
|
|
|
5
|
|
|
use awssat\Visits\Traits\Lists; |
6
|
|
|
use awssat\Visits\Traits\Periods; |
7
|
|
|
use awssat\Visits\Traits\Record; |
8
|
|
|
use awssat\Visits\Traits\Setters; |
9
|
|
|
use Illuminate\Support\Carbon; |
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
11
|
|
|
use Illuminate\Support\Arr; |
12
|
|
|
use Illuminate\Support\Facades\Redis; |
13
|
|
|
use Jaybizzle\CrawlerDetect\CrawlerDetect; |
14
|
|
|
|
15
|
|
|
class Visits |
16
|
|
|
{ |
17
|
|
|
use Record, Lists, Periods, Setters; |
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var mixed |
21
|
|
|
*/ |
22
|
|
|
protected $ipSeconds; |
23
|
|
|
/** |
24
|
|
|
* @var null |
25
|
|
|
*/ |
26
|
|
|
protected $subject; |
27
|
|
|
/** |
28
|
|
|
* @var bool|mixed |
29
|
|
|
*/ |
30
|
|
|
protected $fresh = false; |
31
|
|
|
/** |
32
|
|
|
* @var null|string |
33
|
|
|
*/ |
34
|
|
|
protected $country = null; |
35
|
|
|
/** |
36
|
|
|
* @var null|string |
37
|
|
|
*/ |
38
|
|
|
protected $referer = null; |
39
|
|
|
/** |
40
|
|
|
* @var null|string |
41
|
|
|
*/ |
42
|
|
|
protected $operatingSystem = null; |
43
|
|
|
/** |
44
|
|
|
* @var null|string |
45
|
|
|
*/ |
46
|
|
|
protected $language = null; |
47
|
|
|
/** |
48
|
|
|
* @var mixed |
49
|
|
|
*/ |
50
|
|
|
protected $periods; |
51
|
|
|
/** |
52
|
|
|
* @var Keys |
53
|
|
|
*/ |
54
|
|
|
protected $keys; |
55
|
|
|
/** |
56
|
|
|
* @var Redis |
57
|
|
|
*/ |
58
|
|
|
public $redis; |
59
|
|
|
/** |
60
|
|
|
* @var boolean |
61
|
|
|
*/ |
62
|
|
|
public $ignoreCrawlers = false; |
63
|
|
|
/** |
64
|
|
|
* @var array |
65
|
|
|
*/ |
66
|
|
|
public $globalIgnore = []; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Visits constructor. |
70
|
|
|
* @param $subject |
71
|
|
|
* @param string $tag|null |
72
|
|
|
*/ |
73
|
|
|
public function __construct($subject = null, $tag = 'visits') |
74
|
|
|
{ |
75
|
|
|
$config = config('visits'); |
76
|
|
|
$this->redis = Redis::connection($config['connection']); |
|
|
|
|
77
|
|
|
$this->periods = $config['periods']; |
78
|
|
|
$this->ipSeconds = $config['remember_ip']; |
79
|
|
|
$this->fresh = $config['always_fresh']; |
80
|
|
|
$this->ignoreCrawlers = $config['ignore_crawlers']; |
81
|
|
|
$this->globalIgnore = $config['global_ignore']; |
82
|
|
|
$this->subject = $subject; |
83
|
|
|
$this->keys = new Keys($subject, $tag); |
84
|
|
|
|
85
|
|
|
$this->periodsSync(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param $subject |
90
|
|
|
* @return $this |
91
|
|
|
*/ |
92
|
|
|
public function by($subject) |
93
|
|
|
{ |
94
|
|
|
if($subject instanceof Model) { |
95
|
|
|
$this->keys->append($this->keys->modelName($subject), $subject->{$subject->getKeyName()}); |
96
|
|
|
} else if (is_array($subject)) { |
97
|
|
|
$this->keys->append(array_keys($subject)[0], Arr::first($subject)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Reset methods |
105
|
|
|
* |
106
|
|
|
* @param $method |
107
|
|
|
* @param string $args |
108
|
|
|
* @return Reset |
109
|
|
|
*/ |
110
|
|
|
public function reset($method = 'visits', $args = '') |
111
|
|
|
{ |
112
|
|
|
return new Reset($this, $method, $args); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Check for the ip is has been recorded before |
117
|
|
|
* |
118
|
|
|
* @return bool |
119
|
|
|
* @internal param $subject |
120
|
|
|
*/ |
121
|
|
|
public function recordedIp() |
122
|
|
|
{ |
123
|
|
|
return ! $this->redis->set($this->keys->ip(request()->ip()), true, 'EX', $this->ipSeconds, 'NX'); |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Get visits of model instance. |
128
|
|
|
* |
129
|
|
|
* @return mixed |
130
|
|
|
* @internal param $subject |
131
|
|
|
*/ |
132
|
|
|
public function count() |
133
|
|
|
{ |
134
|
|
|
if ($this->country) { |
135
|
|
|
return $this->redis->zscore($this->keys->visits . "_countries:{$this->keys->id}", $this->country); |
|
|
|
|
136
|
|
|
} else if ($this->referer) { |
137
|
|
|
return $this->redis->zscore($this->keys->visits . "_referers:{$this->keys->id}", $this->referer); |
138
|
|
|
} else if ($this->operatingSystem) { |
139
|
|
|
return $this->redis->zscore($this->keys->visits . "_OSes:{$this->keys->id}", $this->operatingSystem); |
140
|
|
|
} else if ($this->language) { |
141
|
|
|
return $this->redis->zscore($this->keys->visits . "_languages:{$this->keys->id}", $this->language); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return intval( |
145
|
|
|
$this->keys->instanceOfModel ? |
146
|
|
|
$this->redis->zscore($this->keys->visits, $this->keys->id) : |
147
|
|
|
$this->redis->get($this->keys->visitsTotal()) |
|
|
|
|
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* use diffForHumans to show diff |
153
|
|
|
* @return Carbon |
154
|
|
|
*/ |
155
|
|
|
public function timeLeft() |
156
|
|
|
{ |
157
|
|
|
return Carbon::now()->addSeconds($this->redis->ttl($this->keys->visits)); |
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* use diffForHumans to show diff |
162
|
|
|
* @return Carbon |
163
|
|
|
*/ |
164
|
|
|
public function ipTimeLeft() |
165
|
|
|
{ |
166
|
|
|
return Carbon::now()->addSeconds($this->redis->ttl($this->keys->ip(request()->ip()))); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
protected function isCrawler() |
170
|
|
|
{ |
171
|
|
|
return $this->ignoreCrawlers && app(CrawlerDetect::class)->isCrawler(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Increment a new/old subject to the cache. |
176
|
|
|
* |
177
|
|
|
* @param int $inc |
178
|
|
|
* @param bool $force |
179
|
|
|
* @param bool $periods |
180
|
|
|
* @param array $ignore to ignore recording visits of periods, country, refer, language and operatingSystem. pass them on this array. |
181
|
|
|
*/ |
182
|
|
|
public function increment($inc = 1, $force = false, $ignore = []) |
183
|
|
|
{ |
184
|
|
|
if ($force || (!$this->isCrawler() && !$this->recordedIp())) { |
185
|
|
|
$this->redis->zincrby($this->keys->visits, $inc, $this->keys->id); |
|
|
|
|
186
|
|
|
$this->redis->incrby($this->keys->visitsTotal(), $inc); |
|
|
|
|
187
|
|
|
|
188
|
|
|
if(is_array($this->globalIgnore) && sizeof($this->globalIgnore) > 0) { |
189
|
|
|
$ignore = array_merge($ignore, $this->globalIgnore); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
//NOTE: $$method is parameter also .. ($periods,$country,$refer) |
193
|
|
|
foreach (['country', 'refer', 'periods', 'operatingSystem', 'language'] as $method) { |
194
|
|
|
if(! in_array($method, $ignore)) { |
195
|
|
|
$this->{'record'.studly_case($method)}($inc); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param int $inc |
203
|
|
|
* @param array $ignore to ignore recording visits like country, periods ... |
204
|
|
|
*/ |
205
|
|
|
public function forceIncrement($inc = 1, $ignore = []) |
206
|
|
|
{ |
207
|
|
|
$this->increment($inc, true, $ignore); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Decrement a new/old subject to the cache cache. |
212
|
|
|
* |
213
|
|
|
* @param int $dec |
214
|
|
|
* @param array $ignore to ignore recording visits like country, periods ... |
215
|
|
|
*/ |
216
|
|
|
public function decrement($dec = 1, $force = false, $ignore = []) |
217
|
|
|
{ |
218
|
|
|
$this->increment(-$dec, $force, $ignore); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param int $dec |
223
|
|
|
* @param array $ignore to ignore recording visits like country, periods ... |
224
|
|
|
*/ |
225
|
|
|
public function forceDecrement($dec = 1, $ignore = []) |
226
|
|
|
{ |
227
|
|
|
$this->decrement($dec, true, $ignore); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param $period |
232
|
|
|
* @param int $time |
233
|
|
|
* @return bool |
234
|
|
|
*/ |
235
|
|
|
public function expireAt($period, $time) |
236
|
|
|
{ |
237
|
|
|
$periodKey = $this->keys->period($period); |
238
|
|
|
return $this->redis->expire($periodKey, $time); |
|
|
|
|
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|