Completed
Push — master ( fea220...9daadf )
by Martin
14:08 queued 02:54
created

DataModel   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 15

Test Coverage

Coverage 26.19%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 15
dl 0
loc 142
rs 9.1666
c 0
b 0
f 0
ccs 11
cts 42
cp 0.2619

4 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 0 52 14
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
23
final class DataModel extends Nette\Object
0 ignored issues
show
Deprecated Code introduced by
The class Nette\Object has been deprecated with message: use trait Nette\SmartObject

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
24 1
{
25
26
	/**
27
	 * @var callable[]
28
	 */
29
	public $onBeforeFilter = [];
30
31
	/**
32
	 * @var callable[]
33
	 */
34
	public $onAfterFilter = [];
35
36
	/**
37
	 * @var callable[]
38
	 */
39
	public $onAfterPaginated = [];
40
41
	/**
42
	 * @var IDataSource
43
	 */
44
	private $data_source;
45
46
47
	/**
48
	 * @param IDataSource|array|Dibi\Fluent|Selection|QueryBuilder|Collection $source
49
	 * @param string $primary_key
50
	 */
51
	public function __construct($source, $primary_key)
52
	{
53
		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...
54 1
			/**
55
			 * Custom user datasource is ready for use
56
			 *
57
			 * $source = $source;
58
			 */
59
60
		} else if (is_array($source)) {
61 1
			$source = new DataSource\ArrayDataSource($source);
62 1
63
		} else if ($source instanceof Dibi\Fluent) {
64
			$driver = $source->getConnection()->getDriver();
65
66
			if ($driver instanceof Dibi\Drivers\OdbcDriver) {
67
				$source = new DataSource\DibiFluentMssqlDataSource($source, $primary_key);
0 ignored issues
show
Documentation introduced by
$source is of type object<Dibi\Fluent>, but the function expects a object<DibiFluent>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
68
69
			} else if ($driver instanceof Dibi\Drivers\MsSqlDriver) {
70
				$source = new DataSource\DibiFluentMssqlDataSource($source, $primary_key);
0 ignored issues
show
Documentation introduced by
$source is of type object<Dibi\Fluent>, but the function expects a object<DibiFluent>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71
72
			} else if ($driver instanceof Dibi\Drivers\SqlsrvDriver) {
73
				$source = new DataSource\DibiFluentMssqlDataSource($source, $primary_key);
0 ignored issues
show
Documentation introduced by
$source is of type object<Dibi\Fluent>, but the function expects a object<DibiFluent>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74
75
			} else {
76
				$source = new DataSource\DibiFluentDataSource($source, $primary_key);
0 ignored issues
show
Documentation introduced by
$source is of type object<Dibi\Fluent>, but the function expects a object<DibiFluent>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
77
			}
78
79
		} else if ($source instanceof Selection) {
80
			$driver = NetteDatabaseSelectionHelper::getDriver($source);
81
82
			if ($driver instanceof NDBDrivers\MsSqlDriver || $driver instanceof NDBDrivers\SqlsrvDriver) {
83
				$source = new DataSource\NetteDatabaseTableMssqlDataSource($source, $primary_key);
84
			} else {
85
				$source = new DataSource\NetteDatabaseTableDataSource($source, $primary_key);
86
			}
87
88
		} else if ($source instanceof QueryBuilder) {
89
			$source = new DataSource\DoctrineDataSource($source, $primary_key);
90
91
		} else if ($source instanceof Collection) {
92
			$source = new DataSource\DoctrineCollectionDataSource($source, $primary_key);
93
94
		} else {
95
			throw new DataGridWrongDataSourceException(sprintf(
96
				"DataGrid can not take [%s] as data source.",
97
				is_object($source) ? get_class($source) : 'NULL'
98
			));
99
		}
100
101
		$this->data_source = $source;
102
	}
103
104
105 1
	/**
106 1
	 * Return dat asource
107
	 * @return IDataSource
108
	 */
109
	public function getDataSource()
110
	{
111
		return $this->data_source;
112
	}
113
114
115
	/**
116
	 * Filter/paginate/limit/order data source and return reset of data in array
117
	 * @param  Components\DataGridPaginator\DataGridPaginator $paginator_component
118
	 * @param  Sorting                                        $sorting
119
	 * @param  array                                          $filters
120
	 * @return array
121
	 */
122
	public function filterData(
123
		Components\DataGridPaginator\DataGridPaginator $paginator_component = NULL,
124
		Sorting $sorting,
125
		array $filters
126
	) {
127
		$this->onBeforeFilter($this->data_source);
0 ignored issues
show
Documentation Bug introduced by
The method onBeforeFilter does not exist on object<Ublaboo\DataGrid\DataModel>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
128
129
		$this->data_source->filter($filters);
130
131 1
		$this->onAfterFilter($this->data_source);
0 ignored issues
show
Documentation Bug introduced by
The method onAfterFilter does not exist on object<Ublaboo\DataGrid\DataModel>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
132
133 1
		/**
134
		 * Paginator is optional
135 1
		 */
136
		if ($paginator_component) {
137
			$paginator = $paginator_component->getPaginator();
138
			$paginator->setItemCount($this->data_source->getCount());
139
140 1
			$this->data_source->sort($sorting)->limit(
141
				$paginator->getOffset(),
142
				$paginator->getItemsPerPage()
143
			);
144
145
			$this->onAfterPaginated($this->data_source);
0 ignored issues
show
Documentation Bug introduced by
The method onAfterPaginated does not exist on object<Ublaboo\DataGrid\DataModel>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
146
147
			return $this->data_source->getData();
148
		}
149
150
		return $this->data_source->sort($sorting)->getData();
151
	}
152
153
154 1
	/**
155
	 * Filter one row
156
	 * @param  array $condition
157
	 * @return mixed
158
	 */
159
	public function filterRow(array $condition)
160
	{
161
		return $this->data_source->filterOne($condition)->getData();
162
	}
163
164
}
165