GridRowsExtension   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 92.68%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 0
dl 0
loc 84
ccs 38
cts 41
cp 0.9268
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B SplitDataListIntoGridRows() 0 24 5
A SplitClassNameDataListIntoGridRows() 0 10 1
B createGrid() 0 24 4
1
<?php
2
3
namespace WebOfTalent\GridRows;
4
5
use SilverStripe\Core\Injector\Injector;
6
use SilverStripe\ORM\ArrayList;
7
use SilverStripe\ORM\DataExtension;
8
/**
9
 * Helper methods to lay out DataObjects in a grid of rows and colums to a
10
 * required size.  These methods are intended to be called from a template
11
 */
12
class GridRowsExtension extends DataExtension
13
{
14
    /*
15
    If you are laying out using some form of grid, e.g. HTML table (ugh) or
16
    bootstraps span classes it is useful to have the DataList split by row.
17 3
    Here the DataList is generated from a method accessible to the current
18
    controller
19 3
20 3
    See README.md for a worked example
21
22
    */
23 3
    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...
24 1
    {
25 1
        $methodFound = false;
26 1
        $itemsInGrid = null;
27
28 3
        // Check first the controller and then the model for the method to call
29
        if ($this->owner->hasMethod($itemsInGridMethod)) {
30
            $itemsInGrid = $this->owner->$itemsInGridMethod();
31
            $methodFound = true;
32
        }
33 3
34
        if (!$methodFound && method_exists($this->owner->model, $itemsInGridMethod)) {
35 2
            $itemsInGrid = $this->owner->model->$itemsInGridMethod();
36 2
            $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...
37
        }
38
39 1
        if ($itemsInGrid == null) {
40
            $message = 'Method not found.  A grid cannot be formed from the '
41
                     . 'method ' . $itemsInGridMethod;
42
            throw new \InvalidArgumentException($message);
43
        }
44
45
        return $this->createGrid($itemsInGrid, $numberOfCols);
46
    }
47
48
    /*
49
    If you are laying out using some form of grid, e.g. HTML table (ugh) or
50 1
    bootstraps span classes it is useful to have the DataList split by row.
51
    This is what this method does.
52
53 1
    See USAGE.md for a worked example
54 1
55 1
    */
56
    public function SplitClassNameDataListIntoGridRows(
0 ignored issues
show
Coding Style introduced by
Method name "GridRowsExtension::SplitClassNameDataListIntoGridRows" is not in camel caps format
Loading history...
57
        $className,
58
        $numberOfCols,
59
        $limit = 10,
60
        $sort = 'LastEdited DESC'
61
    ) {
62 2
        $clazz = Injector::inst()->create($className);
63
        $itemsInGrid = $clazz->get()->limit($limit)->sort($sort);
64 2
        return $this->createGrid($itemsInGrid, $numberOfCols);
65 2
    }
66 2
67 2
    /*
68 2
    The actual method that splits the DataList into an ArrayList of rows that
69 2
    contain an ArrayList of Columns
70 2
    */
71 2
    private function createGrid($itemsInGrid, $numberOfCols)
72 2
    {
73 2
        $position = 1;
74 2
        $columns = new ArrayList();
75 2
        $result = new ArrayList();
76 2
        foreach ($itemsInGrid as $key => $item) {
77
            $columns->push($item);
78 2
            if (($position) >= $numberOfCols) {
79 2
                $position = 1;
80 2
                $row = new ArrayList();
81 2
                $row->Columns = $columns;
82 2
                $result->push($row);
83 2
                $columns = new ArrayList();
84 2
            } else {
85
                $position = $position + 1;
86
            }
87 1
        }
88
        if ($columns->Count() > 0) {
89
            $row = new ArrayList();
90
            $row->Columns = $columns;
91
            $result->push($row);
92
        }
93
        return $result;
94
    }
95
}
96