Passed
Pull Request — master (#85)
by Robbie
02:04
created

SiteSummary::getLastUpdated()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace BringYourOwnIdeas\Maintenance\Reports;
4
5
use BringYourOwnIdeas\Maintenance\Forms\GridFieldDropdownFilter;
6
use BringYourOwnIdeas\Maintenance\Forms\GridFieldHtmlFragment;
7
use BringYourOwnIdeas\Maintenance\Forms\GridFieldLinkButton;
8
use BringYourOwnIdeas\Maintenance\Forms\GridFieldRefreshButton;
9
use BringYourOwnIdeas\Maintenance\Model\Package;
10
use BringYourOwnIdeas\Maintenance\Tasks\UpdatePackageInfoTask;
11
use SilverStripe\Core\Config\Config;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Core\Config\Config was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use SilverStripe\Core\Injector\Injector;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Core\Injector\Injector was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use SilverStripe\Forms\GridField\GridField;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Forms\GridField\GridField was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use SilverStripe\Forms\GridField\GridFieldConfig;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Forms\GridField\GridFieldConfig was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use SilverStripe\Forms\GridField\GridFieldExportButton;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Forms\GridField\GridFieldExportButton was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use SilverStripe\Forms\GridField\GridFieldPaginator;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Forms\GridField\GridFieldPaginator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use SilverStripe\Forms\GridField\GridFieldPrintButton;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Forms\GridField\GridFieldPrintButton was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use SilverStripe\ORM\FieldType\DBDatetime;
0 ignored issues
show
Bug introduced by
The type SilverStripe\ORM\FieldType\DBDatetime was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use SilverStripe\ORM\FieldType\DBField;
0 ignored issues
show
Bug introduced by
The type SilverStripe\ORM\FieldType\DBField was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use SilverStripe\Reports\Report;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Reports\Report was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use SilverStripe\View\ArrayData;
0 ignored issues
show
Bug introduced by
The type SilverStripe\View\ArrayData was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use SilverStripe\View\Requirements;
0 ignored issues
show
Bug introduced by
The type SilverStripe\View\Requirements was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
24
/**
25
 * A report listing all installed modules used in this site (from a cache).
26
 */
27
class SiteSummary extends Report
28
{
29
    public function title()
30
    {
31
        return _t(__CLASS__ . '.TITLE', 'Installed modules');
0 ignored issues
show
Bug introduced by
The function _t was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

31
        return /** @scrutinizer ignore-call */ _t(__CLASS__ . '.TITLE', 'Installed modules');
Loading history...
32
    }
33
34
    public function sourceRecords()
35
    {
36
        $packageList = Package::get();
37
        $typeFilters = Config::inst()->get(UpdatePackageInfoTask::class, 'allowed_types');
38
39
        if (!empty($typeFilters)) {
40
            $packageList = $packageList->filter('Type', $typeFilters);
41
        }
42
43
        $this->extend('updateSourceRecords', $packageList);
44
45
        return $packageList;
46
    }
47
48
    /**
49
     * Provide column selection and formatting rules for the CMS report. You can extend data columns by extending
50
     * {@link Package::summary_fields}, or you can extend this method to adjust the formatting rules, or to provide
51
     * composite fields (such as Summary below) for the CMS report but not the CSV export.
52
     *
53
     * {@inheritDoc}
54
     */
55
    public function columns()
56
    {
57
        $columns = Package::create()->summaryFields();
58
59
        // Remove the default Title and Description and create Summary as a composite of both for the CMS report only
60
        unset($columns['Title'], $columns['Description']);
61
        $columns = ['Summary' => 'Summary'] + $columns;
62
63
        $this->extend('updateColumns', $columns);
64
65
        return $columns;
66
    }
67
68
    /**
69
     * Add a button row, including link out to the SilverStripe addons repository, and export button
70
     *
71
     * {@inheritdoc}
72
     */
73
    public function getReportField()
74
    {
75
        Requirements::css('bringyourownideas/silverstripe-maintenance: client/dist/styles/bundle.css');
76
77
        /** @var GridField $grid */
78
        $grid = parent::getReportField();
79
80
        /** @var GridFieldConfig $config */
81
        $config = $grid->getConfig();
82
83
        $grid->addExtraClass('package-summary');
84
85
        /** @var GridFieldExportButton $exportButton */
86
        $exportButton = $config->getComponentByType(GridFieldExportButton::class);
87
        $exportButton->setExportColumns(Package::create()->summaryFields());
88
89
        $versionHtml = ArrayData::create([
90
            'Title' => _t(__CLASS__ . '.VERSION', 'Version'),
0 ignored issues
show
Bug introduced by
The function _t was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

90
            'Title' => /** @scrutinizer ignore-call */ _t(__CLASS__ . '.VERSION', 'Version'),
Loading history...
91
            'Version' => $this->resolveCmsVersion(),
92
            'LastUpdated' => $this->getLastUpdated(),
93
        ])->renderWith('SiteSummary_VersionHeader');
94
95
        $config->addComponents(
96
            Injector::inst()->create(GridFieldRefreshButton::class, 'buttons-before-left'),
97
            Injector::inst()->create(
98
                GridFieldLinkButton::class,
99
                'https://addons.silverstripe.org',
100
                _t(__CLASS__ . '.LINK_TO_ADDONS', 'Explore Addons'),
101
                'buttons-before-left'
102
            ),
103
            $this->getDropdownFilter(),
104
            $this->getInfoLink(),
105
            Injector::inst()->create(GridFieldHtmlFragment::class, 'header', $versionHtml)
106
        );
107
108
        // Re-order the paginator to ensure it counts correctly, and reorder the buttons
109
        $paginator = $config->getComponentByType(GridFieldPaginator::class);
110
        $config->removeComponent($paginator)->addComponent($paginator);
111
112
        $exportButton = $config->getComponentByType(GridFieldExportButton::class);
113
        $config->removeComponent($exportButton)->addComponent($exportButton);
114
115
        $printButton = $config->getComponentByType(GridFieldPrintButton::class);
116
        $config->removeComponentsByType($printButton)->addComponent($printButton);
117
118
        return $grid;
119
    }
120
121
    /**
122
     * Returns a dropdown filter with user configurable options in it
123
     *
124
     * @return GridFieldDropdownFilter
125
     */
126
    protected function getDropdownFilter()
127
    {
128
        /** @var GridFieldDropdownFilter $dropdownFilter */
129
        $dropdownFilter = Injector::inst()->create(
130
            GridFieldDropdownFilter::class,
131
            'addonFilter',
132
            'buttons-before-right',
133
            _t(__CLASS__ . '.ShowAllModules', 'Show all modules')
0 ignored issues
show
Bug introduced by
The function _t was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

133
            /** @scrutinizer ignore-call */ 
134
            _t(__CLASS__ . '.ShowAllModules', 'Show all modules')
Loading history...
134
        );
135
136
        $dropdownFilter->addFilterOption(
137
            'supported',
138
            _t(__CLASS__ . '.FilterSupported', 'Supported modules'),
139
            ['Supported' => true]
140
        );
141
        $dropdownFilter->addFilterOption(
142
            'unsupported',
143
            _t(__CLASS__ . '.FilterUnsupported', 'Unsupported modules'),
144
            ['Supported' => false]
145
        );
146
147
        $this->extend('updateDropdownFilterOptions', $dropdownFilter);
148
149
        return $dropdownFilter;
150
    }
151
152
    /**
153
     * Returns a link to more information on this module on the addons site
154
     *
155
     * @return GridFieldHtmlFragment
156
     */
157
    protected function getInfoLink()
158
    {
159
        return Injector::inst()->create(
160
            GridFieldHtmlFragment::class,
161
            'buttons-before-right',
162
            DBField::create_field('HTMLFragment', ArrayData::create([
163
                'Link' => 'https://addons.silverstripe.org/add-ons/bringyourownideas/silverstripe-maintenance',
164
                'Label' => _t(__CLASS__ . '.MORE_INFORMATION', 'More information'),
0 ignored issues
show
Bug introduced by
The function _t was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

164
                'Label' => /** @scrutinizer ignore-call */ _t(__CLASS__ . '.MORE_INFORMATION', 'More information'),
Loading history...
165
            ])->renderWith(__CLASS__ . '/MoreInformationLink'))
166
        );
167
    }
168
169
    /**
170
     * Extract CMS and Framework version details from the records in the report
171
     *
172
     * @return string
173
     */
174
    protected function resolveCmsVersion()
175
    {
176
        $versionModules = [
177
            'silverstripe/framework' => 'Framework',
178
            'silverstripe/cms' => 'CMS',
179
        ];
180
        $this->extend('updateVersionModules', $versionModules);
181
182
        $records = $this->sourceRecords()->filter('Name', array_keys($versionModules));
183
        $versionParts = [];
184
185
        foreach ($versionModules as $name => $label) {
186
            $record = $records->find('Name', $name);
187
            if (!$record) {
188
                $version = _t(__CLASS__.'.VersionUnknown', 'Unknown');
0 ignored issues
show
Bug introduced by
The function _t was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

188
                $version = /** @scrutinizer ignore-call */ _t(__CLASS__.'.VersionUnknown', 'Unknown');
Loading history...
189
            } else {
190
                $version = $record->Version;
191
            }
192
193
            $versionParts[] = "$label $version";
194
        }
195
196
        return implode(', ', $versionParts);
197
    }
198
199
    /**
200
     * Get the "last updated" date for the report. This is based on the modified date of any of the records, since
201
     * they are regenerated when the report is generated.
202
     *
203
     * @return string
204
     */
205
    public function getLastUpdated()
206
    {
207
        $packages = Package::get()->limit(1);
208
        if (!$packages->count()) {
209
            return '';
210
        }
211
        /** @var DBDatetime $datetime */
212
        $datetime = $packages->first()->dbObject('LastEdited');
213
        return $datetime->Date() . ' ' . $datetime->Time12();
214
    }
215
}
216