Passed
Pull Request — main (#63)
by
unknown
03:22
created

PostService   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
eloc 12
c 4
b 0
f 2
dl 0
loc 28
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A getPostByTags() 0 20 2
1
<?php
2
3
namespace CSlant\Blog\Api\Services;
4
5
use Botble\Base\Models\BaseQueryBuilder;
0 ignored issues
show
Bug introduced by
The type Botble\Base\Models\BaseQueryBuilder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use CSlant\Blog\Core\Http\Responses\Base\BaseHttpResponse;
7
use CSlant\Blog\Core\Models\Post;
8
use Illuminate\Database\Eloquent\Builder;
9
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
10
use Illuminate\Support\Arr;
11
12
/**
13
 * Class PostService
14
 *
15
 * @package CSlant\Blog\Api\Services
16
 *
17
 * @method BaseHttpResponse httpResponse()
18
 */
19
class PostService
20
{
21
    /**
22
     * Get posts by tags.
23
     *
24
     * @param array<string, mixed> $filters
25
     * @return Builder<Post>|LengthAwarePaginator
26
     */
27
    public function getPostByTags(array $filters): Builder|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
        $orderBy = Arr::get($filters, 'order_by', 'updated_at');
40
        $order = Arr::get($filters, 'order', 'desc');
41
42
        $data = $data
43
            ->wherePublished()
44
            ->orderBy($orderBy, $order);
45
46
        return $data->paginate((int) $filters['per_page']);
47
    }
48
}
49