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

PostService   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

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