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

PostService   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 18
Bugs 6 Features 6
Metric Value
eloc 9
dl 0
loc 21
rs 10
c 18
b 6
f 6
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A getCustomFilters() 0 14 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