Passed
Push — master ( c9d46b...06e49e )
by Zing
12:33 queued 06:36
created

QueryBuilder   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 61
ccs 14
cts 15
cp 0.9333
rs 10
c 4
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fromBuilder() 0 7 2
A __call() 0 5 2
A __construct() 0 4 1
A getBuilder() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zing\QueryBuilder;
6
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Database\Eloquent\Relations\Relation;
10
use Illuminate\Http\Request;
11
use Illuminate\Support\Traits\ForwardsCalls;
12
use Zing\QueryBuilder\Concerns\Pageable;
13
use Zing\QueryBuilder\Concerns\WithFilters;
14
use Zing\QueryBuilder\Concerns\WithSearchable;
15
use Zing\QueryBuilder\Concerns\WithSorts;
16
use Zing\QueryBuilder\Concerns\WithTypedFilter;
17
18
/**
19
 * @mixin \Illuminate\Database\Eloquent\Builder
20
 */
21
class QueryBuilder
22
{
23
    use WithFilters;
24
    use WithSearchable;
25
    use WithSorts;
26
    use Pageable;
27
    use ForwardsCalls;
28
    use WithTypedFilter;
29
30
    /**
31
     * @var \Illuminate\Http\Request
32
     */
33
    protected $request;
34
35
    /**
36
     * @var \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation
37
     */
38
    protected $builder;
39
40
    /**
41
     * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation $builder
42
     * @param \Illuminate\Http\Request $request
43
     */
44 46
    public function __construct($builder, $request)
45
    {
46 46
        $this->builder = $builder;
47 46
        $this->request = $request;
48 46
    }
49
50
    /**
51
     * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation|string $baseQuery
52
     */
53 46
    public static function fromBuilder($baseQuery, Request $request): self
54
    {
55 46
        if (is_subclass_of($baseQuery, Model::class)) {
56 45
            $baseQuery = forward_static_call([$baseQuery, 'query']);
57
        }
58
59 46
        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\QueryBuilder::__construct() does only seem to accept Illuminate\Database\Eloq...uent\Relations\Relation, 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

59
        return new self(/** @scrutinizer ignore-type */ $baseQuery, $request);
Loading history...
60
    }
61
62 40
    public function getBuilder(): Builder
63
    {
64 40
        if ($this->builder instanceof Relation) {
65
            return $this->builder->getQuery();
66
        }
67
68 40
        return $this->builder;
69
    }
70
71
    /**
72
     * @param string $name
73
     * @param mixed[] $arguments
74
     *
75
     * @return $this|mixed
76
     */
77 45
    public function __call($name, $arguments)
78
    {
79 45
        $result = $this->forwardCallTo($this->builder, $name, $arguments);
80
81 45
        return $result === $this->builder ? $this : $result;
82
    }
83
}
84