Passed
Pull Request — master (#123)
by Robbie
01:32
created

ElementUseReport::getCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
namespace DNADesign\Elemental\Reports\ElementUseReport;
4
5
use DNADesign\Elemental\Models\BaseElement;
6
use SilverStripe\Core\ClassInfo;
7
use SilverStripe\Core\Convert;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\ORM\ArrayList;
10
use SilverStripe\Reports\Report;
11
use SilverStripe\View\ArrayData;
12
13
class ElementUseReport extends Report
14
{
15
    protected $title = 'Content Blocks in Use';
16
17
    protected $description = 'Show what Content Blocks are being used.';
18
19
    public function getCount($params = array())
20
    {
21
        return BaseElement::get()->count();
22
    }
23
24
    public function sourceRecords($params = [])
25
    {
26
        if (empty($params)) {
27
            $classes = ClassInfo::subclassesFor(BaseElement::class);
28
            $output = new ArrayList();
29
30
            foreach ($classes as $class) {
31
                if ($class === BaseElement::class) {
32
                    continue;
33
                }
34
35
                $inst = Injector::inst()->create($class);
36
37
                $output->push(new ArrayData([
38
                    'Icon' => $inst->getIcon(),
39
                    'Title' => $inst->getTypeNice(),
40
                    'ClassName' => $inst->sanitiseClassName($class),
41
                    'Total' => $class::get()->count()
42
                ]));
43
            }
44
45
            return $output;
46
        } elseif (isset($params['ClassName'])) {
47
            $convertClass = Injector::inst()->create(BaseElement::class)->unsanitiseClassName($params['ClassName']);
48
49
            if (ClassInfo::exists($convertClass)) {
50
                return $convertClass::get()->sort('Title', 'ASC');
51
            } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches 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 else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
52
            }
53
        }
54
    }
55
56
    public function columns()
57
    {
58
        $params = $this->params;
0 ignored issues
show
Bug Best Practice introduced by
The property params does not exist on DNADesign\Elemental\Repo...Report\ElementUseReport. Since you implemented __get, consider adding a @property annotation.
Loading history...
59
60
        if (empty($params)) {
61
            return [
62
                'Icon' => [
63
                    'title' => ''
64
                ],
65
                'Title' => [
66
                    'title' => _t(__CLASS__.'.Title', 'Content Type')
67
                ],
68
                'Total' => [
69
                    'title' => _t(__CLASS__.'.Total', 'Total'),
70
                    'link' => function ($value, $item) {
71
                        return sprintf(
72
                            '<a class="grid-field__link" href="%s" title="%s">%s</a>',
73
                            Convert::raw2att($this->getLink('?filters[ClassName]='. $item->ClassName)),
74
                            Convert::raw2att($value),
0 ignored issues
show
Bug introduced by
It seems like SilverStripe\Core\Convert::raw2att($value) can also be of type array; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
                            /** @scrutinizer ignore-type */ Convert::raw2att($value),
Loading history...
75
                            Convert::raw2xml($value)
76
                        );
77
                    }
78
                ]
79
            ];
80
        }
81
82
        return [
83
            'EditorPreview' => [
84
                'title' => ''
85
            ],
86
            'PageTitle' => [
87
                'title' => _t(__CLASS__.'.Page', 'Page'),
88
                'link' => true
89
            ]
90
        ];
91
    }
92
}
93