This repository has been archived by the owner on Jun 2, 2023. It is now read-only.
/
NotifyUserOfNewGistComment.php
84 lines (68 loc) · 2.32 KB
/
NotifyUserOfNewGistComment.php
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
namespace App\Jobs;
use App\NotifiedComment;
use Carbon\Carbon;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
class NotifyUserOfNewGistComment extends Job implements SelfHandling, ShouldQueue
{
private $user;
private $comment;
private $gist;
use InteractsWithQueue, SerializesModels;
public function __construct($user, $comment, $gist)
{
$this->user = $user;
$this->comment = $comment;
$this->gist = $gist;
}
public function handle()
{
if ($this->commentNoLongerNeedsNotification()) {
Log::info(sprintf(
'Skipped emailing notification for comment %s because it no longer needs notification.',
$this->comment['id']
));
return true;
}
$this->sendNotificationEmail($this->comment, $this->gist, $this->user);
$this->markCommentAsNotified($this->comment);
Log::info('Emailed notification for comment ' . $this->comment['id']);
}
private function sendNotificationEmail($comment, $gist, $user)
{
$parser = app('App\GitHubMarkdownParser', [$user]);
Mail::send(
'emails.new-comment',
[
'comment' => $comment,
'gist' => $gist,
'user' => $user,
'body' => $parser->parse($comment['body'])
],
function ($message) use ($user) {
$message
->to($user->email, $user->name)
->subject('You have a new Gist Comment!');
}
);
}
private function markCommentAsNotified($comment)
{
$eloquentComment = NotifiedComment::firstOrNew([
'github_id' => $comment['id']
]);
$eloquentComment->github_updated_at = Carbon::createFromFormat('Y-m-d\TH:i:s\Z', $comment['updated_at']);
$eloquentComment->save();
}
private function commentNoLongerNeedsNotification()
{
return NotifiedComment::where('github_id', $this->comment['id'])
->where('github_updated_at', $this->comment['updated_at'])
->count() > 0;
}
}