Passed
Pull Request — main (#83)
by Tan
03:02
created

PostService::getCustomFilters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 14
rs 10
c 2
b 1
f 1
1
<?php
2
3
namespace CSlant\Blog\Api\Services;
4
5
use CSlant\Blog\Api\Supports\Queries\QueryPost;
6
use CSlant\Blog\Core\Http\Responses\Base\BaseHttpResponse;
7
use CSlant\Blog\Core\Models\Post;
8
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
9
use Illuminate\Support\Arr;
10
11
/**
12
 * Class PostService
13
 *
14
 * @package CSlant\Blog\Api\Services
15
 *
16
 * @method BaseHttpResponse httpResponse()
17
 */
18
class PostService
19
{
20
    /**
21
     * @param  array<string, mixed>  $filters
22
     *
23
     * @return LengthAwarePaginator<int, Post>
24
     */
25
    public function getCustomFilters(array $filters): LengthAwarePaginator
26
    {
27
        $query = Post::query()->withCount(['comments', 'likes'])->with(['comments', 'likes']);
28
29
        $query = QueryPost::setBaseCustomFilterQuery($query, $filters);
30
31
        $data = $query
32
            ->wherePublished()
33
            ->orderBy(
34
                Arr::get($filters, 'order_by', 'updated_at'),
35
                Arr::get($filters, 'order', 'desc')
36
            );
37
38
        return $data->paginate((int) $filters['per_page']);
39
    }
40
}
41