Passed
Pull Request — master (#37)
by Zing
06:39
created

Queryable::paginate()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 2
rs 10
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 32
    public function __construct(Builder $builder, $request)
23
    {
24 32
        parent::__construct($builder->getQuery());
25
26 32
        $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 32
        $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 32
        $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 32
        $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 32
        $this->request = $request;
31 32
    }
32
33
    /**
34
     * @param \Illuminate\Database\Eloquent\Builder|string $baseQuery
35
     * @param \Illuminate\Http\Request $request
36
     *
37
     * @return \Zing\QueryBuilder\Builders\QueryBuilder
38
     */
39 31
    public static function fromBuilder($baseQuery, Request $request): self
40
    {
41 31
        if (is_subclass_of($baseQuery, Model::class)) {
42 31
            $baseQuery = forward_static_call([$baseQuery, 'query']);
43
        }
44
45 31
        return new self($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 self(/** @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((int) $perPage, $columns, $pageName, $page);
53
    }
54
}
55