Passed
Pull Request — main (#63)
by
unknown
02:45 queued 11s
created

PostService::getFilters()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 20
rs 9.9
1
<?php
2
3
namespace CSlant\Blog\Api\Services;
4
5
use CSlant\Blog\Core\Http\Responses\Base\BaseHttpResponse;
6
use CSlant\Blog\Core\Models\Post;
7
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
8
use Illuminate\Database\Eloquent\Builder;
9
use Illuminate\Support\Arr;
10
use Illuminate\Support\Collection;
11
12
/**
13
 * Class PostService
14
 *
15
 * @package CSlant\Blog\Api\Services
16
 *
17
 * @method BaseHttpResponse httpResponse()
18
 */
19
class PostService
20
{
21
    public function getFilters(array $filters): Collection|LengthAwarePaginator
22
    {
23
        $data = Post::query();
24
25
        if ($filters['tags'] !== null) {
26
            $tags = array_filter((array) $filters['tags']);
27
28
            $data = $data->whereHas('tags', function (Builder $query) use ($tags): void {
29
                $query->whereIn('tags.id', $tags);
30
            });
31
        }
32
33
        $orderBy = Arr::get($filters, 'order_by', 'updated_at');
34
        $order = Arr::get($filters, 'order', 'desc');
35
36
        $data = $data
37
            ->wherePublished()
38
            ->orderBy($orderBy, $order);
39
40
        return $data->paginate((int) $filters['per_page']);
41
    }
42
}
43