Completed
Push — master ( b49699...e11dfd )
by Pavel
02:57
created

DataModel   B

Complexity

Total Complexity 19

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Test Coverage

Coverage 25%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 16
dl 0
loc 145
ccs 12
cts 48
cp 0.25
rs 8.4614
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) {
65
			$driver = $source->getConnection()->getDriver();
66
67
			if ($driver instanceof Dibi\Drivers\OdbcDriver) {
68
				$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...
69
70
			} else if ($driver instanceof Dibi\Drivers\MsSqlDriver) {
71
				$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...
72
73
			} else if ($driver instanceof Dibi\Drivers\SqlsrvDriver) {
74
				$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...
75
76
			} else {
77
				$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...
78
			}
79
80
		} else if ($source instanceof Selection) {
81
			$driver = NetteDatabaseSelectionHelper::getDriver($source);
82
83
			if ($driver instanceof NDBDrivers\MsSqlDriver || $driver instanceof NDBDrivers\SqlsrvDriver) {
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) {
90
			$source = new DataSource\DoctrineDataSource($source, $primary_key);
91
92
		} else if ($source instanceof Collection) {
93
			$source = new DataSource\DoctrineCollectionDataSource($source, $primary_key);
94
95
		} elseif ($source instanceof ICollection) {
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);
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...
132
133 1
		$this->data_source->filter($filters);
134
135 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...
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);
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...
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