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

PostService   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
eloc 12
c 3
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\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 tags.
22
     *
23
     * @param array<string, mixed> $filters
24
     * @return BaseQueryBuilder|Builder<Post>
25
     */
26
    public function getPostByTags(array $filters): Builder|BaseQueryBuilder
27
    {
28
        $data = Post::query();
29
30
        if ($filters['tags'] !== null) {
31
            $tags = array_filter((array) $filters['tags']);
32
33
            $data = $data->whereHas('tags', function (Builder $query) use ($tags): void {
34
                $query->whereIn('tags.id', $tags);
35
            });
36
        }
37
38
        $orderBy = Arr::get($filters, 'order_by', 'updated_at');
39
        $order = Arr::get($filters, 'order', 'desc');
40
41
        $data = $data
42
            ->wherePublished()
43
            ->orderBy($orderBy, $order);
44
45
        return $data->paginate((int) $filters['per_page']);
46
    }
47
}
48