]> BookStack Code Mirror - bookstack/blob - app/Actions/Queries/WebhooksAllPaginatedAndSorted.php
Revised webhooks list to new format
[bookstack] / app / Actions / Queries / WebhooksAllPaginatedAndSorted.php
1 <?php
2
3 namespace BookStack\Actions\Queries;
4
5 use BookStack\Actions\Webhook;
6 use Illuminate\Pagination\LengthAwarePaginator;
7
8 /**
9  * Get all the webhooks in the system in a paginated format.
10  */
11 class WebhooksAllPaginatedAndSorted
12 {
13     /**
14      * @param array{sort: string, order: string, search: string} $sortData
15      */
16     public function run(int $count, array $sortData): LengthAwarePaginator
17     {
18         $sort = $sortData['sort'];
19
20         $query = Webhook::query()->select(['*'])
21             ->withCount(['trackedEvents'])
22             ->orderBy($sort, $sortData['order']);
23
24         if ($sortData['search']) {
25             $term = '%' . $sortData['search'] . '%';
26             $query->where(function ($query) use ($term) {
27                 $query->where('name', 'like', $term)
28                     ->orWhere('endpoint', 'like', $term);
29             });
30         }
31
32         return $query->paginate($count);
33     }
34 }