Passed
Pull Request — master (#85)
by Robbie
01:48
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\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...
18
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...
19
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...
20
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...
21
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...
22
23
/**
24
 * A report listing all installed modules used in this site (from a cache).
25
 */
26
class SiteSummary extends Report
27
{
28
    public function title()
29
    {
30
        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

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

89
            'Title' => /** @scrutinizer ignore-call */ _t(__CLASS__ . '.VERSION', 'Version'),
Loading history...
90
            'Version' => $this->resolveCmsVersion(),
91
            'LastUpdated' => $this->getLastUpdated(),
92
        ])->renderWith('SiteSummary_VersionHeader');
93
94
        $config->addComponents(
95
            Injector::inst()->create(GridFieldRefreshButton::class, 'buttons-before-left'),
96
            Injector::inst()->create(
97
                GridFieldLinkButton::class,
98
                'https://addons.silverstripe.org',
99
                _t(__CLASS__ . '.LINK_TO_ADDONS', 'Explore Addons'),
100
                'buttons-before-left'
101
            ),
102
            $this->getDropdownFilter(),
103
            $this->getInfoLink(),
104
            Injector::inst()->create(GridFieldHtmlFragment::class, 'header', $versionHtml)
105
        );
106
107
        // Re-order the paginator to ensure it counts correctly.
108
        $paginator = $config->getComponentByType(GridFieldPaginator::class);
109
        $config->removeComponent($paginator);
110
        $config->addComponent($paginator);
111
112
        return $grid;
113
    }
114
115
    /**
116
     * Returns a dropdown filter with user configurable options in it
117
     *
118
     * @return GridFieldDropdownFilter
119
     */
120
    protected function getDropdownFilter()
121
    {
122
        /** @var GridFieldDropdownFilter $dropdownFilter */
123
        $dropdownFilter = Injector::inst()->create(
124
            GridFieldDropdownFilter::class,
125
            'addonFilter',
126
            'buttons-before-right',
127
            _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

127
            /** @scrutinizer ignore-call */ 
128
            _t(__CLASS__ . '.ShowAllModules', 'Show all modules')
Loading history...
128
        );
129
130
        $dropdownFilter->addFilterOption(
131
            'supported',
132
            _t(__CLASS__ . '.FilterSupported', 'Supported modules'),
133
            ['Supported' => true]
134
        );
135
        $dropdownFilter->addFilterOption(
136
            'unsupported',
137
            _t(__CLASS__ . '.FilterUnsupported', 'Unsupported modules'),
138
            ['Supported' => false]
139
        );
140
141
        $this->extend('updateDropdownFilterOptions', $dropdownFilter);
142
143
        return $dropdownFilter;
144
    }
145
146
    /**
147
     * Returns a link to more information on this module on the addons site
148
     *
149
     * @return GridFieldHtmlFragment
150
     */
151
    protected function getInfoLink()
152
    {
153
        return Injector::inst()->create(
154
            GridFieldHtmlFragment::class,
155
            'buttons-before-right',
156
            DBField::create_field('HTMLFragment', ArrayData::create([
157
                'Link' => 'https://addons.silverstripe.org/add-ons/bringyourownideas/silverstripe-maintenance',
158
                '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

158
                'Label' => /** @scrutinizer ignore-call */ _t(__CLASS__ . '.MORE_INFORMATION', 'More information'),
Loading history...
159
            ])->renderWith(__CLASS__ . '/MoreInformationLink'))
160
        );
161
    }
162
163
    /**
164
     * Extract CMS and Framework version details from the records in the report
165
     *
166
     * @return string
167
     */
168
    protected function resolveCmsVersion()
169
    {
170
        $versionModules = [
171
            'silverstripe/framework' => 'Framework',
172
            'silverstripe/cms' => 'CMS',
173
        ];
174
        $this->extend('updateVersionModules', $versionModules);
175
176
        $records = $this->sourceRecords()->filter('Name', array_keys($versionModules));
177
        $versionParts = [];
178
179
        foreach ($versionModules as $name => $label) {
180
            $record = $records->find('Name', $name);
181
            if (!$record) {
182
                $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

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