|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Session\Store; |
|
6
|
|
|
|
|
7
|
|
|
class ViewPostThrottleMiddleware |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Views will expire after 12 hours. |
|
11
|
|
|
* |
|
12
|
|
|
* @var int |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $throttleTime = 43200; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var Store |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $session; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* ViewThrottleMiddleware constructor. |
|
23
|
|
|
* @param Store $session |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(Store $session) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->session = $session; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Handle an incoming request. |
|
32
|
|
|
* |
|
33
|
|
|
* @param \Illuminate\Http\Request $request |
|
34
|
|
|
* @param \Closure $next |
|
35
|
|
|
* @param string|null $guard |
|
36
|
|
|
* @return mixed |
|
37
|
|
|
*/ |
|
38
|
|
|
public function handle($request, \Closure $next, $guard = null) |
|
|
|
|
|
|
39
|
|
|
{ |
|
40
|
|
|
$posts = $this->getViewedPosts(); |
|
41
|
|
|
|
|
42
|
|
|
if ( ! is_null($posts)) |
|
43
|
|
|
{ |
|
44
|
|
|
$posts = $this->cleanExpiredViews($posts); |
|
45
|
|
|
|
|
46
|
|
|
$this->storePosts($posts); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $next($request); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Get all the viewed posts from the session. If no |
|
54
|
|
|
* entry in the session exists, default to null. |
|
55
|
|
|
* |
|
56
|
|
|
* @return mixed |
|
57
|
|
|
*/ |
|
58
|
|
|
private function getViewedPosts() |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->session->get('viewed_posts', null); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Clean expired views. |
|
65
|
|
|
* |
|
66
|
|
|
* @param $posts |
|
67
|
|
|
* @return array |
|
68
|
|
|
*/ |
|
69
|
|
|
private function cleanExpiredViews($posts) |
|
70
|
|
|
{ |
|
71
|
|
|
$time = time(); |
|
72
|
|
|
|
|
73
|
|
|
// Filter through the post array. The argument passed to the |
|
74
|
|
|
// function will be the value from the array, which is the |
|
75
|
|
|
// timestamp in our case. |
|
76
|
|
|
return array_filter($posts, function ($timestamp) use ($time) |
|
77
|
|
|
{ |
|
78
|
|
|
// If the view timestamp + the throttle time is |
|
79
|
|
|
// still after the current timestamp the view |
|
80
|
|
|
// has not expired yet, so we want to keep it. |
|
81
|
|
|
return ($timestamp + $this->throttleTime) > $time; |
|
82
|
|
|
}); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Push the post id onto the viewed_posts session array. |
|
87
|
|
|
* |
|
88
|
|
|
* @param $posts |
|
89
|
|
|
*/ |
|
90
|
|
|
private function storePosts($posts) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->session->put('viewed_posts', $posts); |
|
93
|
|
|
} |
|
94
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.