QueryPost::setBaseCustomFilterQuery()   F
last analyzed

Complexity

Conditions 11
Paths 768

Size

Total Lines 53
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 26
nc 768
nop 2
dl 0
loc 53
rs 3.4722
c 1
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace CSlant\Blog\Api\Supports\Queries;
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 Botble\Language\Facades\Language;
0 ignored issues
show
Bug introduced by
The type Botble\Language\Facades\Language 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...
7
use CSlant\Blog\Core\Models\Post;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Query\Builder;
10
11
class QueryPost
12
{
13
    /**
14
     * @param  BaseQueryBuilder|Builder<Model>  $query
15
     * @param  array<string, mixed>  $filters
16
     *
17
     * @return BaseQueryBuilder|Builder<Model>|Post
18
     */
19
    public static function setBaseCustomFilterQuery(
20
        Builder|BaseQueryBuilder $query,
21
        array $filters
22
    ): Builder|BaseQueryBuilder|Post {
23
        if ($filters['tags'] !== null) {
24
            $tags = array_filter((array) $filters['tags']);
25
26
            $query = $query->whereHas('tags', function (BaseQueryBuilder|Builder $query) use ($tags): void {
27
                $query->whereIn('tags.id', $tags);
28
            });
29
        }
30
31
        if ($filters['categories'] !== null) {
32
            $categories = array_filter((array) $filters['categories']);
33
34
            $query = $query->whereHas('categories', function (BaseQueryBuilder|Builder $query) use ($categories): void {
35
                $query->whereIn('categories.id', $categories);
36
            });
37
        }
38
39
        if ($filters['categories_exclude'] !== null) {
40
            $query = $query
41
                ->whereHas('categories', function (BaseQueryBuilder|Builder $query) use ($filters): void {
42
                    $query->whereNotIn('categories.id', array_filter((array) $filters['categories_exclude']));
43
                });
44
        }
45
46
        if ($filters['exclude'] !== null) {
47
            $query = $query->whereNotIn('id', array_filter((array) $filters['exclude']));
48
        }
49
50
        if ($filters['include'] !== null) {
51
            $query = $query->whereNotIn('id', array_filter((array) $filters['include']));
52
        }
53
54
        if ($filters['author'] !== null) {
55
            $query = $query->whereIn('author_id', array_filter((array) $filters['author']));
56
        }
57
58
        if ($filters['author_exclude'] !== null) {
59
            $query = $query->whereNotIn('author_id', array_filter((array) $filters['author_exclude']));
60
        }
61
62
        if ($filters['featured'] !== null) {
63
            $query = $query->where('is_featured', $filters['featured']);
64
        }
65
66
        if ($filters['search'] !== null) {
67
            $keyword = isset($filters['search']) ? (string) $filters['search'] : null;
68
            $query = self::search($query, $keyword);
69
        }
70
71
        return $query;
72
    }
73
74
    /**
75
     * @param  BaseQueryBuilder|Builder<Model>  $model
76
     * @param  null|string  $keyword
77
     *
78
     * @return BaseQueryBuilder|Builder<Model>
79
     */
80
    public static function search(Builder|BaseQueryBuilder $model, ?string $keyword): Builder|BaseQueryBuilder
81
    {
82
        if (!$model instanceof BaseQueryBuilder || !$keyword) {
83
            return $model;
84
        }
85
86
        if (is_plugin_active('language')
87
            && is_plugin_active('language-advanced')
88
            && Language::getCurrentLocale() != Language::getDefaultLocale()
89
        ) {
90
            return $model
91
                ->whereHas('translations', function (BaseQueryBuilder $query) use ($keyword): void {
92
                    $query->addSearch('name', $keyword, false, false)
93
                        ->addSearch('description', $keyword, false);
94
                });
95
        }
96
97
        return $model
98
            ->where(function (BaseQueryBuilder $query) use ($keyword): void {
99
                $query->addSearch('name', $keyword, false, false)
100
                    ->addSearch('description', $keyword, false);
101
            });
102
    }
103
}
104