Completed
Pull Request — master (#155)
by
unknown
03:10
created

EloquentDataProvider::finalSrc()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.2
cc 4
eloc 7
nc 2
nop 0
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
        return $this->src;
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function filter($fieldName, $operator, $value)
149
    {
150 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...
151
            case "eq":
152
                $operator = '=';
153
                break;
154
            case "n_eq":
155
                $operator = '<>';
156
                break;
157
            case "gt":
158
                $operator = '>';
159
                break;
160
            case "lt":
161
                $operator = '<';
162
                break;
163
            case "ls_e":
164
                $operator = '<=';
165
                break;
166
            case "gt_e":
167
                $operator = '>=';
168
                break;
169
            case "in":
170
                if (!is_array($value)) {
171
                    $operator = '=';
172
                }
173
                break;
174
        }
175
176
        if (strpos($fieldName, '.') !== false) {
177
            $fieldNameParts = explode('.', $fieldName);
178
            $relationName = $fieldNameParts[0];
179
            $endFor = count($fieldNameParts) - 1;
180
            for ($i = 1;$i < $endFor;$i++) {
181
                $relationName .= '.' . $fieldNameParts[$i];
182
            }
183
            $relatedColumnName = $fieldNameParts[$i];
184
            $func = function ($query) use ($relationName, $relatedColumnName, $operator, $value) {
185
                $query
186
                    ->where($relatedColumnName, $operator, $value);
187
            };
188
            $with = [$relationName => $func];
189
190
            $this->with = $with;
191
            if (!isset($this->whereHas[$relationName])) {
192
                $this->whereHas[$relationName] = [];
193
            }
194
            $this->whereHas[$relationName][] = $func;
195
        } else {
196
            $this->src->where($fieldName, $operator, $value);
197
        }
198
199
        return $this;
200
    }
201
}
202