PrerenderWhenCommentedOnMatch.php
1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
namespace FootyRoom\Core\Prerender;
use FootyRoom\PrerenderJob;
use FootyRoom\Http\Match\MatchSlug;
use Illuminate\Contracts\Bus\Dispatcher;
use FootyRoom\Core\Comment\CommentPosted;
use Illuminate\Contracts\Cache\Factory as CacheFactory;
class PrerenderWhenCommentedOnMatch
{
/**
* @var \Illuminate\Contracts\Cache\Repository
*/
private $cache;
/** @var \Illuminate\Contracts\Bus\Dispatcher */
private $dispatcher;
public function __construct(CacheFactory $cache, Dispatcher $dispatcher)
{
$this->cache = $cache->store('common');
$this->dispatcher = $dispatcher;
}
/**
* Dispatch job for prerendering
*
* @param \FootyRoom\Core\Comment\CommentPosted $event
*/
public function handle(CommentPosted $event)
{
if ($event->commentable->getCommentableType() == 'match') {
$url = route('matchReview', [
'id' => $event->commentable->getId(),
'slug' => MatchSlug::fromTeams($event->commentable->getHomeTeam()->getName(), $event->commentable->getAwayTeam()->getName()),
]);
// We don't want to prerender on every comment but debounce for 10 minutes
$debounceKey = 'prerender-debounce-match:'.$event->commentable->getId();
if (!$this->cache->has($debounceKey)) {
$this->cache->put($debounceKey, true, 10);
$this->dispatcher->dispatch(new PrerenderJob($url, true));
}
}
}
}