SubscribeToPostWhenCommentPostedInForum.php
1.66 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
<?php
namespace FootyRoom\Core\Subscription;
use FootyRoom\Core\ForumPost\ForumPostCommentPosted;
use FootyRoom\Queries\Subscription\OneSubscriptionQuery;
use FootyRoom\Queries\Subscription\SubscriptionQueryHandler;
use FootyRoom\Repositories\SubscriptionRepository;
class SubscribeToPostWhenCommentPostedInForum
{
/**
* @var \FootyRoom\Repositories\SubscriptionRepository
*/
protected $subRepo;
/**
* @var \FootyRoom\Queries\Subscription\SubscriptionQueryHandler
*/
protected $subQueryHandler;
/**
* Constructor.
*
* @param \FootyRoom\Repositories\SubscriptionRepository $subRepo
* @param \FootyRoom\Queries\Subscription\SubscriptionQueryHandler $subQueryHandler
*/
public function __construct(SubscriptionRepository $subRepo, SubscriptionQueryHandler $subQueryHandler)
{
$this->subRepo = $subRepo;
$this->subQueryHandler = $subQueryHandler;
}
/**
* Handler.
*
* @param \FootyRoom\Core\ForumPost\ForumPostCommentPosted $event
*/
public function handle(ForumPostCommentPosted $event)
{
$postId = explode(':', $event->comment->getDiscussionId())[1];
$subQuery = (new OneSubscriptionQuery())
->userId($event->comment->getAuthor()->getUserId())
->subjectType('post')
->subjectId($postId);
if ($this->subQueryHandler->findOne($subQuery)) {
return;
}
$subscription = new Subscription(
$postId,
'post',
$event->comment->getAuthor()->getUserId()
);
$subscription->follow();
$this->subRepo->create($subscription);
}
}