Completed
Push — main ( fe3e23...ff0b05 )
by Tan
27s queued 23s
created

CategoryService::search()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 11
c 1
b 0
f 1
nc 3
nop 2
dl 0
loc 19
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 (is_plugin_active('language')
63
            && is_plugin_active('language-advanced')
64
            && Language::getCurrentLocale() != Language::getDefaultLocale()
65
        ) {
66
            return $model
67
                ->whereHas('translations', function (BaseQueryBuilder $query) use ($keyword): void {
68
                    $query->addSearch('name', $keyword, false, false);
69
                });
70
        }
71
72
        return $model
73
            ->where(function (BaseQueryBuilder $query) use ($keyword): void {
74
                $query->addSearch('name', $keyword, false, false);
75
            });
76
    }
77
}
78