-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathQueryPopular.php
More file actions
42 lines (34 loc) · 1.3 KB
/
QueryPopular.php
File metadata and controls
42 lines (34 loc) · 1.3 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
<?php
namespace BookStack\Entities\Queries;
use BookStack\Activity\Models\View;
use BookStack\Entities\EntityProvider;
use BookStack\Entities\Tools\MixedEntityListLoader;
use BookStack\Permissions\PermissionApplicator;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
class QueryPopular
{
public function __construct(
protected PermissionApplicator $permissions,
protected EntityProvider $entityProvider,
protected MixedEntityListLoader $listLoader,
) {
}
public function run(int $count, int $page, array $filterModels): Collection
{
$query = $this->permissions
->restrictEntityRelationQuery(View::query(), 'views', 'viewable_id', 'viewable_type')
->select('*', 'viewable_id', 'viewable_type', DB::raw('SUM(views) as view_count'))
->groupBy('viewable_id', 'viewable_type')
->orderBy('view_count', 'desc');
if (!empty($filterModels)) {
$query->whereIn('viewable_type', $this->entityProvider->getMorphClasses($filterModels));
}
$views = $query
->skip($count * ($page - 1))
->take($count)
->get();
$this->listLoader->loadIntoRelations($views->all(), 'viewable', true);
return $views->pluck('viewable')->filter();
}
}