Completed
Pull Request — master (#13)
by Helpful
02:24
created

CMSExternalLinks_Controller::getJobStatus()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 21
rs 9.3143
cc 2
eloc 14
nc 2
nop 0
1
<?php
2
3
class CMSExternalLinks_Controller extends Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
6
    private static $allowed_actions = array('getJobStatus', 'start');
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
7
8
    /*
9
     * Respond to Ajax requests for info on a running job
10
     *
11
     * @return string JSON string detailing status of the job
12
     */
13
    public function getJobStatus()
14
    {
15
        // Set headers
16
        HTTP::set_cache_age(0);
17
        HTTP::add_cache_headers($this->response);
18
        $this->response
19
            ->addHeader('Content-Type', 'application/json')
20
            ->addHeader('Content-Encoding', 'UTF-8')
21
            ->addHeader('X-Content-Type-Options', 'nosniff');
22
23
        // Format status
24
        $track = BrokenExternalPageTrackStatus::get_latest();
25
        if ($track) {
26
            return json_encode(array(
27
            'TrackID' => $track->ID,
28
            'Status' => $track->Status,
0 ignored issues
show
Documentation introduced by
The property Status does not exist on object<BrokenExternalPageTrackStatus>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
29
            'Completed' => $track->getCompletedPages(),
30
            'Total' => $track->getTotalPages()
31
        ));
32
        }
33
    }
34
35
36
    /*
37
     * Starts a broken external link check
38
     */
39
    public function start()
40
    {
41
        // return if the a job is already running
42
        $status = BrokenExternalPageTrackStatus::get_latest();
43
        if ($status && $status->Status == 'Running') {
0 ignored issues
show
Documentation introduced by
The property Status does not exist on object<BrokenExternalPageTrackStatus>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
44
            return;
45
        }
46
47
        // Create a new job
48
        if (class_exists('QueuedJobService')) {
49
            // Force the creation of a new run
50
            BrokenExternalPageTrackStatus::create_status();
51
            $checkLinks = new CheckExternalLinksJob();
52
            singleton('QueuedJobService')->queueJob($checkLinks);
53
        } else {
54
            //TODO this hangs as it waits for the connection to be released
55
            // should return back and continue processing
56
            // http://us3.php.net/manual/en/features.connection-handling.php
57
            $task = CheckExternalLinksTask::create();
58
            $task->runLinksCheck();
59
        }
60
    }
61
}
62