DataModel::__construct()   C
last analyzed

Complexity

Conditions 16
Paths 13

Size

Total Lines 58
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 164.1303

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 58
rs 6.4446
ccs 5
cts 30
cp 0.1666
cc 16
eloc 33
nc 13
nop 2
crap 164.1303

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Dibi\Fluent;
12
use Doctrine\Common\Collections\Collection;
13
use Doctrine\ORM\QueryBuilder;
14
use Nette\SmartObject;
15
use Nette\Database\Drivers as NDBDrivers;
16
use Nette\Database\Table\Selection;
17
use Nextras\Orm\Collection\ICollection;
18
use Ublaboo\DataGrid\DataSource\ApiDataSource;
19
use Ublaboo\DataGrid\DataSource\IDataSource;
20
use Ublaboo\DataGrid\Exception\DataGridWrongDataSourceException;
21
use Ublaboo\DataGrid\Utils\NetteDatabaseSelectionHelper;
22
use Ublaboo\DataGrid\Utils\Sorting;
23
24 1
final class DataModel
25
{
26
27 1
	use SmartObject;
28
29
	/**
30
	 * @var callable[]
31
	 */
32
	public $onBeforeFilter = [];
33
34
	/**
35
	 * @var callable[]
36
	 */
37
	public $onAfterFilter = [];
38
39
	/**
40
	 * @var callable[]
41
	 */
42
	public $onAfterPaginated = [];
43
44
	/**
45
	 * @var IDataSource
46
	 */
47
	private $data_source;
48
49
50
	/**
51
	 * @param IDataSource|array|Fluent|Selection|QueryBuilder|Collection $source
52
	 * @param string $primary_key
53
	 */
54
	public function __construct($source, $primary_key)
55
	{
56 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...
57
			/**
58
			 * Custom user datasource is ready for use
59
			 *
60
			 * $source = $source;
61
			 */
62
63 1
		} elseif (is_array($source)) {
64 1
			$source = new DataSource\ArrayDataSource($source);
65
66
		} elseif ($source instanceof 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...
67
			$driver = $source->getConnection()->getDriver();
68
69
			if ($driver instanceof Dibi\Drivers\OdbcDriver) {
0 ignored issues
show
Bug introduced by
The class Ublaboo\DataGrid\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...
70
				$source = new DataSource\DibiFluentMssqlDataSource($source, $primary_key);
71
72
			} elseif ($driver instanceof Dibi\Drivers\MsSqlDriver) {
0 ignored issues
show
Bug introduced by
The class Ublaboo\DataGrid\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...
73
				$source = new DataSource\DibiFluentMssqlDataSource($source, $primary_key);
74
75
			} elseif ($driver instanceof Dibi\Drivers\PostgreDriver) {
0 ignored issues
show
Bug introduced by
The class Ublaboo\DataGrid\Dibi\Drivers\PostgreDriver 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...
76
				$source = new DataSource\DibiFluentPostgreDataSource($source, $primary_key);
77
78
			} elseif ($driver instanceof Dibi\Drivers\SqlsrvDriver) {
0 ignored issues
show
Bug introduced by
The class Ublaboo\DataGrid\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...
79
				$source = new DataSource\DibiFluentMssqlDataSource($source, $primary_key);
80
81
			} else {
82
				$source = new DataSource\DibiFluentDataSource($source, $primary_key);
83
			}
84
85
		} elseif ($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...
86
			$driver = NetteDatabaseSelectionHelper::getDriver($source);
87
88
			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...
89
				$source = new DataSource\NetteDatabaseTableMssqlDataSource($source, $primary_key);
90
			} else {
91
				$source = new DataSource\NetteDatabaseTableDataSource($source, $primary_key);
92
			}
93
94
		} elseif ($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...
95
			$source = new DataSource\DoctrineDataSource($source, $primary_key);
96
97
		} elseif ($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...
98
			$source = new DataSource\DoctrineCollectionDataSource($source, $primary_key);
99
100
		} 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...
101
			$source = new DataSource\NextrasDataSource($source, $primary_key);
102
103
		} else {
104
			throw new DataGridWrongDataSourceException(sprintf(
105
				'DataGrid can not take [%s] as data source.',
106
				is_object($source) ? get_class($source) : 'NULL'
107
			));
108
		}
109
110 1
		$this->data_source = $source;
111 1
	}
112
113
114
	/**
115
	 * Return dat asource
116
	 * @return IDataSource
117
	 */
118
	public function getDataSource()
119
	{
120
		return $this->data_source;
121
	}
122
123
124
	/**
125
	 * Filter/paginate/limit/order data source and return reset of data in array
126
	 * @param  Components\DataGridPaginator\DataGridPaginator $paginator_component
127
	 * @param  Sorting                                        $sorting
128
	 * @param  array                                          $filters
129
	 * @return array
130
	 */
131
	public function filterData(
132
		Components\DataGridPaginator\DataGridPaginator $paginator_component = null,
133
		Sorting $sorting,
134
		array $filters
135
	) {
136 1
		$this->onBeforeFilter($this->data_source);
137
138 1
		$this->data_source->filter($filters);
139
140 1
		$this->onAfterFilter($this->data_source);
141
142
		/**
143
		 * Paginator is optional
144
		 */
145 1
		if ($paginator_component) {
146
			$paginator = $paginator_component->getPaginator();
147
			$paginator->setItemCount($this->data_source->getCount());
148
149
			$this->data_source->sort($sorting)->limit(
150
				$paginator->getOffset(),
151
				$paginator->getItemsPerPage()
152
			);
153
154
			$this->onAfterPaginated($this->data_source);
155
156
			return $this->data_source->getData();
157
		}
158
159 1
		return $this->data_source->sort($sorting)->getData();
160
	}
161
162
163
	/**
164
	 * Filter one row
165
	 * @param  array $condition
166
	 * @return mixed
167
	 */
168
	public function filterRow(array $condition)
169
	{
170
		$this->onBeforeFilter($this->data_source);
171
		$this->onAfterFilter($this->data_source);
172
173
		return $this->data_source->filterOne($condition)->getData();
174
	}
175
}
176