Passed
Push — master ( 88e0f5...509ad8 )
by Zing
05:22
created

QueryBuilder::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 0
Metric Value
cc 2
eloc 2
nc 1
nop 4
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Created by PhpStorm.
7
 * User: zing
8
 * Date: 2018/12/26
9
 * Time: 7:14 PM.
10
 */
11
12
namespace Zing\QueryBuilder;
13
14
use Illuminate\Database\Eloquent\Builder;
15
use Illuminate\Database\Eloquent\Model;
16
use Illuminate\Http\Request;
17
use Zing\QueryBuilder\Concerns\WithFilters;
18
use Zing\QueryBuilder\Concerns\WithSearchable;
19
use Zing\QueryBuilder\Concerns\WithSorts;
20
21
class QueryBuilder extends Builder
22
{
23
    use WithFilters;
24
    use WithSearchable;
25
    use WithSorts;
26
27
    /**
28
     * @var \Illuminate\Http\Request
29
     */
30
    protected $request;
31
32 21
    public function __construct(Builder $builder, $request)
33
    {
34 21
        parent::__construct($builder->getQuery());
35 21
        $this->setModel($builder->getModel())->setEagerLoads($builder->getEagerLoads());
36 21
        $this->scopes = $builder->scopes;
37 21
        $this->localMacros = $builder->localMacros;
38 21
        $this->onDelete = $builder->onDelete;
39 21
        $this->request = $request;
40 21
    }
41
42
    /**
43
     * @param \Illuminate\Database\Eloquent\Builder|string $baseQuery
44
     * @param \Illuminate\Http\Request $request
45
     *
46
     * @return \Zing\QueryBuilder\QueryBuilder
47
     */
48 21
    public static function fromBuilder($baseQuery, Request $request)
49
    {
50 21
        if (is_subclass_of($baseQuery, Model::class)) {
51 21
            $baseQuery = $baseQuery::query();
52
        }
53
54 21
        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\QueryBuilder::__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

54
        return new static(/** @scrutinizer ignore-type */ $baseQuery, $request);
Loading history...
55
    }
56
57 1
    public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
58
    {
59 1
        $perPage = $perPage ?: $this->request->input(config('query-builder.per_page.key', 'per_page'), config('query-builder.per_page.value', $this->model->getPerPage()));
60
61 1
        return parent::paginate($perPage, $columns, $pageName, $page);
62
    }
63
}
64