Skip to content

Commit e3a9c88

Browse files
committed
* split parts about union post queries and get its result into a new method getUnionQueryResult() @ App\PostsQuery\QueryResult::setResult()
@ be
1 parent fd7df92 commit e3a9c88

File tree

2 files changed

+50
-27
lines changed

2 files changed

+50
-27
lines changed

be/src/PostsQuery/CursorCodec.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
* threads: Collection<Thread>,
1515
* replies: Collection<Reply>,
1616
* subReplies: Collection<SubReply>,
17-
* } */
17+
* }
18+
*/
1819
class CursorCodec
1920
{
2021
/** @param PostsKeyByTypePluralName $postsKeyByTypePluralName */

be/src/PostsQuery/QueryResult.php

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,7 @@ public function getQueryResult(AbstractQuery $query, int $maxResults): array
6969
];
7070
}
7171

72-
/** @psalm-type UnionPostKey = array{
73-
* postType: 'reply'|'subReply'|'thread',
74-
* postId: int,
75-
* fid: int,
76-
* tid: int,
77-
* pid: int,
78-
* orderByField: mixed
79-
* }
80-
* @param Collection<Helper::POST_TYPE, QueryBuilder> $queries
81-
*/
72+
/** @param Collection<Helper::POST_TYPE, QueryBuilder> $queries */
8273
public function setResult(
8374
Collection $queries,
8475
?string $cursorParamValue,
@@ -121,7 +112,46 @@ public function setResult(
121112
$cursors->mapWithKeys(fn($fieldValue, string $fieldName) =>
122113
$qb->setParameter("cursor_$fieldName", $fieldValue)); // prevent overwriting existing param
123114
});
115+
[
116+
'unionOfQueriesSQL' => $unionOfQueriesSQL,
117+
'postsKeyByTypePluralName' => $postsKeyByTypePluralName,
118+
'hasMorePages' => $hasMorePages,
119+
'queryPlan' => $queryPlan
120+
] = $this->getUnionQueryResult($queries, $orderByDesc, $maxResults);
121+
122+
$this->threads = $postsKeyByTypePluralName->get('threads', collect());
123+
$this->replies = $postsKeyByTypePluralName->get('replies', collect());
124+
$this->subReplies = $postsKeyByTypePluralName->get('subReplies', collect());
125+
$this->fid = $this->threads->first()->fid
126+
?? $this->replies->first()->fid
127+
?? $this->subReplies->first()->fid;
128+
$this->currentCursor = $cursorParamValue ?? '';
129+
$this->nextCursor = $hasMorePages
130+
? $this->cursorCodec->encodeNextCursor($postsKeyByTypePluralName->except(
131+
$queryByPostIDParamsName->map(static fn(string $postID) => Helper::POST_ID_TO_TYPE_PLURAL[$postID])
132+
))
133+
: null;
134+
$this->query = ['query' => $unionOfQueriesSQL, 'plan' => $queryPlan];
124135

136+
$this->stopwatch->stop('setResult');
137+
}
138+
139+
/**
140+
* @psalm-type UnionPostKey = array{
141+
* postType: 'reply'|'subReply'|'thread',
142+
* postId: int,
143+
* fid: int,
144+
* tid: int,
145+
* pid: int,
146+
* orderByField: mixed
147+
* }
148+
* @param Collection<Helpecr::POST_TYPE, QueryBuilder> $queries
149+
* @param bool $orderByDesc
150+
* @param int $maxResults
151+
* @return array{unionOfQueriesSQL: string, postsKeyByTypePluralName: PostsKeyByTypePluralName, hasMorePages: bool, queryPlan: array}
152+
*/
153+
private function getUnionQueryResult(Collection $queries, bool $orderByDesc, int $maxResults): array
154+
{
125155
/** @var DBALQueryBuilder $unionOfQueries */
126156
// https://stackoverflow.com/questions/36959801/doctrine-orm-querybuilder-or-dbal-querybuilder
127157
$unionOfQueries = $queries->reduce(function (?DBALQueryBuilder $dbalQueryBuilder, QueryBuilder $ormQueryBuilder) {
@@ -173,23 +203,15 @@ public function setResult(
173203
'reply' => new ReplyKey($fid, $tid, $postId, $orderByFieldValue),
174204
'subReply' => new SubReplyKey($fid, $tid, $pid, $postId, $orderByFieldValue)
175205
};
176-
})]);
206+
})
207+
]);
177208
Helper::abortAPIIf(40401, $postsKeyByTypePluralName->every(static fn(Collection $i) => $i->isEmpty()));
178209

179-
$this->threads = $postsKeyByTypePluralName->get('threads', collect());
180-
$this->replies = $postsKeyByTypePluralName->get('replies', collect());
181-
$this->subReplies = $postsKeyByTypePluralName->get('subReplies', collect());
182-
$this->fid = $this->threads->first()->fid
183-
?? $this->replies->first()->fid
184-
?? $this->subReplies->first()->fid;
185-
$this->currentCursor = $cursorParamValue ?? '';
186-
$this->nextCursor = $hasMorePages
187-
? $this->cursorCodec->encodeNextCursor($postsKeyByTypePluralName->except(
188-
$queryByPostIDParamsName->map(static fn(string $postID) => Helper::POST_ID_TO_TYPE_PLURAL[$postID])
189-
))
190-
: null;
191-
$this->query = ['query' => $unionOfQueriesSQL, 'plan' => $queryPlan];
192-
193-
$this->stopwatch->stop('setResult');
210+
return [
211+
'unionOfQueriesSQL' => $unionOfQueriesSQL,
212+
'postsKeyByTypePluralName' => $postsKeyByTypePluralName,
213+
'hasMorePages' => $hasMorePages,
214+
'queryPlan' => $queryPlan
215+
];
194216
}
195217
}

0 commit comments

Comments
 (0)