Completed
Pull Request — master (#27)
by
unknown
03:13
created

BrokenExternalPageTrackStatus::updateJobInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\ExternalLinks\Model;
4
5
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrack;
6
use SilverStripe\ExternalLinks\Model\BrokenExternalLink;
7
use SilverStripe\CMS\Model\SiteTree;
8
use SilverStripe\Versioned\Versioned;
9
use SilverStripe\ORM\DataObject;
10
11
/**
12
 * Represents the status of a track run
13
 *
14
 * @method DataList TrackedPages()
15
 * @method DataList BrokenLinks()
16
 * @property int $TotalPages Get total pages count
17
 * @property int $CompletedPages Get completed pages count
18
 */
19
class BrokenExternalPageTrackStatus extends DataObject
20
{
21
    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...
22
23
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
24
        'Status' => 'Enum("Completed, Running", "Running")',
25
        'JobInfo' => 'Varchar(255)'
26
    );
27
28
    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...
29
        'TrackedPages' => BrokenExternalPageTrack::class,
30
        'BrokenLinks' => BrokenExternalLink::class
31
    );
32
33
    /**
34
     * Get the latest track status
35
     *
36
     * @return self
37
     */
38
    public static function get_latest()
39
    {
40
        return self::get()
41
            ->sort('ID', 'DESC')
42
            ->first();
43
    }
44
45
    /**
46
     * Gets the list of Pages yet to be checked
47
     *
48
     * @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...
49
     */
50
    public function getIncompletePageList()
51
    {
52
        $pageIDs = $this
53
            ->getIncompleteTracks()
54
            ->column('PageID');
55
        if ($pageIDs) {
56
            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...
57
            ->byIDs($pageIDs);
58
        }
59
    }
60
61
    /**
62
     * Get the list of incomplete BrokenExternalPageTrack
63
     *
64
     * @return DataList
65
     */
66
    public function getIncompleteTracks()
67
    {
68
        return $this
69
            ->TrackedPages()
70
            ->filter('Processed', 0);
71
    }
72
73
    /**
74
     * Get total pages count
75
     */
76
    public function getTotalPages()
77
    {
78
        return $this->TrackedPages()->count();
79
    }
80
81
    /**
82
     * Get completed pages count
83
     */
84
    public function getCompletedPages()
85
    {
86
        return $this
87
            ->TrackedPages()
88
            ->filter('Processed', 1)
89
            ->count();
90
    }
91
92
    /**
93
     * Returns the latest run, or otherwise creates a new one
94
     *
95
     * @return self
96
     */
97
    public static function get_or_create()
98
    {
99
        // Check the current status
100
        $status = self::get_latest();
101
        if ($status && $status->Status == 'Running') {
102
            $status->updateStatus();
103
            return $status;
104
        }
105
106
        return self::create_status();
107
    }
108
109
    /*
110
	 * Create and prepare a new status
111
	 *
112
	 * @return self
113
	 */
114
    public static function create_status()
115
    {
116
        // If the script is to be started create a new status
117
        $status = self::create();
118
        $status->updateJobInfo('Creating new tracking object');
119
120
        // Setup all pages to test
121
        $pageIDs = Versioned::get_by_stage(SiteTree::class, 'Stage')
122
            ->column('ID');
123
        foreach ($pageIDs as $pageID) {
124
            $trackPage = BrokenExternalPageTrack::create();
125
            $trackPage->PageID = $pageID;
126
            $trackPage->StatusID = $status->ID;
127
            $trackPage->write();
128
        }
129
130
        return $status;
131
    }
132
133
    public function updateJobInfo($message)
134
    {
135
        $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...
136
        $this->write();
137
    }
138
139
    /**
140
     * Self check status
141
     */
142
    public function updateStatus()
143
    {
144
        if ($this->CompletedPages == $this->TotalPages) {
145
            $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...
146
            $this->updateJobInfo('Setting to completed');
147
        }
148
    }
149
}
150