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