@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if (! function_exists('visits')) |
|
3 | +if (!function_exists('visits')) |
|
4 | 4 | { |
5 | 5 | function visits($subject, $tag = 'visits') |
6 | 6 | { |
@@ -16,16 +16,16 @@ discard block |
||
16 | 16 | public function boot() |
17 | 17 | { |
18 | 18 | $this->publishes([ |
19 | - __DIR__.'/config/visits.php' => config_path('visits.php'), |
|
19 | + __DIR__ . '/config/visits.php' => config_path('visits.php'), |
|
20 | 20 | ], 'config'); |
21 | 21 | |
22 | - if (! class_exists('CreateVisitsTable')) { |
|
22 | + if (!class_exists('CreateVisitsTable')) { |
|
23 | 23 | $this->publishes([ |
24 | - __DIR__.'/../database/migrations/create_visits_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_visits_table.php'), |
|
24 | + __DIR__ . '/../database/migrations/create_visits_table.php.stub' => database_path('migrations/' . date('Y_m_d_His', time()) . '_create_visits_table.php'), |
|
25 | 25 | ], 'migrations'); |
26 | 26 | } |
27 | 27 | |
28 | - Carbon::macro('endOfxHours', function ($xhours) { |
|
28 | + Carbon::macro('endOfxHours', function($xhours) { |
|
29 | 29 | if ($xhours > 12) { |
30 | 30 | throw new \Exception('12 is the maximum period in xHours feature'); |
31 | 31 | } |
@@ -47,7 +47,7 @@ discard block |
||
47 | 47 | public function register() |
48 | 48 | { |
49 | 49 | $this->mergeConfigFrom( |
50 | - __DIR__.'/config/visits.php', |
|
50 | + __DIR__ . '/config/visits.php', |
|
51 | 51 | 'visits' |
52 | 52 | ); |
53 | 53 |
@@ -18,17 +18,17 @@ |
||
18 | 18 | |
19 | 19 | if ($this->noExpiration($periodKey)) { |
20 | 20 | $expireInSeconds = $this->newExpiration($period); |
21 | - $this->connection->increment($periodKey.'_total', 0); |
|
21 | + $this->connection->increment($periodKey . '_total', 0); |
|
22 | 22 | $this->connection->increment($periodKey, 0, 0); |
23 | 23 | $this->connection->setExpiration($periodKey, $expireInSeconds); |
24 | - $this->connection->setExpiration($periodKey.'_total', $expireInSeconds); |
|
24 | + $this->connection->setExpiration($periodKey . '_total', $expireInSeconds); |
|
25 | 25 | } |
26 | 26 | } |
27 | 27 | } |
28 | 28 | |
29 | 29 | protected function noExpiration($periodKey) |
30 | 30 | { |
31 | - return $this->connection->timeLeft($periodKey) == -1 || ! $this->connection->exists($periodKey); |
|
31 | + return $this->connection->timeLeft($periodKey) == -1 || !$this->connection->exists($periodKey); |
|
32 | 32 | } |
33 | 33 | |
34 | 34 | protected function newExpiration($period) |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | if ($this->keys->id) { |
44 | 44 | $this->connection->delete($this->keys->visits, $this->keys->id); |
45 | 45 | foreach (['countries', 'referers', 'OSes', 'languages'] as $item) { |
46 | - $this->connection->delete($this->keys->visits."_{$item}:{$this->keys->id}"); |
|
46 | + $this->connection->delete($this->keys->visits . "_{$item}:{$this->keys->id}"); |
|
47 | 47 | } |
48 | 48 | |
49 | 49 | foreach ($this->periods as $period => $_) { |
@@ -53,13 +53,13 @@ discard block |
||
53 | 53 | $this->ips(); |
54 | 54 | } else { |
55 | 55 | $this->connection->delete($this->keys->visits); |
56 | - $this->connection->delete($this->keys->visits.'_total'); |
|
56 | + $this->connection->delete($this->keys->visits . '_total'); |
|
57 | 57 | } |
58 | 58 | } |
59 | 59 | |
60 | 60 | public function allrefs() |
61 | 61 | { |
62 | - $cc = $this->connection->search($this->keys->visits.'_referers:*'); |
|
62 | + $cc = $this->connection->search($this->keys->visits . '_referers:*'); |
|
63 | 63 | |
64 | 64 | if (count($cc)) { |
65 | 65 | $this->connection->delete($cc); |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | |
69 | 69 | public function allOperatingSystems() |
70 | 70 | { |
71 | - $cc = $this->connection->search($this->keys->visits.'_OSes:*'); |
|
71 | + $cc = $this->connection->search($this->keys->visits . '_OSes:*'); |
|
72 | 72 | |
73 | 73 | if (count($cc)) { |
74 | 74 | $this->connection->delete($cc); |
@@ -77,7 +77,7 @@ discard block |
||
77 | 77 | |
78 | 78 | public function allLanguages() |
79 | 79 | { |
80 | - $cc = $this->connection->search($this->keys->visits.'_languages:*'); |
|
80 | + $cc = $this->connection->search($this->keys->visits . '_languages:*'); |
|
81 | 81 | |
82 | 82 | if (count($cc)) { |
83 | 83 | $this->connection->delete($cc); |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | |
87 | 87 | public function allcountries() |
88 | 88 | { |
89 | - $cc = $this->connection->search($this->keys->visits.'_countries:*'); |
|
89 | + $cc = $this->connection->search($this->keys->visits . '_countries:*'); |
|
90 | 90 | |
91 | 91 | if (count($cc)) { |
92 | 92 | $this->connection->delete($cc); |
@@ -101,7 +101,7 @@ discard block |
||
101 | 101 | foreach ($this->periods as $period => $_) { |
102 | 102 | $periodKey = $this->keys->period($period); |
103 | 103 | $this->connection->delete($periodKey); |
104 | - $this->connection->delete($periodKey.'_total'); |
|
104 | + $this->connection->delete($periodKey . '_total'); |
|
105 | 105 | } |
106 | 106 | } |
107 | 107 |
@@ -20,7 +20,7 @@ |
||
20 | 20 | { |
21 | 21 | $currentEngine = config('visits.engine') ?? ''; |
22 | 22 | |
23 | - if($currentEngine == 'eloquent') { |
|
23 | + if ($currentEngine == 'eloquent') { |
|
24 | 24 | $this->cleanEloquent(); |
25 | 25 | } |
26 | 26 | } |
@@ -31,10 +31,10 @@ discard block |
||
31 | 31 | |
32 | 32 | public function increment(string $key, int $value, $member = null): bool |
33 | 33 | { |
34 | - if (! empty($member) || is_numeric($member)) { |
|
35 | - $this->connection->zincrby($this->prefix.$key, $value, $member); |
|
34 | + if (!empty($member) || is_numeric($member)) { |
|
35 | + $this->connection->zincrby($this->prefix . $key, $value, $member); |
|
36 | 36 | } else { |
37 | - $this->connection->incrby($this->prefix.$key, $value); |
|
37 | + $this->connection->incrby($this->prefix . $key, $value); |
|
38 | 38 | } |
39 | 39 | |
40 | 40 | // both methods returns integer and raise an exception in case of an error. |
@@ -48,35 +48,35 @@ discard block |
||
48 | 48 | |
49 | 49 | public function delete($key, $member = null): bool |
50 | 50 | { |
51 | - if(is_array($key)) { |
|
51 | + if (is_array($key)) { |
|
52 | 52 | array_walk($key, function($item) { |
53 | 53 | $this->delete($item); |
54 | 54 | }); |
55 | 55 | return true; |
56 | 56 | } |
57 | 57 | |
58 | - if(! empty($member) || is_numeric($member)) { |
|
59 | - return $this->connection->zrem($this->prefix.$key, $member) > 0; |
|
58 | + if (!empty($member) || is_numeric($member)) { |
|
59 | + return $this->connection->zrem($this->prefix . $key, $member) > 0; |
|
60 | 60 | } else { |
61 | - return $this->connection->del($this->prefix.$key) > 0; |
|
61 | + return $this->connection->del($this->prefix . $key) > 0; |
|
62 | 62 | } |
63 | 63 | } |
64 | 64 | |
65 | 65 | public function get(string $key, $member = null) |
66 | 66 | { |
67 | - if(! empty($member) || is_numeric($member)) { |
|
68 | - return $this->connection->zscore($this->prefix.$key, $member); |
|
67 | + if (!empty($member) || is_numeric($member)) { |
|
68 | + return $this->connection->zscore($this->prefix . $key, $member); |
|
69 | 69 | } else { |
70 | - return $this->connection->get($this->prefix.$key); |
|
70 | + return $this->connection->get($this->prefix . $key); |
|
71 | 71 | } |
72 | 72 | } |
73 | 73 | |
74 | 74 | public function set(string $key, $value, $member = null): bool |
75 | 75 | { |
76 | - if(! empty($member) || is_numeric($member)) { |
|
77 | - return $this->connection->zAdd($this->prefix.$key, $value, $member) > 0; |
|
76 | + if (!empty($member) || is_numeric($member)) { |
|
77 | + return $this->connection->zAdd($this->prefix . $key, $value, $member) > 0; |
|
78 | 78 | } else { |
79 | - return (bool) $this->connection->set($this->prefix.$key, $value); |
|
79 | + return (bool) $this->connection->set($this->prefix . $key, $value); |
|
80 | 80 | } |
81 | 81 | } |
82 | 82 | |
@@ -90,39 +90,39 @@ discard block |
||
90 | 90 | |
91 | 91 | return $item; |
92 | 92 | }, |
93 | - $this->connection->keys($this->prefix.$word) ?? [] |
|
93 | + $this->connection->keys($this->prefix . $word) ?? [] |
|
94 | 94 | ); |
95 | 95 | } |
96 | 96 | |
97 | 97 | public function flatList(string $key, int $limit = -1): array |
98 | 98 | { |
99 | - return $this->connection->lrange($this->prefix.$key, 0, $limit); |
|
99 | + return $this->connection->lrange($this->prefix . $key, 0, $limit); |
|
100 | 100 | } |
101 | 101 | |
102 | 102 | public function addToFlatList(string $key, $value): bool |
103 | 103 | { |
104 | - return $this->connection->rpush($this->prefix.$key, $value) !== false; |
|
104 | + return $this->connection->rpush($this->prefix . $key, $value) !== false; |
|
105 | 105 | } |
106 | 106 | |
107 | 107 | public function valueList(string $key, int $limit = -1, bool $orderByAsc = false, bool $withValues = false): array |
108 | 108 | { |
109 | 109 | $range = $orderByAsc ? 'zrange' : 'zrevrange'; |
110 | 110 | |
111 | - return $this->connection->$range($this->prefix.$key, 0, $limit, $this->isPHPRedis ? $withValues : ['withscores' => $withValues]) ?: []; |
|
111 | + return $this->connection->$range($this->prefix . $key, 0, $limit, $this->isPHPRedis ? $withValues : ['withscores' => $withValues]) ?: []; |
|
112 | 112 | } |
113 | 113 | |
114 | 114 | public function exists(string $key): bool |
115 | 115 | { |
116 | - return (bool) $this->connection->exists($this->prefix.$key); |
|
116 | + return (bool) $this->connection->exists($this->prefix . $key); |
|
117 | 117 | } |
118 | 118 | |
119 | 119 | public function timeLeft(string $key): int |
120 | 120 | { |
121 | - return $this->connection->ttl($this->prefix.$key); |
|
121 | + return $this->connection->ttl($this->prefix . $key); |
|
122 | 122 | } |
123 | 123 | |
124 | 124 | public function setExpiration(string $key, int $time): bool |
125 | 125 | { |
126 | - return $this->connection->expire($this->prefix.$key, $time); |
|
126 | + return $this->connection->expire($this->prefix . $key, $time); |
|
127 | 127 | } |
128 | 128 | } |
129 | 129 | \ No newline at end of file |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | ->connect($this->config['connection']) |
82 | 82 | ->setPrefix($this->config['keys_prefix'] ?? $this->config['redis_keys_prefix'] ?? 'visits'); |
83 | 83 | |
84 | - if(! $this->connection) { |
|
84 | + if (!$this->connection) { |
|
85 | 85 | return; |
86 | 86 | } |
87 | 87 | |
@@ -93,7 +93,7 @@ discard block |
||
93 | 93 | $this->subject = $subject; |
94 | 94 | $this->keys = new Keys($subject, preg_replace('/[^a-z0-9_]/i', '', $tag)); |
95 | 95 | |
96 | - if (! empty($this->keys->id)) { |
|
96 | + if (!empty($this->keys->id)) { |
|
97 | 97 | $this->periodsSync(); |
98 | 98 | } |
99 | 99 | } |
@@ -105,7 +105,7 @@ discard block |
||
105 | 105 | 'eloquent' => \Awssat\Visits\DataEngines\EloquentEngine::class |
106 | 106 | ]; |
107 | 107 | |
108 | - if(! array_key_exists($name, $connections)) { |
|
108 | + if (!array_key_exists($name, $connections)) { |
|
109 | 109 | throw new \Exception("(Laravel-Visits) The selected engine `{$name}` is not supported! Please correct this issue from config/visits.php."); |
110 | 110 | } |
111 | 111 | |
@@ -118,7 +118,7 @@ discard block |
||
118 | 118 | */ |
119 | 119 | public function by($subject) |
120 | 120 | { |
121 | - if($subject instanceof Model) { |
|
121 | + if ($subject instanceof Model) { |
|
122 | 122 | $this->keys->append($this->keys->modelName($subject), $subject->{$subject->getKeyName()}); |
123 | 123 | } else if (is_array($subject)) { |
124 | 124 | $this->keys->append(array_keys($subject)[0], Arr::first($subject)); |
@@ -145,7 +145,7 @@ discard block |
||
145 | 145 | */ |
146 | 146 | public function recordedIp() |
147 | 147 | { |
148 | - if(! $this->connection->exists($this->keys->ip(request()->ip()))) { |
|
148 | + if (!$this->connection->exists($this->keys->ip(request()->ip()))) { |
|
149 | 149 | $this->connection->set($this->keys->ip(request()->ip()), true); |
150 | 150 | $this->connection->setExpiration($this->keys->ip(request()->ip()), $this->ipSeconds); |
151 | 151 | |
@@ -162,13 +162,13 @@ discard block |
||
162 | 162 | public function count() |
163 | 163 | { |
164 | 164 | if ($this->country) { |
165 | - return $this->connection->get($this->keys->visits."_countries:{$this->keys->id}", $this->country); |
|
165 | + return $this->connection->get($this->keys->visits . "_countries:{$this->keys->id}", $this->country); |
|
166 | 166 | } else if ($this->referer) { |
167 | - return $this->connection->get($this->keys->visits."_referers:{$this->keys->id}", $this->referer); |
|
167 | + return $this->connection->get($this->keys->visits . "_referers:{$this->keys->id}", $this->referer); |
|
168 | 168 | } else if ($this->operatingSystem) { |
169 | - return $this->connection->get($this->keys->visits."_OSes:{$this->keys->id}", $this->operatingSystem); |
|
169 | + return $this->connection->get($this->keys->visits . "_OSes:{$this->keys->id}", $this->operatingSystem); |
|
170 | 170 | } else if ($this->language) { |
171 | - return $this->connection->get($this->keys->visits."_languages:{$this->keys->id}", $this->language); |
|
171 | + return $this->connection->get($this->keys->visits . "_languages:{$this->keys->id}", $this->language); |
|
172 | 172 | } |
173 | 173 | |
174 | 174 | return intval( |
@@ -212,14 +212,14 @@ discard block |
||
212 | 212 | $this->connection->increment($this->keys->visits, $inc, $this->keys->id); |
213 | 213 | $this->connection->increment($this->keys->visitsTotal(), $inc); |
214 | 214 | |
215 | - if(is_array($this->globalIgnore) && sizeof($this->globalIgnore) > 0) { |
|
215 | + if (is_array($this->globalIgnore) && sizeof($this->globalIgnore) > 0) { |
|
216 | 216 | $ignore = array_merge($ignore, $this->globalIgnore); |
217 | 217 | } |
218 | 218 | |
219 | 219 | //NOTE: $$method is parameter also .. ($periods,$country,$refer) |
220 | 220 | foreach (['country', 'refer', 'periods', 'operatingSystem', 'language'] as $method) { |
221 | - if(! in_array($method, $ignore)) { |
|
222 | - $this->{'record'.Str::studly($method)}($inc); |
|
221 | + if (!in_array($method, $ignore)) { |
|
222 | + $this->{'record' . Str::studly($method)}($inc); |
|
223 | 223 | } |
224 | 224 | } |
225 | 225 | |
@@ -280,6 +280,6 @@ discard block |
||
280 | 280 | { |
281 | 281 | $prefix = $this->config['keys_prefix'] ?? $this->config['redis_keys_prefix'] ?? 'visits'; |
282 | 282 | |
283 | - return $this->subject->hasOne(Visit::class, 'secondary_key')->where('primary_key', $prefix.':'.$this->keys->visits); |
|
283 | + return $this->subject->hasOne(Visit::class, 'secondary_key')->where('primary_key', $prefix . ':' . $this->keys->visits); |
|
284 | 284 | } |
285 | 285 | } |
@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | */ |
16 | 16 | public function top($limit = 5, $orderByAsc = false, $constraints = []) |
17 | 17 | { |
18 | - if(is_array($orderByAsc)) { |
|
18 | + if (is_array($orderByAsc)) { |
|
19 | 19 | $constraints = $orderByAsc; |
20 | 20 | $orderByAsc = false; |
21 | 21 | } |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | $cachedList = $this->cachedList($limit, $cacheKey); |
26 | 26 | $visitsIds = $this->getVisitsIds($limit, $this->keys->visits, $orderByAsc); |
27 | 27 | |
28 | - if($visitsIds === $cachedList->pluck($this->keys->primary)->toArray() && ! $this->fresh) { |
|
28 | + if ($visitsIds === $cachedList->pluck($this->keys->primary)->toArray() && !$this->fresh) { |
|
29 | 29 | return $cachedList; |
30 | 30 | } |
31 | 31 | |
@@ -113,9 +113,9 @@ discard block |
||
113 | 113 | return $query->where($constraints); |
114 | 114 | }) |
115 | 115 | ->get() |
116 | - ->sortBy(function ($subject) use ($visitsIds) { |
|
116 | + ->sortBy(function($subject) use ($visitsIds) { |
|
117 | 117 | return array_search($subject->{$this->keys->primary}, $visitsIds); |
118 | - })->each(function ($subject) use ($cacheKey) { |
|
118 | + })->each(function($subject) use ($cacheKey) { |
|
119 | 119 | $this->connection->addToFlatList($cacheKey, serialize($subject)); |
120 | 120 | }); |
121 | 121 | } |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | */ |
34 | 34 | public function visits() |
35 | 35 | { |
36 | - return (app()->environment('testing') ? 'testing:' : '').$this->modelName."_{$this->tag}"; |
|
36 | + return (app()->environment('testing') ? 'testing:' : '') . $this->modelName . "_{$this->tag}"; |
|
37 | 37 | } |
38 | 38 | |
39 | 39 | /** |
@@ -49,8 +49,8 @@ discard block |
||
49 | 49 | */ |
50 | 50 | public function ip($ip) |
51 | 51 | { |
52 | - return $this->visits.'_'.Str::snake( |
|
53 | - 'recorded_ips:'.($this->instanceOfModel ? "{$this->id}:" : '') . $ip |
|
52 | + return $this->visits . '_' . Str::snake( |
|
53 | + 'recorded_ips:' . ($this->instanceOfModel ? "{$this->id}:" : '') . $ip |
|
54 | 54 | ); |
55 | 55 | } |
56 | 56 | |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | */ |
60 | 60 | public function cache($limit = '*', $isLow = false, $constraints = []) |
61 | 61 | { |
62 | - $key = $this->visits.'_lists'; |
|
62 | + $key = $this->visits . '_lists'; |
|
63 | 63 | |
64 | 64 | if ($limit == '*') { |
65 | 65 | return "{$key}:*"; |
@@ -67,9 +67,9 @@ discard block |
||
67 | 67 | |
68 | 68 | //it might not be that unique but it does the job since not many lists |
69 | 69 | //will be generated to one key. |
70 | - $constraintsPart = count($constraints) ? ':'.substr(sha1(serialize($constraints)), 0, 7) : ''; |
|
70 | + $constraintsPart = count($constraints) ? ':' . substr(sha1(serialize($constraints)), 0, 7) : ''; |
|
71 | 71 | |
72 | - return "{$key}:".($isLow ? 'low' : 'top').$constraintsPart.$limit; |
|
72 | + return "{$key}:" . ($isLow ? 'low' : 'top') . $constraintsPart . $limit; |
|
73 | 73 | } |
74 | 74 | |
75 | 75 | /** |