Completed
Pull Request — master (#155)
by
unknown
02:36
created

EloquentDataProvider::filter()   C

Complexity

Conditions 12
Paths 27

Size

Total Lines 52
Code Lines 42

Duplication

Lines 25
Ratio 48.08 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 25
loc 52
rs 5.9842
cc 12
eloc 42
nc 27
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Nayjest\Grids;
3
4
use Illuminate\Database\Eloquent\Builder;
5
use Event;
6
use Illuminate\Foundation\Application;
7
use Illuminate\Support\Collection;
8
9
class EloquentDataProvider extends DataProvider
10
{
11
    protected $collection;
12
13
    protected $paginator;
14
15
    /** @var  $iterator \ArrayIterator */
16
    protected $iterator;
17
18
    protected $with = [];
19
20
    protected $whereHas = [];
21
22
    /**
23
     * @var Builder
24
     */
25
    protected $src;
26
27
    /**
28
     * Constructor.
29
     *
30
     * @param Builder $src
31
     */
32
    public function __construct(Builder $src)
33
    {
34
        parent::__construct($src);
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function reset()
41
    {
42
        $this->getIterator()->rewind();
43
        return $this;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getCollection()
50
    {
51
        if (!$this->collection) {
52
            $paginator = $this->getPaginator();
53
            if (version_compare(Application::VERSION, '5', '<')) {
54
                $this->collection = $paginator->getCollection();
55
            } else {
56
                $this->collection = Collection::make(
57
                    $this->getPaginator()->items()
58
                );
59
            }
60
        }
61
        return $this->collection;
62
    }
63
64
    public function getPaginator()
65
    {
66
        if (!$this->paginator) {
67
            $this->paginator = $this->finalSrc()->paginate($this->page_size);
68
        }
69
        return $this->paginator;
70
    }
71
72
    /**
73
     * @return \Illuminate\Pagination\Factory
74
     */
75
    public function getPaginationFactory()
76
    {
77
        return $this->finalSrc()->getQuery()->getConnection()->getPaginator();
78
    }
79
80
    protected function getIterator()
81
    {
82
        if (!$this->iterator) {
83
            $this->iterator = $this->getCollection()->getIterator();
84
        }
85
        return $this->iterator;
86
    }
87
88
    /**
89
     * @return Builder
90
     */
91
    public function getBuilder()
92
    {
93
        return $this->finalSrc();
94
    }
95
96 View Code Duplication
    public function getRow()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
        if ($this->index < $this->count()) {
99
            $this->index++;
100
            $item = $this->iterator->current();
101
            $this->iterator->next();
102
            $row = new EloquentDataRow($item, $this->getRowId());
0 ignored issues
show
Deprecated Code introduced by
The class Nayjest\Grids\EloquentDataRow has been deprecated with message: Class EloquentDataRow

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
103
            Event::fire(self::EVENT_FETCH_ROW, [$row, $this]);
104
            return $row;
105
        } else {
106
            return null;
107
        }
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function count()
114
    {
115
        return $this->getCollection()->count();
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function orderBy($fieldName, $direction)
122
    {
123
        $this->src->orderBy($fieldName, $direction);
124
        return $this;
125
    }
126
127
    /**
128
     * @return Builder
129
     */
130
    public function finalSrc()
131
    {
132
        if ($this->whereHas !== []) {
133
            foreach ($this->whereHas as $relation => $funcs) {
134
                $this->src->whereHas($relation, function($query) use ($funcs) {
135
                    foreach ($funcs as $func) {
136
                        call_user_func($func, $query);
137
                    }
138
                });
139
            }
140
        }
141
142
        if ($this->with !== []) {
143
            $this->src->with($this->with);
144
        }
145
146
        return $this->src;
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function filter($fieldName, $operator, $value)
153
    {
154 View Code Duplication
        switch ($operator) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
            case "eq":
156
                $operator = '=';
157
                break;
158
            case "n_eq":
159
                $operator = '<>';
160
                break;
161
            case "gt":
162
                $operator = '>';
163
                break;
164
            case "lt":
165
                $operator = '<';
166
                break;
167
            case "ls_e":
168
                $operator = '<=';
169
                break;
170
            case "gt_e":
171
                $operator = '>=';
172
                break;
173
            case "in":
174
                if (!is_array($value)) {
175
                    $operator = '=';
176
                }
177
                break;
178
        }
179
180
        if (strpos($fieldName, '.') !== false) {
181
            $fieldNameParts = explode('.', $fieldName);
182
            $relationName = $fieldNameParts[0];
183
            $endFor = count($fieldNameParts) - 1;
184
            for ($i = 1;$i < $endFor;$i++) {
185
                $relationName .= '.' . $fieldNameParts[$i];
186
            }
187
            $relatedColumnName = $fieldNameParts[$i];
188
            $func = function ($query) use ($relationName, $relatedColumnName, $operator, $value) {
189
                $query
190
                    ->where($relatedColumnName, $operator, $value);
191
            };
192
            $with = [$relationName => $func];
193
            $this->with = array_merge($this->with, $with);
194
            if (!isset($this->whereHas[$relationName])) {
195
                $this->whereHas[$relationName] = [];
196
            }
197
            $this->whereHas[$relationName][] = $func;
198
        } else {
199
            $this->src->where($fieldName, $operator, $value);
200
        }
201
202
        return $this;
203
    }
204
}
205