DiscussionComposer.php
5.89 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
namespace FootyRoom\Http\ViewComposers;
use Illuminate\View\View;
use FootyRoom\User\User;
use Illuminate\Http\Request;
use FootyRoom\Queries\Comment\CommentQuery;
use FootyRoom\Queries\Comment\TopCommentsQuery;
use FootyRoom\Queries\Comment\CommentCountQuery;
use FootyRoom\Queries\Comment\CommentQueryHandler;
class DiscussionComposer
{
/**
* @var \FootyRoom\Queries\Comment\CommentQueryHandler
*/
protected $commentQueryHandler;
/**
* @var \Illuminate\Http\Request
*/
protected $request;
/**
* Constructor.
*
* @param \FootyRoom\Queries\Comment\CommentQueryHandler $commentQueryHandler
* @param \Illuminate\Http\Request $request
*/
public function __construct(CommentQueryHandler $commentQueryHandler, Request $request)
{
$this->commentQueryHandler = $commentQueryHandler;
$this->request = $request;
}
/**
* Compose the view.
*
* @param \Illuminate\View\View $view
* @param int $discussionId
* @param \FootyRoom\User\User $user
* @param string $filter
* @param int $page
* @param int $sinceId
* @param bool $showTopComments Set to false to not include top comments.
*
* @return \Illuminate\View\View
*/
public function compose(
View $view,
$discussionId,
User $user = null,
$filter = null,
$page = 1,
$sinceId = 0,
$showTopComments = true
) {
$commentsPerPage = 20;
if ($commentId = $this->request->get('commentId')) {
$theCommentQuery = (new CommentQuery($discussionId))
->order('desc')
->threadOf($commentId)
->withUserInfo(true)
->viewedBy($user ? $user->getRole() : 0);
$theComment = $this->commentQueryHandler->find($theCommentQuery);
}
if (isset($theComment[0])) {
$view->with('theComment', $theComment[0]);
}
if (isset($theComment[0])) {
$limit = $commentsPerPage - 1;
} else {
$limit = $commentsPerPage;
}
if ($without = $this->request->get('without')) {
$offset = $commentsPerPage * ($page - 1) - 1;
} else {
$offset = $commentsPerPage * ($page - 1);
$without = isset($theComment[0]) ? $theComment[0]->id : 0;
}
$commentQuery = (new CommentQuery($discussionId))
->order('desc')
->limit($limit)
->offset($offset)
->sinceId($sinceId)
->filter($filter)
->without($without)
->withUserInfo(true)
->viewedBy($user ? $user->getRole() : 0);
$comments = $this->commentQueryHandler->find($commentQuery);
$view->with('comments', $comments);
if ($page === 1) {
if ($showTopComments) {
$this->loadTopComments($view, $discussionId, $filter, $user);
}
$this->loadCount($view, $discussionId, $filter, $user);
}
if (isset($view->count) && $view->count >= $commentsPerPage) {
$hasNext = $this->hasNext($commentQuery);
} elseif (!isset($view->count)) {
$hasNext = $this->hasNext($commentQuery);
} else {
$hasNext = false;
}
$this->buildJsonData($view, $discussionId, $page, $filter, $sinceId, $comments, $hasNext, $without);
return $view;
}
/**
* Builds json data for discussion.
*
* @param View $view
* @param int $discussionId
* @param int $page
* @param string $filter
* @param int $sinceId
* @param mixed $comments
* @param bool $hasNext
* @param int $without
*/
protected function buildJsonData($view, $discussionId, $page, $filter, $sinceId, $comments, $hasNext, $without)
{
$jsonData = [
'discussionId' => $discussionId,
'filter' => $filter,
'cursor' => [
'since' => $sinceId ?: (isset($comments[0]) ? $comments[0]->id : 0),
'next' => $hasNext ? $page + 1 : null,
],
];
if ($without) {
$jsonData['without'] = $without;
}
$view->with('jsonData', json_encode($jsonData));
}
/**
* Loads comments count.
*
* @param \Illuminate\View\View $view
* @param int $discussionId
* @param string $filter
* @param \FootyRoom\User\User $user
*/
protected function loadCount(View $view, $discussionId, $filter, User $user = null)
{
$countQuery = (new CommentCountQuery($discussionId))
->filter($filter)
->viewedBy($user ? $user->getRole() : 0);
$count = $this->commentQueryHandler->count($countQuery);
$view->with('count', $count);
}
/**
* Loads top comments.
*
* @param \Illuminate\View\View $view
* @param int $discussionId
* @param string $filter
* @param \FootyRoom\User\User $user
*/
protected function loadTopComments(View $view, $discussionId, $filter, User $user = null)
{
$topCommentsQuery = (new TopCommentsQuery($discussionId))
->filter($filter)
->withUserInfo(true)
->viewedBy($user ? $user->getRole() : 0);
$topComments = $this->commentQueryHandler->findTop($topCommentsQuery);
$view->with('topComments', $topComments);
}
/**
* Determines whether we have next page of comments.
*
* @param \FootyRoom\Queries\Comment\CommentQuery $commentQuery
*
* @return bool
*/
protected function hasNext(CommentQuery $commentQuery)
{
$hasNextQuery = (clone $commentQuery);
$hasNextQuery
->offset($commentQuery->getOffset() + $commentQuery->getLimit())
->withUserInfo(false)
->limit(1);
return (bool) $this->commentQueryHandler->find($hasNextQuery);
}
}