Index   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 18
c 2
b 0
f 1
dl 0
loc 33
ccs 18
cts 18
cp 1
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 21 1
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Http\Controllers\Api\User;
6
7
use App\Contracts\Http\Responses\ResponseFactory;
8
use App\Http\Requests\Api\User\IndexRequest;
9
use App\Models\User;
10
use Illuminate\Database\Eloquent\Builder;
11
use Illuminate\Http\JsonResponse;
12
use KoenHoeijmakers\LaravelFilterable\Contracts\Filtering;
13
14
final class Index
15
{
16
    private ResponseFactory $responseFactory;
17
18
    private Filtering $filtering;
19
20 7
    public function __construct(ResponseFactory $responseFactory, Filtering $filtering)
21
    {
22 7
        $this->responseFactory = $responseFactory;
23 7
        $this->filtering = $filtering;
24 7
    }
25
26 7
    public function __invoke(IndexRequest $request): JsonResponse
27
    {
28 7
        $builder = User::query()->select(['id', 'name', 'email']);
29
30 7
        $this->filtering->builder($builder)
0 ignored issues
show
Bug introduced by
It seems like $builder can also be of type Illuminate\Database\Query\Builder; however, parameter $builder of KoenHoeijmakers\LaravelF...ts\Filtering::builder() does only seem to accept Illuminate\Database\Eloquent\Builder, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
        $this->filtering->builder(/** @scrutinizer ignore-type */ $builder)
Loading history...
31
            ->filterFor('name', static function (Builder $builder, string $value) {
32 1
                $builder->where('name', 'like', '%' . $value . '%');
33 7
            })
34
            ->filterFor('email', static function (Builder $builder, string $value) {
35 1
                $builder->where('email', 'like', '%' . $value . '%');
36 7
            })
37 7
            ->sortFor('name')
38 7
            ->sortFor('email')
39 7
            ->defaultSorting('name')
40 7
            ->filter();
41
42 7
        $paginator = $builder->paginate(
43 7
            $request->input('per_page')
44
        );
45
46 7
        return $this->responseFactory->paginator($paginator);
47
    }
48
}
49
50