Completed
Pull Request — master (#375)
by Dalibor
02:34
created

DataModel   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 13

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 13
dl 0
loc 143
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 0 52 14
A getDataSource() 0 4 1
B filterData() 0 24 2
A filterRow() 0 4 1
A aggregationRow() 0 16 4
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;
10
11
use Nette;
12
use DibiFluent;
13
use DibiOdbcDriver;
14
use DibiMsSqlDriver;
15
use Nette\Database\Table\Selection;
16
use Doctrine\Common\Collections\Collection;
17
use Doctrine\ORM\QueryBuilder;
18
use Ublaboo\DataGrid\Column\Column;
19
use Ublaboo\DataGrid\DataSource\IDataSource;
20
use Ublaboo\DataGrid\Exception\DataGridWrongDataSourceException;
21
use Ublaboo\DataGrid\Utils\Sorting;
22
use Ublaboo\DataGrid\Utils\NetteDatabaseSelectionHelper;
23
use Nette\Database\Drivers as NDBDrivers;
24
use Ublaboo\DataGrid\DataSource\ApiDataSource;
25
use Nextras\Orm\Collection\ICollection;
26
27
class DataModel
28
{
29
30
	/**
31
	 * @var IDataSource
32
	 */
33
	protected $data_source;
34
35
36
	/**
37
	 * @param IDataSource|array|DibiFluent|Selection|QueryBuilder|Collection $source
38
	 * @param string $primary_key
39
	 */
40
	public function __construct($source, $primary_key)
41
	{
42
		if ($source instanceof IDataSource || $source instanceof ApiDataSource) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
43
			/**
44
			 * Custom user datasource is ready for use
45
			 *
46
			 * $source = $source;
47
			 */
48
49
		} else if (is_array($source)) {
50
			$source = new DataSource\ArrayDataSource($source);
51
52
		} else if ($source instanceof DibiFluent) {
0 ignored issues
show
Bug introduced by
The class DibiFluent does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
53
			$driver = $source->getConnection()->getDriver();
54
55
			if ($driver instanceof DibiOdbcDriver) {
0 ignored issues
show
Bug introduced by
The class DibiOdbcDriver does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
56
				$source = new DataSource\DibiFluentMssqlDataSource($source, $primary_key);
57
58
			} else if ($driver instanceof DibiMsSqlDriver) {
0 ignored issues
show
Bug introduced by
The class DibiMsSqlDriver does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
59
				$source = new DataSource\DibiFluentMssqlDataSource($source, $primary_key);
60
61
			} else {
62
				$source = new DataSource\DibiFluentDataSource($source, $primary_key);
63
			}
64
65
		} else if ($source instanceof Selection) {
66
			$driver = NetteDatabaseSelectionHelper::getDriver($source);
67
68
			if ($driver instanceof NDBDrivers\MsSqlDriver || $driver instanceof NDBDrivers\SqlsrvDriver) {
69
				$source = new DataSource\NetteDatabaseTableMssqlDataSource($source, $primary_key);
70
			} else {
71
				$source = new DataSource\NetteDatabaseTableDataSource($source, $primary_key);
72
			}
73
74
		} else if ($source instanceof QueryBuilder) {
75
			$source = new DataSource\DoctrineDataSource($source, $primary_key);
76
77
		} else if ($source instanceof Collection) {
78
			$source = new DataSource\DoctrineCollectionDataSource($source, $primary_key);
79
80
		} elseif ($source instanceof ICollection) {
81
			$source = new DataSource\NextrasDataSource($source, $primary_key);
82
83
		} else {
84
			throw new DataGridWrongDataSourceException(sprintf(
85
				"DataGrid can not take [%s] as data source.",
86
				is_object($source) ? get_class($source) : 'NULL'
87
			));
88
		}
89
90
		$this->data_source = $source;
91
	}
92
93
94
	/**
95
	 * Return dat asource
96
	 * @return IDataSource
97
	 */
98
	public function getDataSource()
99
	{
100
		return $this->data_source;
101
	}
102
103
104
	/**
105
	 * Filter/paginate/limit/order data source and return reset of data in array
106
	 * @param  Components\DataGridPaginator\DataGridPaginator $paginator_component
107
	 * @param  Sorting                                        $sorting
108
	 * @param  array                                          $filters
109
	 * @return array
110
	 */
111
	public function filterData(
112
		Components\DataGridPaginator\DataGridPaginator $paginator_component = NULL,
113
		Sorting $sorting,
114
		array $filters
115
	) {
116
		$this->data_source->filter($filters);
117
118
		/**
119
		 * Paginator is optional
120
		 */
121
		if ($paginator_component) {
122
			$paginator = $paginator_component->getPaginator();
123
			$paginator->setItemCount($this->data_source->getCount());
124
125
			$this->data_source->sort($sorting)->limit(
126
				$paginator->getOffset(),
127
				$paginator->getItemsPerPage()
128
			);
129
130
			return $this->data_source->getData();
131
		}
132
133
		return $this->data_source->sort($sorting)->getData();
134
	}
135
136
137
	/**
138
	 * Filter one row
139
	 * @param  array $condition
140
	 * @return mixed
141
	 */
142
	public function filterRow(array $condition)
143
	{
144
		return $this->data_source->filterOne($condition)->getData();
145
	}
146
147
148
	/**
149
	 * @param array $columns
150
	 * @return array|bool
151
	 */
152
	public function aggregationRow(array $columns)
153
	{
154
		$hasAggregation = FALSE;
155
		/** @var Column $column */
156
		foreach ($columns as $column) {
157
			if ($column->hasAggregationFunction()) {
158
				$hasAggregation = TRUE;
159
				$aggregationFunction = $column->getAggregationFunction();
160
				$this->data_source->addAggregationColumn($aggregationFunction->getAggregationType(), $aggregationFunction->getColumn());
161
			}
162
		}
163
		if (!$hasAggregation) {
164
			return FALSE;
165
		}
166
		return $this->data_source->getAggregationData();
167
	}
168
169
}
170