Passed
Push — master ( 673916...a119ad )
by
unknown
02:00
created

src/Model/BrokenExternalPageTrackStatus.php (4 issues)

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';
22
23
    private static $db = array(
24
        'Status' => 'Enum("Completed, Running", "Running")',
25
        'JobInfo' => 'Varchar(255)'
26
    );
27
28
    private static $has_many = array(
29
        'TrackedPages' => BrokenExternalPageTrack::class,
30
        'BrokenLinks' => BrokenExternalLink::class
31
    );
32
33
    /**
34
     * Get the latest track status
35
     *
36
     * @return BrokenExternalPageTrackStatus
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
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
     * @return int
77
     */
78
    public function getTotalPages()
79
    {
80
        return $this->TrackedPages()->count();
81
    }
82
83
    /**
84
     * Get completed pages count
85
     *
86
     * @return int
87
     */
88
    public function getCompletedPages()
89
    {
90
        return $this
91
            ->TrackedPages()
92
            ->filter('Processed', 1)
93
            ->count();
94
    }
95
96
    /**
97
     * Returns the latest run, or otherwise creates a new one
98
     *
99
     * @return BrokenExternalPageTrackStatus
100
     */
101
    public static function get_or_create()
102
    {
103
        // Check the current status
104
        $status = self::get_latest();
105
        if ($status && $status->Status == 'Running') {
106
            $status->updateStatus();
107
            return $status;
108
        }
109
110
        return self::create_status();
111
    }
112
113
    /**
114
     * Create and prepare a new status
115
     *
116
     * @return BrokenExternalPageTrackStatus
117
     */
118
    public static function create_status()
119
    {
120
        // If the script is to be started create a new status
121
        $status = self::create();
122
        $status->updateJobInfo('Creating new tracking object');
123
124
        // Setup all pages to test
125
        $pageIDs = Versioned::get_by_stage(SiteTree::class, 'Stage')
126
            ->column('ID');
127
        foreach ($pageIDs as $pageID) {
128
            $trackPage = BrokenExternalPageTrack::create();
129
            $trackPage->PageID = $pageID;
130
            $trackPage->StatusID = $status->ID;
131
            $trackPage->write();
132
        }
133
134
        return $status;
135
    }
136
137
    public function updateJobInfo($message)
138
    {
139
        $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...
140
        $this->write();
141
    }
142
143
    /**
144
     * Self check status
145
     */
146
    public function updateStatus()
147
    {
148
        if ($this->CompletedPages == $this->TotalPages) {
149
            $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...
150
            $this->updateJobInfo('Setting to completed');
151
        }
152
    }
153
}
154