Completed
Push — main ( 02dbe2...47b1ff )
by Tan
19s queued 15s
created

QueryCategory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 16
c 1
b 0
f 1
dl 0
loc 45
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setBaseCustomFilterQuery() 0 10 3
A search() 0 19 6
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\Category;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Query\Builder;
10
11
class QueryCategory
12
{
13
    /**
14
     * @param  BaseQueryBuilder|Builder|Category  $query
15
     * @param  array<string, mixed>  $filters
16
     *
17
     * @return BaseQueryBuilder|Builder|Category
18
     */
19
    public static function setBaseCustomFilterQuery(
20
        Builder|BaseQueryBuilder|Category $query,
21
        array $filters
22
    ): Builder|BaseQueryBuilder|Category {
23
        if ($filters['search'] !== null) {
24
            $keyword = isset($filters['search']) ? (string) $filters['search'] : null;
25
            $query = self::search($query, $keyword);
26
        }
27
28
        return $query;
29
    }
30
31
    /**
32
     * @param  BaseQueryBuilder|Builder<Model>  $model
33
     * @param  null|string  $keyword
34
     *
35
     * @return BaseQueryBuilder|Builder<Model>
36
     */
37
    protected static function search(Builder|BaseQueryBuilder $model, ?string $keyword): Builder|BaseQueryBuilder
38
    {
39
        if (!$model instanceof BaseQueryBuilder || !$keyword) {
40
            return $model;
41
        }
42
43
        if (is_plugin_active('language')
44
            && is_plugin_active('language-advanced')
45
            && Language::getCurrentLocale() != Language::getDefaultLocale()
46
        ) {
47
            return $model
48
                ->whereHas('translations', function (BaseQueryBuilder $query) use ($keyword): void {
49
                    $query->addSearch('name', $keyword, false, false);
50
                });
51
        }
52
53
        return $model
54
            ->where(function (BaseQueryBuilder $query) use ($keyword): void {
55
                $query->addSearch('name', $keyword, false, false);
56
            });
57
    }
58
}
59