Completed
Push — master ( f2a787...96d856 )
by Vitaliy
03:16
created

EloquentDataProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 4
c 1
b 1
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
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
    /**
19
     * Constructor.
20
     *
21
     * @param Builder $src
22
     */
23
    public function __construct(Builder $src)
24
    {
25
        parent::__construct($src);
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function reset()
32
    {
33
        $this->getIterator()->rewind();
34
        return $this;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function getCollection()
41
    {
42
        if (!$this->collection) {
43
            $paginator = $this->getPaginator();
44
            if (version_compare(Application::VERSION, '5', '<')) {
45
                $this->collection = $paginator->getCollection();
46
            } else {
47
                $this->collection = Collection::make(
48
                    $this->getPaginator()->items()
49
                );
50
            }
51
        }
52
        return $this->collection;
53
    }
54
55
    public function getPaginator()
56
    {
57
        if (!$this->paginator) {
58
            $this->paginator = $this->src->paginate($this->page_size);
59
        }
60
        return $this->paginator;
61
    }
62
63
    /**
64
     * @return \Illuminate\Pagination\Factory
65
     */
66
    public function getPaginationFactory()
67
    {
68
        return $this->src->getQuery()->getConnection()->getPaginator();
69
    }
70
71
    protected function getIterator()
72
    {
73
        if (!$this->iterator) {
74
            $this->iterator = $this->getCollection()->getIterator();
75
        }
76
        return $this->iterator;
77
    }
78
79
    /**
80
     * @return Builder
81
     */
82
    public function getBuilder()
83
    {
84
        return $this->src;
85
    }
86
87 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...
88
    {
89
        if ($this->index < $this->count()) {
90
            $this->index++;
91
            $item = $this->iterator->current();
92
            $this->iterator->next();
93
            $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...
94
            Event::fire(self::EVENT_FETCH_ROW, [$row, $this]);
95
            return $row;
96
        } else {
97
            return null;
98
        }
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function count()
105
    {
106
        return $this->getCollection()->count();
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function orderBy($fieldName, $direction)
113
    {
114
        $this->src->orderBy($fieldName, $direction);
115
        return $this;
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 View Code Duplication
    public function filter($fieldName, $operator, $value)
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...
122
    {
123
        switch ($operator) {
124
            case "eq":
125
                $operator = '=';
126
                break;
127
            case "n_eq":
128
                $operator = '<>';    
129
                break;
130
            case "gt":
131
                $operator = '>';    
132
                 break;
133
            case "lt":
134
                $operator = '<';    
135
                break;
136
            case "ls_e":
137
                $operator = '<=';    
138
                break;
139
            case "gt_e":
140
                $operator = '>=';    
141
                break;
142
            case "in":
143
                if (!is_array($value)) {
144
                    $operator = '=';
145
                    break;
146
                }
147
                $this->src->whereIn($fieldName, $value);
148
                return $this;
149
        }
150
        $this->src->where($fieldName, $operator, $value);
151
        return $this;
152
    }
153
}
154