CMSExternalLinksController::getJobStatus()   A
last analyzed

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.8333
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\ExternalLinks\Controllers;
4
5
use SilverStripe\Control\HTTP;
6
use SilverStripe\Core\Convert;
7
use SilverStripe\ExternalLinks\Model\BrokenExternalPageTrackStatus;
8
use SilverStripe\ExternalLinks\Jobs\CheckExternalLinksJob;
9
use SilverStripe\ExternalLinks\Tasks\CheckExternalLinksTask;
10
use SilverStripe\Control\Controller;
11
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...
12
13
class CMSExternalLinksController extends Controller
14
{
15
16
    private static $allowed_actions = [
0 ignored issues
show
introduced by
The private property $allowed_actions is not used, and could be removed.
Loading history...
17
        'getJobStatus',
18
        'start'
19
    ];
20
21
    /**
22
     * Respond to Ajax requests for info on a running job
23
     *
24
     * @return string JSON string detailing status of the job
25
     */
26
    public function getJobStatus()
27
    {
28
        // Set headers
29
        HTTP::set_cache_age(0);
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\Control\HTTP::set_cache_age() has been deprecated: 4.2.0:5.0.0 Use HTTPCacheControlMiddleware::singleton()->setMaxAge($age) instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

29
        /** @scrutinizer ignore-deprecated */ HTTP::set_cache_age(0);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
30
        HTTP::add_cache_headers($this->response);
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\Control\HTTP::add_cache_headers() has been deprecated: 4.2.0:5.0.0 Headers are added automatically by HTTPCacheControlMiddleware instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

30
        /** @scrutinizer ignore-deprecated */ HTTP::add_cache_headers($this->response);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
31
        $this->response
32
            ->addHeader('Content-Type', 'application/json')
33
            ->addHeader('Content-Encoding', 'UTF-8')
34
            ->addHeader('X-Content-Type-Options', 'nosniff');
35
36
        // Format status
37
        $track = BrokenExternalPageTrackStatus::get_latest();
38
        if ($track) {
0 ignored issues
show
introduced by
$track is of type SilverStripe\ExternalLin...ExternalPageTrackStatus, thus it always evaluated to true.
Loading history...
39
            return json_encode([
40
                'TrackID' => $track->ID,
41
                'Status' => $track->Status,
42
                'Completed' => $track->getCompletedPages(),
43
                'Total' => $track->getTotalPages()
44
            ]);
45
        }
46
    }
47
48
49
    /**
50
     * Starts a broken external link check
51
     */
52
    public function start()
53
    {
54
        // return if the a job is already running
55
        $status = BrokenExternalPageTrackStatus::get_latest();
56
        if ($status && $status->Status == 'Running') {
57
            return;
58
        }
59
60
        // Create a new job
61
        if (class_exists(QueuedJobService::class)) {
62
            // Force the creation of a new run
63
            BrokenExternalPageTrackStatus::create_status();
64
            $checkLinks = new CheckExternalLinksJob();
65
            singleton(QueuedJobService::class)->queueJob($checkLinks);
66
        } else {
67
            //TODO this hangs as it waits for the connection to be released
68
            // should return back and continue processing
69
            // http://us3.php.net/manual/en/features.connection-handling.php
70
            $task = CheckExternalLinksTask::create();
71
            $task->runLinksCheck();
72
        }
73
    }
74
}
75