Completed
Push — master ( 241a00...e1f50e )
by Gordon
18:01 queued 13s
created

GridRowsExtension::SplitDataListIntoGridRows()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 25
rs 8.439
cc 5
eloc 14
nc 8
nop 2
1
<?php
2
/**
3
 * Helper methods to lay out DataObjects in a grid of rows and colums to a
4
 * required size.  These methods are intended to be called from a template
5
 */
6
class GridRowsExtension extends DataExtension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
	/*
9
	If you are laying out using some form of grid, e.g. HTML table (ugh) or
10
	bootstraps span classes it is useful to have the DataList split by row.
11
	Here the DataList is generated from a method accessible to the current
12
	controller
13
14
	See README.md for a worked example
15
16
	*/
17
	public function SplitDataListIntoGridRows($itemsInGridMethod, $numberOfCols)
0 ignored issues
show
Coding Style introduced by
Method name "GridRowsExtension::SplitDataListIntoGridRows" is not in camel caps format
Loading history...
18
	{
19
		$methodFound = false;
20
21
		$itemsInGrid = null;
22
23
		// Check first the controller and then the model for the method to call
24
		if ($this->owner->hasMethod($itemsInGridMethod)) {
25
			$itemsInGrid = $this->owner->$itemsInGridMethod();
26
			$methodFound = true;
27
		}
28
29
		if (!$methodFound && $this->owner->model->hasMethod($itemsInGridMethod)) {
30
			$itemsInGrid = $this->owner->model->$itemsInGridMethod();
31
			$methodFound = true;
0 ignored issues
show
Unused Code introduced by
$methodFound is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
32
		}
33
34
		if ($itemsInGrid == null) {
35
			$message = 'Method not found.  A grid cannot be formed from the '
36
					 . 'method ' . $itemsInGridMethod;
37
			throw new InvalidArgumentException($message);
38
		}
39
40
		return $this->createGrid($itemsInGrid, $numberOfCols);
41
	}
42
43
	/*
44
	If you are laying out using some form of grid, e.g. HTML table (ugh) or
45
	bootstraps span classes it is useful to have the DataList split by row.
46
	This is what this method does.
47
48
	See README.md for a worked example
49
50
	*/
51
	public function SplitClassNameDataListIntoGridRows(
0 ignored issues
show
Coding Style introduced by
Method name "GridRowsExtension::SplitClassNameDataListIntoGridRows" is not in camel caps format
Loading history...
52
		$className, $numberOfCols, $limit, $sort = 'LastEdited DESC')
0 ignored issues
show
Coding Style introduced by
Multi-line function declarations must define one parameter per line
Loading history...
Coding Style introduced by
The closing parenthesis of a multi-line function declaration must be on a new line
Loading history...
Coding Style introduced by
There must be a single space between the closing parenthesis and the opening brace of a multi-line function declaration; found newline
Loading history...
53
	{
54
		$clazz = Injector::inst()->create($className);
55
		$itemsInGrid = $clazz->get()->limit($limit)->sort($sort);
56
		return $this->createGrid($itemsInGrid, $numberOfCols);
57
	}
58
59
	/*
60
	The actual method that splits the DataList into an ArrayList of rows that
61
	contain an ArrayList of Columns
62
	*/
63
	private function createGrid($itemsInGrid, $numberOfCols)
64
	{
65
		$position = 1;
66
		$columns = new ArrayList();
67
		$result = new ArrayList();
68
		foreach ($itemsInGrid as $key => $item) {
69
			$columns->push($item);
70
			if (($position) >= $numberOfCols) {
71
				$position = 1;
72
				$row = new ArrayList();
73
				$row->Columns = $columns;
74
				$result->push($row);
75
				$columns = new ArrayList();
76
			} else {
77
				$position = $position + 1;
78
			}
79
		}
80
		if ($columns->Count() > 0) {
81
			$row = new ArrayList();
82
			$row->Columns = $columns;
83
			$result->push($row);
84
		}
85
		return $result;
86
	}
87
}
88