CategoryService   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 1
eloc 10
c 2
b 1
f 1
dl 0
loc 24
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A getCustomFilters() 0 15 1
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\Database\Eloquent\Model;
10
use Illuminate\Support\Arr;
11
12
/**
13
 * Class CategoryService
14
 *
15
 * @package CSlant\Blog\Api\Services
16
 *
17
 * @method BaseHttpResponse httpResponse()
18
 */
19
class CategoryService
20
{
21
    /**
22
     * Get categories by filters.
23
     *
24
     * @param  array<string, mixed>  $filters
25
     *
26
     * @return LengthAwarePaginator<int, Model>
27
     */
28
    public function getCustomFilters(array $filters): LengthAwarePaginator
29
    {
30
        $query = Category::query()
31
            ->withCount('posts');
32
33
        $query = QueryCategory::setBaseCustomFilterQuery($query, $filters);
34
35
        $query = $query
36
            ->wherePublished()
37
            ->orderBy(
38
                Arr::get($filters, 'order_by', 'posts_count'),
39
                Arr::get($filters, 'order', 'desc')
40
            );
41
42
        return $query->paginate((int) $filters['per_page']);
43
    }
44
}
45