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

CMSExternalLinksController::getJobStatus()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 0
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\ExternalLinks\Controllers;
4
5
use SilverStripe\Control\HTTP;
6
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus;
7
use SilverStripe\ExternalLinks\Jobs\CheckExternalLinksJob;
8
use SilverStripe\ExternalLinks\Tasks\CheckExternalLinksTask;
9
use SilverStripe\Control\Controller;
10
11
class CMSExternalLinksController extends Controller
12
{
13
14
    private static $allowed_actions = array('getJobStatus', 'start');
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
15
16
    /*
17
	 * Respond to Ajax requests for info on a running job
18
	 *
19
	 * @return string JSON string detailing status of the job
20
	 */
21
    public function getJobStatus()
22
    {
23
        // Set headers
24
        HTTP::set_cache_age(0);
25
        HTTP::add_cache_headers($this->response);
26
        $this->response
27
            ->addHeader('Content-Type', 'application/json')
28
            ->addHeader('Content-Encoding', 'UTF-8')
29
            ->addHeader('X-Content-Type-Options', 'nosniff');
30
31
        // Format status
32
        $track = BrokenExternalPageTrackStatus::get_latest();
33
        if ($track) {
34
            return json_encode(array(
35
            'TrackID' => $track->ID,
36
            'Status' => $track->Status,
37
            'Completed' => $track->getCompletedPages(),
38
            'Total' => $track->getTotalPages()
39
            ));
40
        }
41
    }
42
43
44
    /*
45
	 * Starts a broken external link check
46
	 */
47
    public function start()
48
    {
49
        // return if the a job is already running
50
        $status = BrokenExternalPageTrackStatus::get_latest();
51
        if ($status && $status->Status == 'Running') {
52
            return;
53
        }
54
55
        // Create a new job
56
        if (class_exists('QueuedJobService')) {
57
            // Force the creation of a new run
58
            BrokenExternalPageTrackStatus::create_status();
59
            $checkLinks = new CheckExternalLinksJob();
60
            singleton('QueuedJobService')->queueJob($checkLinks);
61
        } else {
62
            //TODO this hangs as it waits for the connection to be released
63
            // should return back and continue processing
64
            // http://us3.php.net/manual/en/features.connection-handling.php
65
            $task = CheckExternalLinksTask::create();
66
            $task->runLinksCheck();
67
        }
68
    }
69
}
70