Skip to content

Commit fa5ca0b

Browse files
committed
Extract View Throttle filter to Filter class. Extract increment view to Event class
1 parent 64ea519 commit fa5ca0b

File tree

7 files changed

+208
-37
lines changed

7 files changed

+208
-37
lines changed

app/Controllers/TricksController.php

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Controllers;
44

55
use Illuminate\Support\Facades\Auth;
6+
use Illuminate\Support\Facades\Event;
67
use Illuminate\Support\Facades\Session;
78
use Illuminate\Support\Facades\Request;
89
use Illuminate\Support\Facades\Response;
@@ -48,7 +49,7 @@ public function getShow($slug = null)
4849
return $this->redirectRoute('home');
4950
}
5051

51-
$trick = $this->incrementViews($trick);
52+
Event::fire('trick.view', $trick);
5253

5354
$this->view('tricks.single', compact('trick'));
5455
}
@@ -72,7 +73,7 @@ public function postLike($slug)
7273
}
7374

7475
$user = Auth::user();
75-
76+
7677
$voted = $trick->votes()->whereUserId($user->id)->first();
7778

7879
if(!$voted) {
@@ -92,24 +93,4 @@ public function postLike($slug)
9293

9394
return Response::make($trick->vote_cache, 200);
9495
}
95-
96-
/**
97-
* Increment the view count on the given trick.
98-
*
99-
* @param \Trick $trick
100-
* @return \Trick
101-
*/
102-
private function incrementViews($trick)
103-
{
104-
$viewed = Session::get('viewed_tricks', []);
105-
106-
if (! array_key_exists($trick->id, $viewed)) {
107-
$trick = $this->tricks->incrementViews($trick);
108-
}
109-
110-
$viewed[$trick->id] = time();
111-
Session::put('viewed_tricks', $viewed);
112-
113-
return $trick;
114-
}
11596
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace Tricks\Events;
4+
5+
use Illuminate\Session\Store;
6+
use Tricks\Repositories\TrickRepositoryInterface;
7+
8+
class ViewTrickHandler
9+
{
10+
/**
11+
* Trick repository instance.
12+
*
13+
* @var \Tricks\Repositories\TrickRepositoryInterface
14+
*/
15+
protected $tricks;
16+
17+
/**
18+
* Session store instance.
19+
*
20+
* @var \Illuminate\Session\Store
21+
*/
22+
protected $session;
23+
24+
/**
25+
* Create a new view trick handler instance.
26+
*
27+
* @param \Tricks\Repositories\TrickRepositoryInterface $tricks
28+
* @param \Illuminate\Session\Store $session
29+
* @return void
30+
*/
31+
public function __construct(TrickRepositoryInterface $tricks, Store $session)
32+
{
33+
$this->tricks = $tricks;
34+
$this->session = $session;
35+
}
36+
37+
/**
38+
* Handle the view trick event.
39+
*
40+
* @param \Tricks\Trick $trick
41+
* @return void
42+
*/
43+
public function handle($trick)
44+
{
45+
if (! $this->hasViewedTrick($trick)) {
46+
$trick = $this->tricks->incrementViews($trick);
47+
48+
$this->storeViewedTrick($trick);
49+
}
50+
}
51+
52+
/**
53+
* Determine whether the user has viewed the trick.
54+
*
55+
* @param \Tricks\Trick $trick
56+
* @return bool
57+
*/
58+
protected function hasViewedTrick($trick)
59+
{
60+
return array_key_exists($trick->id, $this->getViewedTricks());
61+
}
62+
63+
/**
64+
* Get the users viewed trick from the session.
65+
*
66+
* @return array
67+
*/
68+
protected function getViewedTricks()
69+
{
70+
return $this->session->get('viewed_tricks', []);
71+
}
72+
73+
/**
74+
* Append the newly viewed trick to the session.
75+
*
76+
* @param \Tricks\Trick $trick
77+
* @return void
78+
*/
79+
protected function storeViewedTrick($trick)
80+
{
81+
$key = 'viewed_tricks.' . $trick->id;
82+
83+
$this->session->put($key, time());
84+
}
85+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
namespace Tricks\Filters;
4+
5+
use Illuminate\Session\Store;
6+
use Illuminate\Config\Repository;
7+
8+
class ViewThrottleFilter
9+
{
10+
/**
11+
* Config repository instance.
12+
*
13+
* @var \Illuminate\Config\Repository
14+
*/
15+
protected $config;
16+
17+
/**
18+
* Session store instance.
19+
*
20+
* @var \Illuminate\Session\Store
21+
*/
22+
protected $session;
23+
24+
/**
25+
* Create a new view throttle filter instance.
26+
*
27+
* @param \Illuminate\Config\Repository $config
28+
* @param \Illuminate\Session\Store $session
29+
* @return void
30+
*/
31+
public function __construct(Repository $config, Store $session)
32+
{
33+
$this->config = $config;
34+
$this->session = $session;
35+
}
36+
37+
/**
38+
* Execute the route filter.
39+
*
40+
* @return void
41+
*/
42+
public function filter()
43+
{
44+
$tricks = $this->getViewedTricks();
45+
46+
if ($tricks !== null) {
47+
$tricks = $this->purgeExpiredTricks($tricks);
48+
49+
$this->storeViewedTricks($tricks);
50+
}
51+
}
52+
53+
/**
54+
* Get the recently viewed tricks from the session.
55+
*
56+
* @return array|null
57+
*/
58+
protected function getViewedTricks()
59+
{
60+
return $this->session->get('viewed_tricks', null);
61+
}
62+
63+
/**
64+
* Get the view throttle time from the config.
65+
*
66+
* @return int
67+
*/
68+
protected function getThrottleTime()
69+
{
70+
return $this->config->get('config.view_throttle_time');
71+
}
72+
73+
/**
74+
* Filter the tricks array, removing expired tricks.
75+
*
76+
* @param array $tricks
77+
* @return array
78+
*/
79+
protected function purgeExpiredTricks(array $tricks)
80+
{
81+
$time = time();
82+
$throttleTime = $this->getThrottleTime();
83+
84+
return array_filter($tricks, function ($timestamp) use ($time, $throttleTime) {
85+
return ($timestamp + $throttleTime) > $time;
86+
});
87+
}
88+
89+
/**
90+
* Store the recently viewed tricks in the session.
91+
*
92+
* @param array $tricks
93+
* @return void
94+
*/
95+
protected function storeViewedTricks(array $tricks)
96+
{
97+
$this->session->put('viewed_tricks', $tricks);
98+
}
99+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Tricks\Providers;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class EventServiceProvider extends ServiceProvider
8+
{
9+
public function boot()
10+
{
11+
$this->app['events']->listen('trick.view', 'Tricks\Events\ViewTrickHandler');
12+
}
13+
14+
public function register()
15+
{
16+
17+
}
18+
}

app/config/app.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
'Tricks\Providers\UploadServiceProvider',
118118
'Tricks\Providers\NavigationServiceProvider',
119119
'Tricks\Providers\SitemapServiceProvider',
120+
'Tricks\Providers\EventServiceProvider',
120121
),
121122

122123
/*

app/filters.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,4 @@
8383
}
8484
});
8585

86-
Route::filter('trick.view_throttle', function() {
87-
if (! is_null($trickIds = Session::get('viewed_tricks', null))) {
88-
$expireTime = time() + Config::get('config.view_expire_time');
89-
$temp = $trickIds;
90-
91-
foreach ($trickIds as $id => $timestamp) {
92-
if ($timestamp < $expireTime) {
93-
unset($temp[$id]);
94-
}
95-
}
96-
97-
Session::put('viewed_tricks', $temp);
98-
}
99-
});
86+
Route::filter('trick.view_throttle', 'Tricks\Filters\ViewThrottleFilter');

app/routes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
# Route filters
33
Route::when('admin/*', 'admin');
4-
Route::when('*', 'tricks.view_throttle');
4+
Route::when('*', 'trick.view_throttle');
55

66
# Route patterns
77
Route::pattern('tag_slug', '[a-z0-9\-]+');

0 commit comments

Comments
 (0)