Passed
Push — master ( 8545b7...fd2d3e )
by Robbie
04:18
created

ElementsInUseReport::columns()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 55
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 36
nc 1
nop 0
dl 0
loc 55
rs 8.7217
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace DNADesign\Elemental\Reports;
4
5
use DNADesign\Elemental\Models\BaseElement;
6
use InvalidArgumentException;
7
use Psr\Log\LoggerInterface;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\Forms\GridField\GridField;
10
use SilverStripe\ORM\DataList;
11
use SilverStripe\Reports\Report;
12
use SilverStripe\View\ArrayData;
13
use SilverStripe\View\Requirements;
14
15
class ElementsInUseReport extends Report
16
{
17
    /**
18
     * The string used in GET params to filter the records in this report by element type
19
     *
20
     * @var string
21
     */
22
    const CLASS_NAME_FILTER_KEY = 'ClassName';
23
24
    public function title()
25
    {
26
        return _t(__CLASS__ . '.ReportTitle', 'Content blocks in use');
27
    }
28
29
    public function sourceRecords($params = [])
30
    {
31
        /** @var DataList $elements */
32
        $elements = BaseElement::get()->exclude(['ClassName' => BaseElement::class]);
33
34
        if (isset($params[static::CLASS_NAME_FILTER_KEY])) {
35
            $className = $this->unsanitiseClassName($params[static::CLASS_NAME_FILTER_KEY]);
36
            $elements = $elements->filter(['ClassName' => $className]);
37
        }
38
39
        return $elements;
40
    }
41
42
    public function columns()
43
    {
44
        return [
45
            'Icon' => [
46
                'title' => '',
47
                'formatting' => function ($value, BaseElement $item) {
48
                    return $item->getIcon();
49
                },
50
            ],
51
            'Title' => [
52
                'title' => _t(__CLASS__ . '.Title', 'Title'),
53
                'formatting' => function ($value, BaseElement $item) {
54
                    $value = $item->Title;
55
56
                    if (!empty($value)) {
57
                        if ($link = $item->CMSEditLink()) {
58
                            return $this->getEditLink($value, $link);
59
                        }
60
                        return $value;
61
                    }
62
                    return '<span class="element__note">' . _t(__CLASS__ . '.None', 'None') . '</span>';
63
                },
64
            ],
65
            'ElementSummary' => [
66
                'title' => _t(__CLASS__ . '.Summary', 'Summary'),
67
                'casting' => 'HTMLText->RAW',
68
                'formatting' => function ($value, BaseElement $item) {
69
                    try {
70
                        return $item->getSummary();
71
                    } catch (InvalidArgumentException $exception) {
72
                         // Don't break the report, just continue. Image manipulation is an example which may
73
                         // throw exceptions here.
74
                        Injector::inst()->get(LoggerInterface::class)->debug(
75
                            'Element #' . $item->ID . ' threw exception in ElementInUseReport via getSummary(): '
76
                            . $exception->getMessage()
77
                        );
78
                        return '';
79
                    }
80
                },
81
            ],
82
            'Type' => [
83
                'title' => _t(__CLASS__ . '.Type', 'Type'),
84
                'formatting' => function ($value, BaseElement $item) {
85
                    return $item->getTypeNice();
86
                },
87
            ],
88
            'Page.Title' => [
89
                'title' => _t(__CLASS__ . '.Page', 'Page'),
90
                'formatting' => function ($value, BaseElement $item) {
91
                    if ($value) {
92
                        if ($link = $item->getPage()->CMSEditLink()) {
93
                            return $this->getEditLink($value, $link);
94
                        }
95
                    }
96
                    return $item->getPageTitle();
97
                },
98
            ],
99
        ];
100
    }
101
102
    /**
103
     * Helper method to return the link to edit an element
104
     *
105
     * @param string $value
106
     * @param string $link
107
     * @return string
108
     */
109
    protected function getEditLink($value, $link)
110
    {
111
        return sprintf(
112
            '<a class="grid-field__link" href="%s" title="%s">%s</a>',
113
            $link,
114
            $value,
115
            $value
116
        );
117
    }
118
119
    /**
120
     * Add elemental CSS and a unique class name to the GridField
121
     *
122
     * @return GridField
123
     */
124
    public function getReportField()
125
    {
126
        Requirements::css('dnadesign/silverstripe-elemental:client/dist/styles/bundle.css');
127
128
        /** @var GridField $field */
129
        $field = parent::getReportField();
130
        $field->addExtraClass('elemental-report__grid-field');
131
132
        return $field;
133
    }
134
135
    /**
136
     * When used with silverstripe/reports >= 4.4, this method will automatically be added as breadcrumbs
137
     * leading up to this report.
138
     *
139
     * @return ArrayData[]
140
     */
141
    public function getBreadcrumbs()
142
    {
143
        $params = $this->getSourceParams();
0 ignored issues
show
Bug introduced by Guy Marriott
The method getSourceParams() does not exist on DNADesign\Elemental\Reports\ElementsInUseReport. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

143
        /** @scrutinizer ignore-call */ 
144
        $params = $this->getSourceParams();
Loading history...
144
145
        // Only apply breadcrumbs if a "ClassName" filter is applied. This implies that we came from the
146
        // "element type report".
147
        if (!isset($params[static::CLASS_NAME_FILTER_KEY])) {
148
            return [];
149
        }
150
151
        $report = ElementTypeReport::create();
152
153
        return [ArrayData::create([
154
            'Title' => $report->title(),
155
            'Link' => $report->getLink(),
156
        ])];
157
    }
158
159
    /**
160
     * Unsanitise a model class' name from a URL param
161
     *
162
     * See {@link \SilverStripe\Admin\ModelAdmin::unsanitiseClassName}
163
     *
164
     * @param string $class
165
     * @return string
166
     */
167
    protected function unsanitiseClassName($class)
168
    {
169
        return str_replace('-', '\\', $class);
170
    }
171
}
172