PagesWithoutReviewScheduleReport   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 142
Duplicated Lines 25.35 %

Coupling/Cohesion

Components 0
Dependencies 10

Importance

Changes 0
Metric Value
wmc 17
lcom 0
cbo 10
dl 36
loc 142
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A title() 0 4 1
A parameterFields() 0 7 1
B columns() 29 58 7
B sourceRecords() 7 33 4
A hasReviewSchedule() 0 14 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace SilverStripe\ContentReview\Reports;
4
5
use SilverStripe\Forms\CheckboxField;
6
use SilverStripe\Forms\FieldList;
7
use SilverStripe\CMS\Controllers\CMSPageEditController;
8
use SilverStripe\CMS\Model\SiteTree;
9
use SilverStripe\CMS\Model\VirtualPage;
10
use SilverStripe\ContentReview\Compatibility\ContentReviewCompatability;
11
use SilverStripe\Core\ClassInfo;
12
use SilverStripe\ORM\ArrayList;
13
use SilverStripe\ORM\DataObject;
14
use SilverStripe\Reports\Report;
15
use SilverStripe\SiteConfig\SiteConfig;
16
use SilverStripe\Versioned\Versioned;
17
18
/**
19
 * Show all pages that need to be reviewed.
20
 */
21
class PagesWithoutReviewScheduleReport extends Report
22
{
23
    /**
24
     * @return string
25
     */
26
    public function title()
27
    {
28
        return _t("PagesWithoutReviewScheduleReport.TITLE", "Pages without a scheduled review.");
29
    }
30
31
    /**
32
     * @return FieldList
33
     */
34
    public function parameterFields()
35
    {
36
        $params = FieldList::create();
37
        $params->push(CheckboxField::create("ShowVirtualPages", "Show Virtual Pages"));
38
39
        return $params;
40
    }
41
42
    /**
43
     * @return array
44
     */
45
    public function columns()
46
    {
47
        $linkBase = singleton(CMSPageEditController::class)->Link("show");
48
        $linkPath = parse_url($linkBase, PHP_URL_PATH);
49
        $linkQuery = parse_url($linkBase, PHP_URL_QUERY);
50
51
        $fields = array(
52
            "Title"             => array(
53
                "title"      => "Page name",
54
                "formatting" => "<a href='{$linkPath}/\$ID?{$linkQuery}' title='Edit page'>\$value</a>",
55
            ),
56
            "NextReviewDate"    => array(
57
                "title"   => "Review Date",
58
                "casting" => "Date->Full",
59
            ),
60
            "OwnerNames"        => array(
61
                "title" => "Owner",
62
            ),
63
            "LastEditedByName"  => "Last edited by",
64
            "AbsoluteLink"      => array(
65
                "title"      => "URL",
66 View Code Duplication
                "formatting" => function ($value, $item) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
                    $liveLink = $item->AbsoluteLiveLink;
68
                    $stageLink = $item->AbsoluteLink();
69
70
                    return sprintf(
71
                        "%s <a href='%s'>%s</a>",
72
                        $stageLink,
73
                        $liveLink ? $liveLink : $stageLink . "?stage=Stage",
74
                        $liveLink ? "(live)" : "(draft)"
75
                    );
76
                },
77
            ),
78
            "ContentReviewType" => array(
79
                "title"      => "Settings are",
80 View Code Duplication
                "formatting" => function ($value, $item) use ($linkPath, $linkQuery) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
                    if ($item->ContentReviewType == "Inherit") {
82
                        $options = $item->getOptions();
83
                        if ($options && $options instanceof SiteConfig) {
84
                            return "Inherited from <a href='admin/settings'>Settings</a>";
85
                        } elseif ($options) {
86
                            return sprintf(
87
                                "Inherited from <a href='%s/%d?%s'>%s</a>",
88
                                $linkPath,
89
                                $options->ID,
90
                                $linkQuery,
91
                                $options->Title
92
                            );
93
                        }
94
                    }
95
96
                    return $value;
97
                },
98
            ),
99
        );
100
101
        return $fields;
102
    }
103
104
    /**
105
     * @param array $params
106
     *
107
     * @return SS_List
108
     */
109
    public function sourceRecords($params = array())
110
    {
111
        Versioned::set_stage(Versioned::DRAFT);
112
113
        $records = SiteTree::get();
114
        $compatibility = ContentReviewCompatability::start();
115
116
        // If there's no review dates set, default to all pages due for review now.
117
118
        // Show virtual pages?
119 View Code Duplication
        if (empty($params["ShowVirtualPages"])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
            $virtualPageClasses = ClassInfo::subclassesFor(VirtualPage::class);
121
            $records = $records->where(sprintf(
122
                "\"SiteTree\".\"ClassName\" NOT IN ('%s')",
123
                implode("','", array_values($virtualPageClasses))
124
            ));
125
        }
126
127
        $records->sort("ParentID");
128
        $records = $records->toArray();
129
130
        // Trim out calculated values
131
        $list = ArrayList::create();
132
        foreach ($records as $record) {
133
            if (!$this->hasReviewSchedule($record)) {
134
                $list->push($record);
135
            }
136
        }
137
138
        ContentReviewCompatability::done($compatibility);
139
140
        return $list;
141
    }
142
143
    /**
144
     * @param DataObject $record
145
     *
146
     * @return bool
147
     */
148
    protected function hasReviewSchedule(DataObject $record)
149
    {
150
        if (!$record->obj("NextReviewDate")->exists()) {
151
            return false;
152
        }
153
154
        $options = $record->getOptions();
155
156
        if ($options->OwnerGroups()->count() == 0 && $options->OwnerUsers()->count() == 0) {
157
            return false;
158
        }
159
160
        return true;
161
    }
162
}
163