Completed
Push — master ( 9ea506...789ce9 )
by Robbie
13s
created

CMSExternalLinksController::getJobStatus()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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