addAggregationFunction()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
rs 8.6737
ccs 0
cts 10
cp 0
cc 5
eloc 11
nc 5
nop 2
crap 30
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\AggregationFunction;
10
11
use Ublaboo\DataGrid\DataModel;
12
use Ublaboo\DataGrid\DataSource\IDataSource;
13
use Ublaboo\DataGrid\Exception\DataGridException;
14
15 1
trait TDataGridAggregationFunction
16
{
17
	/**
18
	 * @var IAggregationFunction[]
19
	 */
20
	private $aggregationFunctions = [];
21
22
	/**
23
	 * @var IAggregationFunction|NULL
24
	 */
25
	private $multipleAggregationFunction;
26
27
28
	/**
29
	 * @param  string  $key
30
	 * @param  IAggregationFunction  $aggregationFunction
31
	 * @return static
32
	 * @throws DataGridException
33
	 */
34
	public function addAggregationFunction($key, IAggregationFunction $aggregationFunction)
35
	{
36
		if ($this->hasColumnsSummary()) {
0 ignored issues
show
Bug introduced by
It seems like hasColumnsSummary() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
37
			throw new DataGridException('You can use either ColumnsSummary or AggregationFunctions');
38
		}
39
40
		if (!$this->dataModel instanceof DataModel) {
0 ignored issues
show
Bug introduced by
The property dataModel does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
41
			throw new DataGridException('You have to set a data source first.');
42
		}
43
44
		if (isset($this->aggregationFunctions[$key])) {
45
			throw new DataGridException('There is already a AggregationFunction defined on column ' . $key);
46
		}
47
48
		if ($this->multipleAggregationFunction instanceof MultipleAggregationFunction) {
0 ignored issues
show
Bug introduced by
The class Ublaboo\DataGrid\Aggrega...ipleAggregationFunction 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...
49
			throw new DataGridException('You can not use both AggregationFunctions and MultipleAggregationFunction');
50
		}
51
52
		$this->aggregationFunctions[$key] = $aggregationFunction;
53
54
		return $this;
55
	}
56
57
58
	/**
59
	 * @param  IMultipleAggregationFunction  $multipleAggregationFunction
60
	 * @throws DataGridException
61
	 * @return static
62
	 */
63
	public function setMultipleAggregationFunction(IMultipleAggregationFunction $multipleAggregationFunction)
64
	{
65
		if ($this->hasColumnsSummary()) {
0 ignored issues
show
Bug introduced by
It seems like hasColumnsSummary() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
66
			throw new DataGridException('You can use either ColumnsSummary or AggregationFunctions');
67
		}
68
69
		if (!empty($this->aggregationFunctions)) {
70
			throw new DataGridException('You can not use both AggregationFunctions and MultipleAggregationFunction');
71
		}
72
73
		$this->multipleAggregationFunction = $multipleAggregationFunction;
0 ignored issues
show
Documentation Bug introduced by
It seems like $multipleAggregationFunction of type object<Ublaboo\DataGrid\...pleAggregationFunction> is incompatible with the declared type object<Ublaboo\DataGrid\...gregationFunction>|null of property $multipleAggregationFunction.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
74
75
		return $this;
76
	}
77
78
79
	/**
80
	 * @param  IDataSource  $dataSource
81
	 * @throws DataGridException
82
	 * @return void
83
	 */
84 View Code Duplication
	public function beforeDataModelFilter(IDataSource $dataSource)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
	{
86 1
		if (!$this->hasSomeAggregationFunction()) {
87 1
			return;
88
		}
89
90
		if (!$dataSource instanceof IAggregatable) {
91
			throw new DataGridException('Used DataSource has to implement IAggregatable for aggegations to work');
92
		}
93
94
		if ($this->multipleAggregationFunction) {
95
			if ($this->multipleAggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_ALL) {
96
				$dataSource->processAggregation([$this->multipleAggregationFunction, 'processDataSource']);
97
			}
98
99
			return;
100
		}
101
102
		foreach ($this->aggregationFunctions as $aggregationFunction) {
103
			if ($aggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_ALL) {
104
				$dataSource->processAggregation([$aggregationFunction, 'processDataSource']);
105
			}
106
		}
107
	}
108
109
110
	/**
111
	 * @param  IDataSource  $dataSource
112
	 * @throws DataGridException
113
	 * @return void
114
	 */
115 View Code Duplication
	public function afterDataModelFilter(IDataSource $dataSource)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
	{
117 1
		if (!$this->hasSomeAggregationFunction()) {
118 1
			return;
119
		}
120
121
		if (!$dataSource instanceof IAggregatable) {
122
			throw new DataGridException('Used DataSource has to implement IAggregatable for aggegations to work');
123
		}
124
125
		if ($this->multipleAggregationFunction) {
126
			if ($this->multipleAggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_FILTERED) {
127
				$dataSource->processAggregation([$this->multipleAggregationFunction, 'processDataSource']);
128
			}
129
130
			return;
131
		}
132
133
		foreach ($this->aggregationFunctions as $aggregationFunction) {
134
			if ($aggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_FILTERED) {
135
				$dataSource->processAggregation([$aggregationFunction, 'processDataSource']);
136
			}
137
		}
138
	}
139
140
141
	/**
142
	 * @param  IDataSource  $dataSource
143
	 * @throws DataGridException
144
	 * @return void
145
	 */
146 View Code Duplication
	public function afterDataModelPaginated(IDataSource $dataSource)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
147
	{
148
		if (!$this->hasSomeAggregationFunction()) {
149
			return;
150
		}
151
152
		if (!$dataSource instanceof IAggregatable) {
153
			throw new DataGridException('Used DataSource has to implement IAggregatable for aggegations to work');
154
		}
155
156
		if ($this->multipleAggregationFunction) {
157
			if ($this->multipleAggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_PAGINATED) {
158
				$dataSource->processAggregation([$this->multipleAggregationFunction, 'processDataSource']);
159
			}
160
161
			return;
162
		}
163
164
		foreach ($this->aggregationFunctions as $aggregationFunction) {
165
			if ($aggregationFunction->getFilterDataType() === IAggregationFunction::DATA_TYPE_PAGINATED) {
166
				$dataSource->processAggregation([$aggregationFunction, 'processDataSource']);
167
			}
168
		}
169
	}
170
171
172
	/**
173
	 * @return bool
174
	 */
175
	public function hasSomeAggregationFunction()
176
	{
177 1
		return !empty($this->aggregationFunctions) || $this->multipleAggregationFunction;
178
	}
179
180
181
	/**
182
	 * @return IAggregationFunction[]
183
	 */
184
	public function getAggregationFunctions()
185
	{
186
		return $this->aggregationFunctions;
187
	}
188
189
190
	/**
191
	 * @return MultipleAggregationFunction
192
	 */
193
	public function getMultipleAggregationFunction()
194
	{
195
		return $this->multipleAggregationFunction;
196
	}
197
}
198