Passed
Pull Request — master (#29)
by Zing
04:50
created

Queryable   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 42
ccs 15
cts 15
cp 1
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A paginate() 0 5 2
A __construct() 0 9 1
A fromBuilder() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zing\QueryBuilder\Concerns;
6
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Http\Request;
10
11
trait Queryable
12
{
13
    use WithFilters;
14
    use WithSearchable;
15
    use WithSorts;
16
17
    /**
18
     * @var \Illuminate\Http\Request
19
     */
20
    protected $request;
21
22 28
    public function __construct(Builder $builder, $request)
23
    {
24 28
        parent::__construct($builder->getQuery());
25
26 28
        $this->setModel($builder->getModel())->setEagerLoads($builder->getEagerLoads());
0 ignored issues
show
Bug introduced by
It seems like setModel() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

26
        $this->/** @scrutinizer ignore-call */ 
27
               setModel($builder->getModel())->setEagerLoads($builder->getEagerLoads());
Loading history...
27 28
        $this->scopes = $builder->scopes;
0 ignored issues
show
Bug Best Practice introduced by
The property scopes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
28 28
        $this->localMacros = $builder->localMacros;
0 ignored issues
show
Bug Best Practice introduced by
The property localMacros does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
29 28
        $this->onDelete = $builder->onDelete;
0 ignored issues
show
Bug Best Practice introduced by
The property onDelete does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
30 28
        $this->request = $request;
31 28
    }
32
33
    /**
34
     * @param \Illuminate\Database\Eloquent\Builder|string $baseQuery
35
     * @param \Illuminate\Http\Request $request
36
     *
37
     * @return \Zing\QueryBuilder\QueryBuilder
38
     */
39 28
    public static function fromBuilder($baseQuery, Request $request)
40
    {
41 28
        if (is_subclass_of($baseQuery, Model::class)) {
42 28
            $baseQuery = forward_static_call([$baseQuery, 'query']);
43
        }
44
45 28
        return new static($baseQuery, $request);
0 ignored issues
show
Bug introduced by
It seems like $baseQuery can also be of type string; however, parameter $builder of Zing\QueryBuilder\Concer...ueryable::__construct() 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

45
        return new static(/** @scrutinizer ignore-type */ $baseQuery, $request);
Loading history...
46
    }
47
48 1
    public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
49
    {
50 1
        $perPage = $perPage ?: $this->request->input(config('query-builder.per_page.key', 'per_page'), config('query-builder.per_page.value', $this->model->getPerPage()));
51
52 1
        return parent::paginate($perPage, $columns, $pageName, $page);
53
    }
54
}
55