1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace awssat\Visits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
|
7
|
|
|
class Keys |
8
|
|
|
{ |
9
|
|
|
public $prefix; |
10
|
|
|
public $testing = ''; |
11
|
|
|
public $modelName = false; |
12
|
|
|
public $id; |
13
|
|
|
public $visits; |
14
|
|
|
public $primary = 'id'; |
15
|
|
|
public $instanceOfModel = false; |
16
|
|
|
public $tag; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Keys constructor. |
20
|
|
|
* @param $subject |
21
|
|
|
* @param $tag |
22
|
|
|
*/ |
23
|
|
|
public function __construct($subject, $tag) |
24
|
|
|
{ |
25
|
|
|
$this->modelName = $this->pluralModelName($subject); |
26
|
|
|
$this->prefix = config('visits.redis_keys_prefix'); |
27
|
|
|
$this->testing = app()->environment('testing') ? 'testing:' : ''; |
28
|
|
|
$this->primary = (new $subject)->getKeyName(); |
29
|
|
|
$this->tag = $tag; |
30
|
|
|
$this->visits = $this->visits(); |
31
|
|
|
|
32
|
|
|
if ($subject instanceof Model) { |
33
|
|
|
$this->instanceOfModel = true; |
34
|
|
|
$this->modelName = $this->modelName($subject); |
35
|
|
|
$this->id = $subject->{$subject->getKeyName()}; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get cache key |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function visits() |
45
|
|
|
{ |
46
|
|
|
return "{$this->prefix}:$this->testing" . $this->modelName . "_{$this->tag}"; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param $ip |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
public function ip($ip) |
54
|
|
|
{ |
55
|
|
|
return $this->visits . '_' . |
56
|
|
|
snake_case("recorded_ips:" . ($this->instanceOfModel ? "{$this->id}:" : '') . $ip); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param $limit |
62
|
|
|
* @param $isLow |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public function cache($limit = '*', $isLow = false) |
66
|
|
|
{ |
67
|
|
|
$key = $this->visits . "_lists"; |
68
|
|
|
|
69
|
|
|
if ($limit == '*') { |
70
|
|
|
return "{$key}:*"; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return "{$key}:" . ($isLow ? "low" : "top") . "{$limit}"; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $period |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function period($period) |
81
|
|
|
{ |
82
|
|
|
return "{$this->visits}_{$period}"; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param $relation |
87
|
|
|
* @param $id |
88
|
|
|
*/ |
89
|
|
|
public function append($relation, $id) |
90
|
|
|
{ |
91
|
|
|
$this->visits .= "_{$relation}_{$id}"; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $subject |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function modelName($subject) |
99
|
|
|
{ |
100
|
|
|
return strtolower(str_singular(class_basename(get_class($subject)))); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param $subject |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function pluralModelName($subject) |
108
|
|
|
{ |
109
|
|
|
return strtolower(str_plural(class_basename(is_string($subject) ? $subject : get_class($subject)))); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|