Passed
Push — 2.6.0 ( ...f22176 )
by steve
18:56
created

DdsDataProvider   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 16
eloc 44
dl 0
loc 137
rs 10
c 0
b 0
f 0
ccs 0
cts 71
cp 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getTotalCount() 0 3 1
A getDdsForm() 0 6 2
A getQueryBuilder() 0 6 2
A prepareTotalCount() 0 13 2
A __clone() 0 5 2
A prepareModels() 0 24 3
A prepareKeys() 0 2 1
A hasDeletedFilter() 0 10 3
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: newicon
5
 * Date: 23/08/2017
6
 * Time: 12:40
7
 */
8
9
namespace neon\core\grid;
10
11
use neon\core\grid\query\DdsQuery;
12
use neon\core\grid\query\IQuery;
13
use \yii\data\BaseDataProvider;
14
15
16
/**
17
 * @property DdsQuery $queryBuilder
18
 *
19
 *
20
 * Class DdsDataProvider
21
 * @package neon\core\grid
22
 */
23
class DdsDataProvider extends BaseDataProvider
24
{
25
	/**
26
	 * The dds class this data provider should represent
27
	 * @var string
28
	 */
29
	public $classType;
30
31
	/**
32
	 * Whether to include deleted records
33
	 * @var bool
34
	 */
35
	public $includeDeleted = false;
36
37
	/**
38
	 * @var DdsQuery
39
	 */
40
	private $_queryBuilder = null;
41
42
	/**
43
	 * On cloning make sure you clone the query builder
44
	 */
45
	public function __clone()
46
	{
47
		parent::__clone();
48
		if ($this->_queryBuilder)
49
			$this->_queryBuilder = clone $this->_queryBuilder;
50
	}
51
52
	/**
53
	 * @return DdsQuery
54
	 */
55
	public function getQueryBuilder()
56
	{
57
		if ($this->_queryBuilder === null) {
58
			$this->_queryBuilder = new DdsQuery();
59
		}
60
		return $this->_queryBuilder;
61
	}
62
63
	/**
64
	 * @inheritDoc
65
	 */
66
	protected function prepareModels()
67
	{
68
		// prepare pagination
69
		if (($pagination = $this->getPagination()) !== false) {
70
			$pagination->totalCount = $this->getTotalCount();
71
			if ($pagination->totalCount === 0) {
72
				return [];
73
			}
74
		}
75
76
		$limit =  [
77
			'start' => $pagination->getOffset(),
78
			'length' => $pagination->getLimit(),
79
			'total' => $pagination->totalCount
80
		];
81
		// get all result rows
82
		$filter = $this->getQueryBuilder()->getFilter();
83
		$order = $this->getQueryBuilder()->getOrder();
84
		$results = neon()->getDds()->IDdsObjectManagement
85
			->runSingleObjectRequest($this->classType, $filter, $order, $limit, $this->hasDeletedFilter());
86
87
		$this->getPagination()->totalCount = $results['total'];
88
89
		return $results['rows'];
90
	}
91
92
	/**
93
	 * @var \neon\core\form\Form
94
	 */
95
	private $_ddsForm;
96
97
	/**
98
	 * Get the DDS form for the class type
99
	 * @return \neon\core\form\Form
100
	 */
101
	public function getDdsForm()
102
	{
103
		if ($this->_ddsForm === null) {
104
			$this->_ddsForm = new \neon\core\form\Form(neon()->phoebe->getFormDefinition('daedalus', $this->classType));
105
		}
106
		return $this->_ddsForm;
107
	}
108
109
	public function getTotalCount()
110
	{
111
		return $this->prepareTotalCount();
112
	}
113
114
	/**
115
	 * @inheritDoc
116
	 */
117
	protected function prepareKeys($models)
118
	{
119
		// TODO: Implement prepareKeys() method.
120
	}
121
122
	/**
123
	 * Store a total count to save multiple recalculations
124
	 * @var type
0 ignored issues
show
Bug introduced by
The type neon\core\grid\type was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
125
	 */
126
	private $_totalCountCache=[];
127
128
	/**
129
	 * @inheritDoc
130
	 */
131
	protected function prepareTotalCount()
132
	{
133
		$filters = $this->queryBuilder->getFilter();
134
		$key = md5(serialize($filters));
135
		if (!isset($this->_totalCountCache[$key])) {
136
			// remove the need for this query!!
137
138
			$order = []; // no need to order for a count
139
			$results = neon()->getDds()->IDdsObjectManagement
140
				->runSingleObjectRequest($this->classType, $filters, $order, ['start'=>0, 'length'=>0, 'total' => true], $this->hasDeletedFilter());
141
			$this->_totalCountCache[$key] = $results['total'];
142
		}
143
		return $this->_totalCountCache[$key];
144
	}
145
146
	/**
147
	 * Checks if the deleted filter exists in the filter query (getQueryBuilder)
148
	 * @return boolean - true if the filtering on deleted rows
149
	 */
150
	public function hasDeletedFilter()
151
	{
152
		$includeDeleted = false;
153
		foreach ($this->queryBuilder->getFilter() as $filter) {
154
			if ($filter[0] === '_deleted') {
155
				$includeDeleted = true;
156
				break;
157
			}
158
		}
159
		return $includeDeleted;
160
	}
161
162
}