NotifyWhenCommentPosted.php
3.74 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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace FootyRoom\Core\Notification;
use FootyRoom\Core\Comment\CommentPosted;
use FootyRoom\Core\Match\Match;
use FootyRoom\Core\ForumPost\ForumPost;
use FootyRoom\Core\Post\Post;
use FootyRoom\Core\Wall\Wall;
use FootyRoom\Support\UrlGenerator;
class NotifyWhenCommentPosted
{
/**
* @var \FootyRoom\Core\Notification\CommentNotifier
*/
protected $notifier;
/**
* @var \FootyRoom\Support\UrlGenerator
*/
protected $urlGenerator;
/**
* Constructor.
*
* @param \FootyRoom\Core\Notification\CommentNotifier $notifier
* @param \FootyRoom\Support\UrlGenerator $urlGenerator
*/
public function __construct(CommentNotifier $notifier, UrlGenerator $urlGenerator)
{
$this->notifier = $notifier;
$this->urlGenerator = $urlGenerator;
}
/**
* Notify users of a reply to a comment.
*
* @param \FootyRoom\Core\Comment\CommentPosted $event
*/
public function handle(CommentPosted $event)
{
if (!$event->comment->getParentId() &&
!$event->commentable instanceof Wall &&
!$event->commentable instanceof ForumPost
) {
return;
}
$content = $this->createPayload($event);
if ($event->commentable instanceof ForumPost) {
$this->notifier->notifyForumReply(
$event->commentable->getId(),
$event->comment->getAuthor()->getUserId(),
$event->comment->getId(),
$content
);
} elseif ($event->commentable instanceof Wall && !$event->comment->getParentId()) {
$this->notifier->notifyProfileWallComment(
$event->commentable->getId(),
$event->comment->getAuthor()->getUserId(),
$event->comment->getId(),
$content
);
} elseif ($event->commentable instanceof Wall) {
$this->notifier->notifyProfileWallReply(
$event->comment->getParentId(),
$event->comment->getAuthor()->getUserId(),
$event->comment->getId(),
$event->commentable->getId(),
$content
);
} else {
$this->notifier->notifyReply(
$event->comment->getParentId(),
$event->comment->getAuthor()->getUserId(),
$event->comment->getId(),
$content
);
}
}
/**
* Create payload for comment notification.
*
* @param \FootyRoom\Core\Comment\CommentPosted $comment
*
* @return string
*/
protected function createPayload(CommentPosted $event)
{
$meta = [
'username' => $event->comment->getAuthor()->getName(),
'user_id' => $event->comment->getAuthor()->getUserId(),
'object_id' => "{$event->comment->getId()}",
'object_type' => 'comment',
];
if ($event->commentable instanceof Post) {
$meta['title'] = $this->getTitleForPost($event->commentable);
} elseif ($event->commentable instanceof Match) {
$meta['title'] = $this->getTitleForMatch($event->commentable);
}
$url = $this->urlGenerator->fromEntity($event->commentable);
if (isset($url)) {
$separator = strpos($url, '?') === false ? '?' : '&';
$meta['url'] = "{$url}{$separator}commentId={$event->comment->getId()}";
}
return json_encode($meta);
}
protected function getTitleForMatch(Match $match)
{
return $match->getHomeTeam()->getName().' vs '.$match->getAwayTeam()->getName();
}
protected function getTitleForPost(Post $post)
{
return $post->getTitle();
}
}