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

CategoryService   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A search() 0 22 6
A getCustomFilters() 0 17 3
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
34
        if ($filters['search'] !== null) {
35
            $keyword = isset($filters['search']) ? (string) $filters['search'] : null;
36
            $data = $this->search($data, $keyword);
37
        }
38
39
        $orderBy = Arr::get($filters, 'order_by', 'updated_at');
40
        $order = Arr::get($filters, 'order', 'desc');
41
42
        $data = $data
43
            ->wherePublished()
44
            ->orderBy($orderBy, $order);
45
46
        return $data->paginate((int) $filters['per_page']);
47
    }
48
49
    /**
50
     * @param  BaseQueryBuilder|Builder<Model>  $model
51
     * @param  null|string  $keyword
52
     *
53
     * @return BaseQueryBuilder|Builder<Model>
54
     */
55
    protected function search(Builder|BaseQueryBuilder $model, ?string $keyword): Builder|BaseQueryBuilder
56
    {
57
        if (!$model instanceof BaseQueryBuilder || !$keyword) {
58
            return $model;
59
        }
60
61
        if (
62
            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
69
                        ->addSearch('name', $keyword, false, false);
70
                });
71
        }
72
73
        return $model
74
            ->where(function (BaseQueryBuilder $query) use ($keyword): void {
75
                $query
76
                    ->addSearch('name', $keyword, false, false);
77
            });
78
    }
79
}
80