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

DbalDataProvider::filter()   D

Complexity

Conditions 9
Paths 9

Size

Total Lines 32
Code Lines 28

Duplication

Lines 32
Ratio 100 %

Importance

Changes 4
Bugs 2 Features 1
Metric Value
cc 9
eloc 28
c 4
b 2
f 1
nc 9
nop 3
dl 32
loc 32
rs 4.909
1
<?php
2
namespace Nayjest\Grids;
3
4
use DB;
5
use Doctrine\DBAL\Query\QueryBuilder;
6
use Event;
7
use Illuminate\Foundation\Application;
8
use Illuminate\Support\Collection;
9
10
class DbalDataProvider extends DataProvider
11
{
12
    protected $collection;
13
14
    protected $paginator;
15
16
    /** @var  $iterator \ArrayIterator */
17
    protected $iterator;
18
19
    /**
20
     * Set true if Laravel query logging required.
21
     * Fails when using Connection::PARAM_INT_ARRAY parameters
22
     * @var bool
23
     */
24
    protected $exec_using_laravel = false;
25
26
    /**
27
     * Constructor.
28
     *
29
     * @param QueryBuilder $src
30
     */
31
    public function __construct(QueryBuilder $src)
32
    {
33
        parent::__construct($src);
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function reset()
40
    {
41
        $this->getIterator()->rewind();
42
        return $this;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function getCollection()
49
    {
50
51
        if (!$this->collection) {
52
            $query = clone $this->src;
53
            $query
54
                ->setFirstResult(
55
                    ($this->getCurrentPage() - 1) * $this->page_size
56
                )
57
                ->setMaxResults($this->page_size);
58
            if ($this->isExecUsingLaravel()) {
59
                $res = DB::select($query, $query->getParameters());
60
            } else {
61
                $res = $query->execute()->fetchAll(\PDO::FETCH_OBJ);
62
            }
63
            $this->collection = Collection::make($res);
64
        }
65
        return $this->collection;
66
    }
67
68
69
    public function getPaginator()
70
    {
71
        if (!$this->paginator) {
72
            $items = $this->getCollection()->toArray();
73
            if (version_compare(Application::VERSION, '5.0.0', '<')) {
74
                $this->paginator = \Paginator::make(
75
                    $items,
76
                    $this->getTotalRowsCount(),
77
                    $this->page_size
78
                );
79
            } else {
80
                $this->paginator = new \Illuminate\Pagination\LengthAwarePaginator(
81
                    $items,
82
                    $this->getTotalRowsCount(),
83
                    $this->page_size,
84
                    $this->getCurrentPage(),
85
                    [
86
                        'path' => \Illuminate\Pagination\Paginator::resolveCurrentPath()
87
                    ]
88
                );
89
            }
90
        }
91
        return $this->paginator;
92
    }
93
94
    /**
95
     * @return \Illuminate\Pagination\Factory
96
     */
97
    public function getPaginationFactory()
98
    {
99
        return \App::make('paginator');
100
    }
101
102
    protected function getIterator()
103
    {
104
        if (!$this->iterator) {
105
            $this->iterator = $this->getCollection()->getIterator();
106
        }
107
        return $this->iterator;
108
    }
109
110
    /**
111
     * @return QueryBuilder
112
     */
113
    public function getBuilder()
114
    {
115
        return $this->src;
116
    }
117
118 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...
119
    {
120
        if ($this->index < $this->getCurrentPageRowsCount()) {
121
            $this->index++;
122
            $item = $this->iterator->current();
123
            $this->iterator->next();
124
            $row = new ObjectDataRow($item, $this->getRowId());
125
            Event::fire(self::EVENT_FETCH_ROW, [$row, $this]);
126
            return $row;
127
        } else {
128
            return null;
129
        }
130
    }
131
132
    protected $_count;
133
134
    /**
135
     * @deprecated
136
     * @return int
137
     */
138
    public function count()
139
    {
140
        return $this->getCurrentPageRowsCount();
141
    }
142
143
    public function getTotalRowsCount()
144
    {
145
        return $this->src->execute()->rowCount();
146
    }
147
148
    public function getCurrentPageRowsCount()
149
    {
150
        return $this->getCollection()->count();
151
    }
152
153
    public function orderBy($fieldName, $direction)
154
    {
155
        $this->src->orderBy($fieldName, $direction);
156
        return $this;
157
    }
158
159 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...
160
    {
161
         switch ($operator) {
162
            case "eq":
163
                $operator = '=';
164
                break;
165
            case "n_eq":
166
                $operator = '<>';    
167
                break;
168
            case "gt":
169
                $operator = '>';    
170
                 break;
171
            case "lt":
172
                $operator = '<';    
173
                break;
174
            case "ls_e":
175
                $operator = '<=';    
176
                break;
177
            case "gt_e":
178
                $operator = '>=';    
179
                break;
180
            case "in":
181
                $operator = 'IN';
182
                if (!is_array($value)) {
183
                    $operator = '=';
184
                }
185
                break;
186
        }
187
        $this->src->andWhere("$fieldName $operator :$fieldName");
188
        $this->src->setParameter($fieldName, $value);
189
        return $this;
190
    }
191
192
    /**
193
     * @return boolean
194
     */
195
    public function isExecUsingLaravel()
196
    {
197
        return $this->exec_using_laravel;
198
    }
199
200
    /**
201
     * @param boolean $execUsingLaravel
202
     */
203
    public function setExecUsingLaravel($execUsingLaravel)
204
    {
205
        $this->exec_using_laravel = $execUsingLaravel;
206
    }
207
208
}
209