Completed
Push — main ( 02dbe2...47b1ff )
by Tan
19s queued 15s
created

CategoryService::search()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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