Passed
Pull Request — main (#71)
by
unknown
02:45
created

CategoryService::search()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 14
c 1
b 0
f 1
nc 3
nop 2
dl 0
loc 22
rs 9.2222
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 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\Http\Responses\Base\BaseHttpResponse;
8
use CSlant\Blog\Core\Models\Category;
9
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
10
use Illuminate\Database\Eloquent\Builder;
11
use Illuminate\Database\Eloquent\Model;
12
use Illuminate\Support\Arr;
13
14
/**
15
 * Class CategoryService
16
 *
17
 * @package CSlant\Blog\Api\Services
18
 *
19
 * @method BaseHttpResponse httpResponse()
20
 */
21
class CategoryService
22
{
23
    /**
24
     * Get categories by filters.
25
     *
26
     * @param  array<string, mixed>  $filters
27
     *
28
     * @return LengthAwarePaginator<Category>
29
     */
30
    public function getCustomFilters(array $filters): LengthAwarePaginator
31
    {
32
        $data = Category::query()
33
                    ->withCount('posts');
34
35
        if ($filters['search'] !== null) {
36
            $keyword = isset($filters['search']) ? (string) $filters['search'] : null;
37
            $data = $this->search($data, $keyword);
38
        }
39
40
        $orderBy = Arr::get($filters, 'order_by', 'posts_count');
41
        $order = Arr::get($filters, 'order', 'desc');
42
43
        $data = $data
44
            ->wherePublished()
45
            ->orderBy($orderBy, $order);
46
47
        return $data->paginate((int) $filters['per_page']);
48
    }
49
50
    /**
51
     * @param  BaseQueryBuilder|Builder<Model>  $model
52
     * @param  null|string  $keyword
53
     *
54
     * @return BaseQueryBuilder|Builder<Model>
55
     */
56
    protected function search(Builder|BaseQueryBuilder $model, ?string $keyword): Builder|BaseQueryBuilder
57
    {
58
        if (!$model instanceof BaseQueryBuilder || !$keyword) {
59
            return $model;
60
        }
61
62
        if (
63
            is_plugin_active('language') &&
64
            is_plugin_active('language-advanced') &&
65
            Language::getCurrentLocale() != Language::getDefaultLocale()
66
        ) {
67
            return $model
68
                ->whereHas('translations', function (BaseQueryBuilder $query) use ($keyword): void {
69
                    $query
70
                        ->addSearch('name', $keyword, false, false);
71
                });
72
        }
73
74
        return $model
75
            ->where(function (BaseQueryBuilder $query) use ($keyword): void {
76
                $query
77
                    ->addSearch('name', $keyword, false, false);
78
            });
79
    }
80
}
81