FailedURLRewriteReport::SourceRecords()   F
last analyzed

Complexity

Conditions 17
Paths 648

Size

Total Lines 56
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 37
c 0
b 0
f 0
dl 0
loc 56
rs 1.5388
cc 17
nc 648
nop 0

How to fix   Long Method    Complexity   

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 PhpTek\Exodus\Report;
4
5
use PhpTek\Exodus\Model\FailedURLRewriteObject;
6
use PhpTek\Exodus\Model\FailedURLRewriteSummary;
7
use PhpTek\Exodus\Model\StaticSiteImportDataObject;
8
use SilverStripe\Core\Injector\Injectable;
9
use SilverStripe\Reports\Report;
10
use SilverStripe\GraphQL\Controller;
0 ignored issues
show
Bug introduced by
The type SilverStripe\GraphQL\Controller 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...
11
use SilverStripe\ORM\ArrayList;
12
use SilverStripe\ORM\DataObject;
13
use SilverStripe\ORM\FieldType\DBField;
14
use SilverStripe\Forms\FieldList;
15
use SilverStripe\Forms\HeaderField;
16
use SilverStripe\Forms\LiteralField;
17
use SilverStripe\Forms\DropdownField;
18
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
19
use SilverStripe\ORM\FieldType\DBDatetime;
20
21
/**
22
 * A CMS report for URLs that failed to be re-written.
23
 *
24
 * @author Russell Michell <[email protected]>
25
 * @package phptek/silverstripe-exodus
26
 * @see {@link FailedURLRewriteObject}
27
 * @see {@link FailedURLRewriteSummary}
28
 * @see {@link StaticSiteRewriteLinksTask}
29
 */
30
class FailedURLRewriteReport extends Report
31
{
32
    use Injectable;
33
34
    /**
35
     *
36
     * @var string
37
     */
38
    protected $description = <<<'TXT'
39
This report shows a record for each page that contains one or more broken links, left over from the selected import.
40
<br/>You can manually delete a record as you go through and correct the links in each one.
41
TXT;
42
43
    /**
44
     *
45
     * @return string
46
     */
47
    public function title(): string
48
    {
49
        return "Imported links rewrite report";
50
    }
51
52
    /**
53
     *
54
     * @return ArrayList
55
     * @todo refactor this and use another, generic method to deal with repeated (similar) conditionals.
56
     */
57
    public function SourceRecords()
58
    {
59
        $reqVars = Controller::curr()->request->requestVars();
60
        $importID = !empty($reqVars['filters']) ? $reqVars['filters']['ImportID'] : 1;
61
        $list = singleton(FailedURLRewriteObject::class)->getBadImportData($importID);
62
        $_list = ArrayList::create();
63
        $countNotImported = $countJunk = $countThirdParty = $countBadScheme = [];
64
        foreach ($list as $badLink) {
65
            if ($badLink->BadLinkType == 'NotImported') {
66
                // Prevent same page showing in the report and "sum" the totals
67
                if (empty($countNotImported[$badLink->ContainedInID])) {
68
                    $countNotImported[$badLink->ContainedInID] = 1;
69
                } else {
70
                    $countNotImported[$badLink->ContainedInID] += 1;
71
                }
72
                continue;
73
            }
74
            if ($badLink->BadLinkType == 'ThirdParty') {
75
                // Prevent same page showing in the report and "sum" the totals
76
                if (empty($countThirdParty[$badLink->ContainedInID])) {
77
                    $countThirdParty[$badLink->ContainedInID] = 1;
78
                } else {
79
                    $countThirdParty[$badLink->ContainedInID] += 1;
80
                }
81
            }
82
            if ($badLink->BadLinkType == 'BadScheme') {
83
                // Prevent same page showing in the report and "sum" the totals
84
                if (empty($countBadScheme[$badLink->ContainedInID])) {
85
                    $countBadScheme[$badLink->ContainedInID] = 1;
86
                } else {
87
                    $countBadScheme[$badLink->ContainedInID] += 1;
88
                }
89
                continue;
90
            }
91
            if ($badLink->BadLinkType == 'Junk') {
92
                // Prevent same page showing in the report and "sum" the totals
93
                if (empty($countJunk[$badLink->ContainedInID])) {
94
                    $countJunk[$badLink->ContainedInID] = 1;
95
                } else {
96
                    $countJunk[$badLink->ContainedInID] += 1;
97
                }
98
                continue;
99
            }
100
        }
101
102
        foreach ($list as $item) {
103
            // Only push new items if not already in the list
104
            if (!$_list->find('ContainedInID', $item->ContainedInID)) {
105
                $item->ThirdPartyTotal = isset($countThirdParty[$item->ContainedInID]) ? $countThirdParty[$item->ContainedInID] : 0;
106
                $item->BadSchemeTotal = isset($countBadScheme[$item->ContainedInID]) ? $countBadScheme[$item->ContainedInID] : 0;
107
                $item->NotImportedTotal = isset($countNotImported[$item->ContainedInID]) ? $countNotImported[$item->ContainedInID] : 0;
108
                $item->JunkTotal = isset($countJunk[$item->ContainedInID]) ? $countJunk[$item->ContainedInID] : 0;
109
                $_list->push($item);
110
            }
111
        }
112
        return $_list;
113
    }
114
115
    /**
116
     * Get the columns to show with header titles.
117
     *
118
     * @return array
119
     */
120
    public function columns()
121
    {
122
        return [
123
            'Title' => [
124
                'title' => 'Imported page',
125
                'formatting' => function ($value, $item) {
126
                    return sprintf(
127
                        '<a href="admin/pages/edit/show/%s">%s</a>',
128
                        $item->ContainedInID,
129
                        $item->Title()
130
                    );
131
                }
132
            ],
133
            'ThirdPartyTotal' => [
134
                'title' => '# 3rd Party Urls',
135
                'formatting' => '".$ThirdPartyTotal."'
136
            ],
137
            'BadSchemeTotal' => [
138
                'title' => '# Urls w/bad-scheme',
139
                'formatting' => '".$BadSchemeTotal."'
140
            ],
141
            'NotImportedTotal' => [
142
                'title' => '# Unimported Urls',
143
                'formatting' => '".$NotImportedTotal."'
144
            ],
145
            'JunkTotal' => [
146
                'title' => '# Junk Urls',
147
                'formatting' => '".$JunkTotal."'
148
            ],
149
            'Created' => [
150
                'title' => 'Task run date',
151
                'casting' => 'SilverStripe\ORM\FieldType\DBDatetime->Time24'
152
            ],
153
            'Import.Created' => [
154
                'title' => 'Import date',
155
                'casting' => 'SilverStripe\ORM\FieldType\DBDatetime->Time24'
156
            ]
157
        ];
158
    }
159
160
    /**
161
     * Get link-rewrite summary for display at the top of the report.
162
     * The data itself comes from a DataList of FailedURLRewriteObject's.
163
     *
164
     * @param number $importID
165
     * @return null | string
166
     */
167
    protected function getSummary($importID)
168
    {
169
        if (!$text = DataObject::get_one(FailedURLRewriteSummary::class, "\"ImportID\" = '$importID'")) {
170
            return;
171
        }
172
173
        $lines = explode(PHP_EOL, $text->Text);
0 ignored issues
show
Bug introduced by
It seems like $text->Text can also be of type null; however, parameter $string of explode() 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

173
        $lines = explode(PHP_EOL, /** @scrutinizer ignore-type */ $text->Text);
Loading history...
174
        $summaryData = '';
175
176
        foreach ($lines as $line) {
177
            $summaryData .= $line . '<br/>';
178
        }
179
180
        return $summaryData;
181
    }
182
183
    /**
184
     * Show a basic form that allows users to filter link-rewrite data according to
185
     * a specific import propogate via query-string.
186
     *
187
     * @return FieldList
188
     */
189
    public function parameterFields()
190
    {
191
        $fields = FieldList::create();
192
        $reqVars = Controller::curr()->request->requestVars();
193
        $importID = !empty($reqVars['filters']) ? $reqVars['filters']['ImportID'] : 1;
194
195
        if ($summary = $this->getSummary($importID)) {
196
            $fields->push(HeaderField::create('SummaryHead', 'Summary', 4));
197
            $fields->push(LiteralField::create('SummaryBody', $summary));
198
        }
199
200
        $source = DataObject::get(StaticSiteImportDataObject::class);
201
        $_source = [];
202
        foreach ($source as $import) {
203
            $date = DBField::create_field(DBDatetime::class, $import->Created)->Time24();
204
            $_source[$import->ID] = $date . ' (Import #' . $import->ID . ')';
205
        }
206
207
        $importDropdown = LiteralField::create('ImportID', '<p>No imports found.</p>');
208
        if ($_source) {
209
            $importDropdown = DropdownField::create('ImportID', 'Import selection', $_source);
210
        }
211
212
        $fields->push($importDropdown);
213
        return $fields;
214
    }
215
216
    /**
217
     * Overrides SSReport::getReportField() with the addition of GridField Actions.
218
     *
219
     * @return GridField
0 ignored issues
show
Bug introduced by
The type PhpTek\Exodus\Report\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...
220
     */
221
    public function getReportField()
222
    {
223
        $gridField = parent::getReportField();
224
        $gridField->setModelClass(FailedURLRewriteObject::class);
225
        $config = $gridField->getConfig();
226
        $config->addComponent(new GridFieldDeleteAction());
227
228
        return $gridField;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $gridField returns the type SilverStripe\Forms\GridField\GridField which is incompatible with the documented return type PhpTek\Exodus\Report\GridField.
Loading history...
229
    }
230
}
231