BrokenExternalPageTrackStatus   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 51
c 2
b 0
f 0
dl 0
loc 149
rs 10
wmc 15

10 Methods

Rating   Name   Duplication   Size   Complexity  
A updateStatus() 0 5 2
A create_status() 0 17 2
A updateJobInfo() 0 4 1
A get_latest() 0 5 1
A getIncompleteTracks() 0 5 1
A get_or_create() 0 10 3
A provideI18nEntities() 0 8 1
A getCompletedPages() 0 6 1
A getTotalPages() 0 3 1
A getIncompletePageList() 0 8 2
1
<?php
2
3
namespace SilverStripe\ExternalLinks\Model;
4
5
use SilverStripe\CMS\Model\SiteTree;
6
use SilverStripe\i18n\i18nEntityProvider;
7
use SilverStripe\Versioned\Versioned;
8
use SilverStripe\ORM\DataObject;
9
10
/**
11
 * Represents the status of a track run
12
 *
13
 * @method DataList TrackedPages()
14
 * @method DataList BrokenLinks()
15
 * @property int $TotalPages Get total pages count
16
 * @property int $CompletedPages Get completed pages count
17
 */
18
class BrokenExternalPageTrackStatus extends DataObject implements i18nEntityProvider
19
{
20
    private static $table_name = 'BrokenExternalPageTrackStatus';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
21
22
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
23
        'Status' => 'Enum("Completed, Running", "Running")',
24
        'JobInfo' => 'Varchar(255)'
25
    );
26
27
    private static $has_many = array(
0 ignored issues
show
introduced by
The private property $has_many is not used, and could be removed.
Loading history...
28
        'TrackedPages' => BrokenExternalPageTrack::class,
29
        'BrokenLinks' => BrokenExternalLink::class
30
    );
31
32
    /**
33
     * Get the latest track status
34
     *
35
     * @return BrokenExternalPageTrackStatus
36
     */
37
    public static function get_latest()
38
    {
39
        return self::get()
40
            ->sort('ID', 'DESC')
41
            ->first();
42
    }
43
44
    /**
45
     * Returns the list of provided translations for this object
46
     *
47
     * @return array
48
     */
49
    public function provideI18nEntities()
50
    {
51
        return [
52
            __CLASS__ . '.SINGULARNAME' => 'Broken External Page Track Status',
53
            __CLASS__ . '.PLURALNAME' => 'Broken External Page Track Statuses',
54
            __CLASS__ . '.PLURALS' => [
55
              'one' => 'A Broken External Page Track Status',
56
              'other' => '{count} Broken External Page Track Statuses',
57
            ],
58
        ];
59
    }
60
61
    /**
62
     * Gets the list of Pages yet to be checked
63
     *
64
     * @return DataList
0 ignored issues
show
Bug introduced by
The type SilverStripe\ExternalLinks\Model\DataList 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...
65
     */
66
    public function getIncompletePageList()
67
    {
68
        $pageIDs = $this
69
            ->getIncompleteTracks()
70
            ->column('PageID');
71
        if ($pageIDs) {
72
            return Versioned::get_by_stage(SiteTree::class, 'Stage')
0 ignored issues
show
Bug Best Practice introduced by
The expression return SilverStripe\Vers...tage')->byIDs($pageIDs) returns the type SilverStripe\ORM\DataList which is incompatible with the documented return type SilverStripe\ExternalLinks\Model\DataList.
Loading history...
73
            ->byIDs($pageIDs);
74
        }
75
    }
76
77
    /**
78
     * Get the list of incomplete BrokenExternalPageTrack
79
     *
80
     * @return DataList
81
     */
82
    public function getIncompleteTracks()
83
    {
84
        return $this
85
            ->TrackedPages()
86
            ->filter('Processed', 0);
87
    }
88
89
    /**
90
     * Get total pages count
91
     *
92
     * @return int
93
     */
94
    public function getTotalPages()
95
    {
96
        return $this->TrackedPages()->count();
97
    }
98
99
    /**
100
     * Get completed pages count
101
     *
102
     * @return int
103
     */
104
    public function getCompletedPages()
105
    {
106
        return $this
107
            ->TrackedPages()
108
            ->filter('Processed', 1)
109
            ->count();
110
    }
111
112
    /**
113
     * Returns the latest run, or otherwise creates a new one
114
     *
115
     * @return BrokenExternalPageTrackStatus
116
     */
117
    public static function get_or_create()
118
    {
119
        // Check the current status
120
        $status = self::get_latest();
121
        if ($status && $status->Status == 'Running') {
122
            $status->updateStatus();
123
            return $status;
124
        }
125
126
        return self::create_status();
127
    }
128
129
    /**
130
     * Create and prepare a new status
131
     *
132
     * @return BrokenExternalPageTrackStatus
133
     */
134
    public static function create_status()
135
    {
136
        // If the script is to be started create a new status
137
        $status = self::create();
138
        $status->updateJobInfo('Creating new tracking object');
139
140
        // Setup all pages to test
141
        $pageIDs = Versioned::get_by_stage(SiteTree::class, 'Stage')
142
            ->column('ID');
143
        foreach ($pageIDs as $pageID) {
144
            $trackPage = BrokenExternalPageTrack::create();
145
            $trackPage->PageID = $pageID;
146
            $trackPage->StatusID = $status->ID;
147
            $trackPage->write();
148
        }
149
150
        return $status;
151
    }
152
153
    public function updateJobInfo($message)
154
    {
155
        $this->JobInfo = $message;
0 ignored issues
show
Bug Best Practice introduced by
The property JobInfo does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
156
        $this->write();
157
    }
158
159
    /**
160
     * Self check status
161
     */
162
    public function updateStatus()
163
    {
164
        if ($this->CompletedPages == $this->TotalPages) {
165
            $this->Status = 'Completed';
0 ignored issues
show
Bug Best Practice introduced by
The property Status does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
166
            $this->updateJobInfo('Setting to completed');
167
        }
168
    }
169
}
170