Completed
Push — master ( f97612...da997d )
by Martin
21:53 queued 11:54
created

DataModel   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 25%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 12
dl 0
loc 145
ccs 12
cts 48
cp 0.25
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 0 55 15
A getDataSource() 0 4 1
B filterData() 0 30 2
A filterRow() 0 4 1
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 Dibi;
13
use Nette\Database\Table\Selection;
14
use Doctrine\Common\Collections\Collection;
15
use Doctrine\ORM\QueryBuilder;
16
use Ublaboo\DataGrid\DataSource\IDataSource;
17
use Ublaboo\DataGrid\Exception\DataGridWrongDataSourceException;
18
use Ublaboo\DataGrid\Utils\Sorting;
19
use Ublaboo\DataGrid\Utils\NetteDatabaseSelectionHelper;
20
use Nette\Database\Drivers as NDBDrivers;
21
use Ublaboo\DataGrid\DataSource\ApiDataSource;
22
use Nextras\Orm\Collection\ICollection;
23
24
final class DataModel extends Nette\Object
25 1
{
26
27
	/**
28
	 * @var callable[]
29
	 */
30
	public $onBeforeFilter = [];
31
32
	/**
33
	 * @var callable[]
34
	 */
35
	public $onAfterFilter = [];
36
37
	/**
38
	 * @var callable[]
39
	 */
40
	public $onAfterPaginated = [];
41
42
	/**
43
	 * @var IDataSource
44
	 */
45
	private $data_source;
46
47
48
	/**
49
	 * @param IDataSource|array|Dibi\Fluent|Selection|QueryBuilder|Collection $source
50
	 * @param string $primary_key
51
	 */
52
	public function __construct($source, $primary_key)
53
	{
54 1
		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...
55
			/**
56
			 * Custom user datasource is ready for use
57
			 *
58
			 * $source = $source;
59
			 */
60
61 1
		} else if (is_array($source)) {
62 1
			$source = new DataSource\ArrayDataSource($source);
63
64 1
		} else if ($source instanceof Dibi\Fluent) {
0 ignored issues
show
Bug introduced by
The class Dibi\Fluent does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
65
			$driver = $source->getConnection()->getDriver();
66
67
			if ($driver instanceof Dibi\Drivers\OdbcDriver) {
0 ignored issues
show
Bug introduced by
The class Dibi\Drivers\OdbcDriver does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
68
				$source = new DataSource\DibiFluentMssqlDataSource($source, $primary_key);
69
70
			} else if ($driver instanceof Dibi\Drivers\MsSqlDriver) {
0 ignored issues
show
Bug introduced by
The class Dibi\Drivers\MsSqlDriver does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
71
				$source = new DataSource\DibiFluentMssqlDataSource($source, $primary_key);
72
73
			} else if ($driver instanceof Dibi\Drivers\SqlsrvDriver) {
0 ignored issues
show
Bug introduced by
The class Dibi\Drivers\SqlsrvDriver does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
74
				$source = new DataSource\DibiFluentMssqlDataSource($source, $primary_key);
75
76
			} else {
77
				$source = new DataSource\DibiFluentDataSource($source, $primary_key);
78
			}
79
80
		} else if ($source instanceof Selection) {
0 ignored issues
show
Bug introduced by
The class Nette\Database\Table\Selection does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
81
			$driver = NetteDatabaseSelectionHelper::getDriver($source);
82
83
			if ($driver instanceof NDBDrivers\MsSqlDriver || $driver instanceof NDBDrivers\SqlsrvDriver) {
0 ignored issues
show
Bug introduced by
The class Nette\Database\Drivers\MsSqlDriver does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
Bug introduced by
The class Nette\Database\Drivers\SqlsrvDriver does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
84
				$source = new DataSource\NetteDatabaseTableMssqlDataSource($source, $primary_key);
85
			} else {
86
				$source = new DataSource\NetteDatabaseTableDataSource($source, $primary_key);
87
			}
88
89
		} else if ($source instanceof QueryBuilder) {
0 ignored issues
show
Bug introduced by
The class Doctrine\ORM\QueryBuilder does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
90
			$source = new DataSource\DoctrineDataSource($source, $primary_key);
91
92
		} else if ($source instanceof Collection) {
0 ignored issues
show
Bug introduced by
The class Doctrine\Common\Collections\Collection does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
93
			$source = new DataSource\DoctrineCollectionDataSource($source, $primary_key);
94
95
		} elseif ($source instanceof ICollection) {
0 ignored issues
show
Bug introduced by
The class Nextras\Orm\Collection\ICollection does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
96
			$source = new DataSource\NextrasDataSource($source, $primary_key);
97
98
		} else {
99
			throw new DataGridWrongDataSourceException(sprintf(
100
				"DataGrid can not take [%s] as data source.",
101
				is_object($source) ? get_class($source) : 'NULL'
102
			));
103
		}
104
105 1
		$this->data_source = $source;
106 1
	}
107
108
109
	/**
110
	 * Return dat asource
111
	 * @return IDataSource
112
	 */
113
	public function getDataSource()
114
	{
115
		return $this->data_source;
116
	}
117
118
119
	/**
120
	 * Filter/paginate/limit/order data source and return reset of data in array
121
	 * @param  Components\DataGridPaginator\DataGridPaginator $paginator_component
122
	 * @param  Sorting                                        $sorting
123
	 * @param  array                                          $filters
124
	 * @return array
125
	 */
126
	public function filterData(
127
		Components\DataGridPaginator\DataGridPaginator $paginator_component = NULL,
128
		Sorting $sorting,
129
		array $filters
130
	) {
131 1
		$this->onBeforeFilter($this->data_source);
132
133 1
		$this->data_source->filter($filters);
134
135 1
		$this->onAfterFilter($this->data_source);
136
137
		/**
138
		 * Paginator is optional
139
		 */
140 1
		if ($paginator_component) {
141
			$paginator = $paginator_component->getPaginator();
142
			$paginator->setItemCount($this->data_source->getCount());
143
144
			$this->data_source->sort($sorting)->limit(
145
				$paginator->getOffset(),
146
				$paginator->getItemsPerPage()
147
			);
148
149
			$this->onAfterPaginated($this->data_source);
150
151
			return $this->data_source->getData();
152
		}
153
154 1
		return $this->data_source->sort($sorting)->getData();
155
	}
156
157
158
	/**
159
	 * Filter one row
160
	 * @param  array $condition
161
	 * @return mixed
162
	 */
163
	public function filterRow(array $condition)
164
	{
165
		return $this->data_source->filterOne($condition)->getData();
166
	}
167
168
}
169