Passed
Push β€” feature/search-closures ( 6f75f3 )
by Fu
11:55
created

RequestCriteria::setBetweenSearchClosure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: guoliang
5
 * Date: 2019/3/11
6
 * Time: 上午10:11.
7
 */
8
9
namespace Modules\Core\Criteria;
10
11
use Closure;
12
use Illuminate\Database\Eloquent\Builder;
13
use Illuminate\Http\Request;
14
use Modules\Core\Traits\Criteria\ParseCrossTrait;
0 ignored issues
show
Bug introduced by
The type Modules\Core\Traits\Criteria\ParseCrossTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Modules\Core\Traits\Criteria\ParseFilterTrait;
16
use Modules\Core\Traits\Criteria\ParseOrderByTrait;
17
use Modules\Core\Traits\Criteria\ParseSearchableTrait;
18
use Modules\Core\Traits\Criteria\ParseWithTrait;
19
use Prettus\Repository\Contracts\CriteriaInterface;
20
use Prettus\Repository\Contracts\RepositoryInterface;
21
22
class RequestCriteria implements CriteriaInterface
23
{
24
    /** @var \Illuminate\Http\Request $request */
25
    protected $request;
26
    /** @var \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder $model */
27
    protected $model;
28
    /** @var \Prettus\Repository\Contracts\RepositoryInterface $repository */
29
    protected $repository;
30
    protected $search;
31
    protected $searchData;
32
    protected $searchFields;
33
    protected $isFirstField;
34
    protected $modelForceAndWhere;
35
    protected $fieldsSearchable;
36
    protected $fields;
37
    protected $filter;
38
    protected $orderBy;
39
    protected $sortedBy;
40
    protected $with;
41
    protected $searchJoin;
42
    protected $acceptedConditions;
43
    protected $originalFields;
44
    protected $searchClosures;
45
46
    use ParseSearchableTrait;
47
    use ParseOrderByTrait;
48
    use ParseFilterTrait;
49
    use ParseWithTrait;
50
51
    public function __construct(Request $request)
52
    {
53
        $this->request = $request;
54
        $this->isFirstField = true;
55
56
        $this->setCrossSearchClosure();
57
        $this->setBetweenSearchClosure();
58
        $this->setInSearchClosure();
59
    }
60
61
    /**
62
     * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|\Illuminate\Database\Query\Builder $model
63
     * @param \Prettus\Repository\Contracts\RepositoryInterface                                                            $repository
64
     *
65
     * @throws \Exception
66
     *
67
     * @return mixed
68
     */
69
    public function apply($model, RepositoryInterface $repository)
70
    {
71
        $this->model = $model;
72
        $this->repository = $repository;
73
74
        $this->parseSearchable();
75
        $this->parseOrderBy();
76
        $this->parseFilter();
77
        $this->parseWith();
78
79
        return $this->model;
80
    }
81
82
    public function setSearchClosure(string $condition, Closure $closure)
83
    {
84
        $this->searchClosures[$condition] = $closure;
85
    }
86
87
    protected function setCrossSearchClosure()
88
    {
89
        $crossMin = config('repository.criteria.cross.min', 'min');
90
        $crossMax = config('repository.criteria.cross.min', 'max');
91
92
        $this->setSearchClosure('cross', function (
93
            Builder $query,
94
            $condition,
95
            $field,
96
            $value,
97
            $modelTableName = null
0 ignored issues
show
Unused Code introduced by
The parameter $modelTableName is not used and could be removed. ( Ignorable by Annotation )

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

97
            /** @scrutinizer ignore-unused */ $modelTableName = null

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
98
        ) use ($crossMin, $crossMax) {
99
            $query->where(function (Builder $query) use ($field, $value, $crossMin, $crossMax) {
100
                $query->where("{$field}_{$crossMin}", '<=', (int) $value[0])
101
                      ->where("{$field}_{$crossMax}", '>=', (int) $value[1]);
102
            })->orWhere(function (Builder $query) use ($field, $value, $crossMin, $crossMax) {
103
                $query->where("{$field}_{$crossMin}", '<=', (int) $value[0])
104
                      ->where("{$field}_{$crossMax}", '>=', (int) $value[0]);
105
            })->orWhere(function (Builder $query) use ($field, $value, $crossMin, $crossMax) {
106
                $query->where("{$field}_{$crossMin}", '>=', (int) $value[0])
107
                      ->where("{$field}_{$crossMax}", '<=', (int) $value[1]);
108
            })->orWhere(function (Builder $query) use ($field, $value, $crossMin, $crossMax) {
109
                $query->where("{$field}_{$crossMin}", '>=', (int) $value[0])
110
                      ->where("{$field}_{$crossMax}", '>=', (int) $value[1])
111
                      ->where("{$field}_{$crossMin}", '<=', (int) $value[1]);
112
            });
113
        });
114
    }
115
116
    protected function setBetweenSearchClosure()
117
    {
118
        $this->setSearchClosure('between', function (
119
            Builder $query,
120
            $condition,
121
            $field,
122
            $value,
123
            $modelTableName = null
0 ignored issues
show
Unused Code introduced by
The parameter $modelTableName is not used and could be removed. ( Ignorable by Annotation )

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

123
            /** @scrutinizer ignore-unused */ $modelTableName = null

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
124
        ) {
125
            $query->whereBetween($field, $value);
126
        });
127
    }
128
129
    protected function setInSearchClosure()
130
    {
131
        $this->setSearchClosure('in', function (Builder $query, $condition, $field, $value, $modelTableName = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $modelTableName is not used and could be removed. ( Ignorable by Annotation )

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

131
        $this->setSearchClosure('in', function (Builder $query, $condition, $field, $value, /** @scrutinizer ignore-unused */ $modelTableName = null) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
132
            $query->whereIn($field, $value);
133
        });
134
    }
135
}
136