Completed
Push — master ( 8e9bfc...80c1ab )
by Pavel
10:40
created

NextrasDataSource::limit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
/**
4
 * @copyright   Copyright (c) 2015 ublaboo <[email protected]>
5
 * @author      Pavel Janda <[email protected]>
6
 * @package     Ublaboo
7
 */
8
9
namespace Ublaboo\DataGrid\DataSource;
10
11
use Ublaboo\DataGrid\Filter;
12
use Nette\Utils\Callback;
13
use Nette\Utils\Strings;
14
use Ublaboo\DataGrid\Utils\Sorting;
15
use Nextras\Orm\Collection\ICollection;
16
use Ublaboo\DataGrid\Utils\ArraysHelper;
17
18
class NextrasDataSource extends FilterableDataSource implements IDataSource
19
{
20
21
	/**
22
	 * @var QueryBuilder
23
	 */
24
	protected $data_source;
25
26
	/**
27
	 * @var array
28
	 */
29
	protected $data = [];
30
31
	/**
32
	 * @var string
33
	 */
34
	protected $primary_key;
35
36
37
	/**
38
	 * @param ICollection  $data_source
39
	 * @param string       $primary_key
40
	 */
41
	public function __construct(ICollection $data_source, $primary_key)
42
	{
43
		$this->data_source = $data_source;
0 ignored issues
show
Documentation Bug introduced by
It seems like $data_source of type object<Nextras\Orm\Collection\ICollection> is incompatible with the declared type object<Ublaboo\DataGrid\DataSource\QueryBuilder> of property $data_source.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
44
		$this->primary_key = $primary_key;
45
	}
46
47
48
	/**
49
	 * @return Doctrine\ORM\Query
50
	*/
51
	public function getQuery()
52
	{
53
		return $this->data_source->getQuery();
54
	}
55
56
57
	/********************************************************************************
58
	 *                          IDataSource implementation                          *
59
	 ********************************************************************************/
60
61
62
	/**
63
	 * Get count of data
64
	 * @return int
65
	 */
66
	public function getCount()
67
	{
68
		return $this->data_source->countStored();
69
70
		return $count;
0 ignored issues
show
Unused Code introduced by
return $count; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
71
	}
72
73
	/**
74
	 * Get the data
75
	 * @return array
76
	 */
77
	public function getData()
78
	{
79
		/**
80
		 * Paginator is better if the query uses ManyToMany associations
81
		 */
82
		return $this->data ?: $this->data_source->fetchAll();
83
	}
84
85
86
	/**
87
	 * Filter data - get one row
88
	 * @param array $condition
89
	 * @return static
90
	 */
91
	public function filterOne(array $condition)
92
	{
93
		/*$p = $this->getPlaceholder();
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
94
95
		foreach ($condition as $column => $value) {
96
			$c = $this->checkAliases($column);
97
98
			$this->data_source->andWhere("$c = ?$p")
99
				->setParameter($p, $value);
100
		}
101
102
		return $this;*/
103
	}
104
105
106
	/**
107
	 * Filter by date
108
	 * @param  Filter\FilterDate $filter
109
	 * @return static
110
	 */
111
	public function applyFilterDate(Filter\FilterDate $filter)
0 ignored issues
show
Unused Code introduced by
The parameter $filter is not used and could be removed.

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

Loading history...
112
	{
113
		/*$p1 = $this->getPlaceholder();
0 ignored issues
show
Unused Code Comprehensibility introduced by
61% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
114
		$p2 = $this->getPlaceholder();
115
116
		foreach ($filter->getCondition() as $column => $value) {
117
			$date = \DateTime::createFromFormat($filter->getPhpFormat(), $value);
118
			$c = $this->checkAliases($column);
119
120
			$this->data_source
121
				->andWhere("$c >= ?$p1")
122
				->andWhere("$c <= ?$p2")
123
				->setParameter($p1, $date->format('Y-m-d 00:00:00'))
124
				->setParameter($p2, $date->format('Y-m-d 23:59:59'));
125
		}
126
127
		return $this;*/
128
	}
129
130
131
	/**
132
	 * Filter by date range
133
	 * @param  Filter\FilterDateRange $filter
134
	 * @return void
135
	 */
136
	public function applyFilterDateRange(Filter\FilterDateRange $filter)
0 ignored issues
show
Unused Code introduced by
The parameter $filter is not used and could be removed.

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

Loading history...
137
	{
138
		/*$conditions = $filter->getCondition();
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
139
		$c = $this->checkAliases($filter->getColumn());
140
141
		$value_from = $conditions[$filter->getColumn()]['from'];
142
		$value_to   = $conditions[$filter->getColumn()]['to'];
143
144
		if ($value_from) {
145
			$date_from = \DateTime::createFromFormat($filter->getPhpFormat(), $value_from);
146
			$date_from->setTime(0, 0, 0);
147
148
			$p = $this->getPlaceholder();
149
150
			$this->data_source->andWhere("$c >= ?$p")->setParameter($p, $date_from->format('Y-m-d H:i:s'));
151
		}
152
153
		if ($value_to) {
154
			$date_to = \DateTime::createFromFormat($filter->getPhpFormat(), $value_to);
155
			$date_to->setTime(23, 59, 59);
156
157
			$p = $this->getPlaceholder();
158
159
			$this->data_source->andWhere("$c <= ?$p")->setParameter($p, $date_to->format('Y-m-d H:i:s'));
160
		}*/
161
	}
162
163
164
	/**
165
	 * Filter by range
166
	 * @param  Filter\FilterRange $filter
167
	 * @return void
168
	 */
169
	public function applyFilterRange(Filter\FilterRange $filter)
0 ignored issues
show
Unused Code introduced by
The parameter $filter is not used and could be removed.

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

Loading history...
170
	{
171
		/*$conditions = $filter->getCondition();
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
172
		$c = $this->checkAliases($filter->getColumn());
173
174
		$value_from = $conditions[$filter->getColumn()]['from'];
175
		$value_to   = $conditions[$filter->getColumn()]['to'];
176
177
		if ($value_from) {
178
			$p = $this->getPlaceholder();
179
			$this->data_source->andWhere("$c >= ?$p")->setParameter($p, $value_from);
180
		}
181
182
		if ($value_to) {
183
			$p = $this->getPlaceholder();
184
			$this->data_source->andWhere("$c <= ?$p")->setParameter($p, $value_to);
185
		}*/
186
	}
187
188
189
	/**
190
	 * Filter by keyword
191
	 * @param  Filter\FilterText $filter
192
	 * @return void
193
	 */
194
	public function applyFilterText(Filter\FilterText $filter)
0 ignored issues
show
Unused Code introduced by
The parameter $filter is not used and could be removed.

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

Loading history...
195
	{
196
		/*$condition = $filter->getCondition();
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
197
		$exprs = [];
198
199
		foreach ($condition as $column => $value) {
200
			$words = explode(' ', $value);
201
			$c = $this->checkAliases($column);
202
203
			foreach ($words as $word) {
204
				$exprs[] = $this->data_source->expr()->like($c, $this->data_source->expr()->literal("%$word%"));
205
			}
206
		}
207
208
		$or = call_user_func_array([$this->data_source->expr(), 'orX'], $exprs);
209
210
		$this->data_source->andWhere($or);*/
211
	}
212
213
214
	/**
215
	 * Filter by multi select value
216
	 * @param  Filter\FilterMultiSelect $filter
217
	 * @return void
218
	 */
219
	public function applyFilterMultiSelect(Filter\FilterMultiSelect $filter)
0 ignored issues
show
Unused Code introduced by
The parameter $filter is not used and could be removed.

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

Loading history...
220
	{
221
		/*$condition = $filter->getCondition();
0 ignored issues
show
Unused Code Comprehensibility introduced by
61% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
222
		$values = $condition[$filter->getColumn()];
223
		$exprs = [];
224
		$c = $this->checkAliases($filter->getColumn());
225
226
		foreach ($values as $value) {
227
			$exprs[] = $this->data_source->expr()->eq($c, $value);
228
		}
229
230
		$or = call_user_func_array([$this->data_source->expr(), 'orX'], $exprs);
231
232
		$this->data_source->andWhere($or);*/
233
	}
234
235
236
	/**
237
	 * Filter by select value
238
	 * @param  Filter\FilterSelect $filter
239
	 * @return void
240
	 */
241
	public function applyFilterSelect(Filter\FilterSelect $filter)
0 ignored issues
show
Unused Code introduced by
The parameter $filter is not used and could be removed.

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

Loading history...
242
	{
243
		/*$p = $this->getPlaceholder();
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
244
245
		foreach ($filter->getCondition() as $column => $value) {
246
			$c = $this->checkAliases($column);
247
248
			$this->data_source->andWhere("$c = ?$p")
249
				->setParameter($p, $value);
250
		}*/
251
	}
252
253
254
	/**
255
	 * Apply limit and offset on data
256
	 * @param int $offset
257
	 * @param int $limit
258
	 * @return static
259
	 */
260
	public function limit($offset, $limit)
261
	{
262
		$this->data_source = $this->data_source->limitBy($limit, $offset);
263
264
		return $this;
265
	}
266
267
268
	/**
269
	 * Sort data
270
	 * @param  Sorting $sorting
271
	 * @return static
272
	 */
273
	public function sort(Sorting $sorting)
274
	{
275
		if (is_callable($sorting->getSortCallback())) {
276
			call_user_func(
277
				$sorting->getSortCallback(),
278
				$this->data_source,
279
				$sorting->getSort()
280
			);
281
282
			return $this;
283
		}
284
285
		$sort = $sorting->getSort();
286
287
		if (!empty($sort)) {
288
			foreach ($sort as $column => $order) {
289
				$this->data_source = $this->data_source->orderBy($column, $order);
290
			}
291
		} else {
292
			/**
293
			 * Has the statement already a order by clause?
294
			 */
295
			$order = $this->data_source->getQueryBuilder()->getClause('order');
296
297
			if (ArraysHelper::testEmpty($order)) {
298
				$this->data_source->orderBy($this->primary_key);
299
			}
300
		}
301
302
		return $this;
303
	}
304
305
306
	/**
307
	 * Get unique int value for each instance class (self)
308
	 * @return int
309
	 */
310
	public function getPlaceholder()
311
	{
312
		$this->placeholder++;
0 ignored issues
show
Bug introduced by
The property placeholder does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
313
314
		return $this->placeholder;
315
	}
316
317
}
318