Passed
Pull Request — main (#69)
by
unknown
05:50 queued 02:54
created

PostService::search()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 16
c 1
b 0
f 1
nc 3
nop 2
dl 0
loc 24
rs 9.1111
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
11
/**
12
 * Class PostService
13
 *
14
 * @package CSlant\Blog\Api\Services
15
 *
16
 * @method BaseHttpResponse httpResponse()
17
 */
18
class PostService
19
{
20
    /**
21
     * Get posts by filters.
22
     *
23
     * @param  array<string, mixed>  $filters
24
     *
25
     * @return LengthAwarePaginator<Post>
26
     */
27
    public function getCustomFilters(array $filters): LengthAwarePaginator
28
    {
29
        $data = Post::query();
30
31
        if ($filters['tags'] !== null) {
32
            $tags = array_filter((array) $filters['tags']);
33
34
            $data = $data->whereHas('tags', function (Builder $query) use ($tags): void {
35
                $query->whereIn('tags.id', $tags);
36
            });
37
        }
38
39
        if ($filters['categories'] !== null) {
40
            $categories = array_filter((array) $filters['categories']);
41
42
            $data = $data->whereHas('categories', function (Builder $query) use ($categories): void {
43
                $query->whereIn('categories.id', $categories);
44
            });
45
        }
46
47
        if ($filters['categories_exclude'] !== null) {
48
            $data = $data
49
                ->whereHas('categories', function (Builder $query) use ($filters): void {
50
                    $query->whereNotIn('categories.id', array_filter((array) $filters['categories_exclude']));
51
                });
52
        }
53
54
        if ($filters['exclude'] !== null) {
55
            $data = $data->whereNotIn('id', array_filter((array) $filters['exclude']));
56
        }
57
58
        if ($filters['include'] !== null) {
59
            $data = $data->whereNotIn('id', array_filter((array) $filters['include']));
60
        }
61
62
        if ($filters['author'] !== null) {
63
            $data = $data->whereIn('author_id', array_filter((array) $filters['author']));
64
        }
65
66
        if ($filters['author_exclude'] !== null) {
67
            $data = $data->whereNotIn('author_id', array_filter((array) $filters['author_exclude']));
68
        }
69
70
        if ($filters['featured'] !== null) {
71
            $data = $data->where('is_featured', $filters['featured']);
72
        }
73
74
        if ($filters['search'] !== null) {
75
            $keyword = isset($filters['search']) ? (string) $filters['search'] : null;
76
            $data = $this->search($data, $keyword);
0 ignored issues
show
Bug introduced by
The method search() does not exist on CSlant\Blog\Api\Services\PostService. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
            /** @scrutinizer ignore-call */ 
77
            $data = $this->search($data, $keyword);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
        }
78
79
        $orderBy = Arr::get($filters, 'order_by', 'updated_at');
80
        $order = Arr::get($filters, 'order', 'desc');
81
82
        $data = $data
83
            ->wherePublished()
84
            ->orderBy($orderBy, $order);
85
86
        return $data->paginate((int) $filters['per_page']);
87
    }
88
}
89