Conditions | 5 |
Paths | 7 |
Total Lines | 40 |
Code Lines | 23 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
12 | public function handle($request, Closure $next) |
||
13 | { |
||
14 | $postId = $request->route('id'); |
||
15 | $ipAddress = $request->ip(); |
||
16 | |||
17 | $post = Post::find($postId); |
||
18 | if (!$post) { |
||
|
|||
19 | return $next($request); |
||
20 | } |
||
21 | |||
22 | $postView = PostView::where('post_id', $postId) |
||
23 | ->where('ip_address', $ipAddress) |
||
24 | ->first(); |
||
25 | |||
26 | $shouldIncrementView = false; |
||
27 | |||
28 | if (!$postView) { |
||
29 | // Access this post for the first time from this IP |
||
30 | PostView::create([ |
||
31 | 'post_id' => $postId, |
||
32 | 'ip_address' => $ipAddress, |
||
33 | 'time_check' => Carbon::now()->addHours(), |
||
34 | ]); |
||
35 | $shouldIncrementView = true; |
||
36 | } else { |
||
37 | // Check if field time_check is passed |
||
38 | if (Carbon::now()->isAfter($postView->time_check)) { |
||
39 | // Update the field time_check |
||
40 | $postView->update([ |
||
41 | 'time_check' => Carbon::now()->addHours(), |
||
42 | ]); |
||
43 | $shouldIncrementView = true; |
||
44 | } |
||
45 | } |
||
46 | |||
47 | if ($shouldIncrementView) { |
||
48 | $post->increment('views'); |
||
49 | } |
||
50 | |||
51 | return $next($request); |
||
52 | } |
||
54 |