Completed
Pull Request — master (#73)
by Helpful
03:21
created

ProcessJobQueueTask::getQueue()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 25
rs 8.439
cc 5
eloc 15
nc 8
nop 1
1
<?php
2
3
/**
4
 * Task used to process the job queue
5
 *
6
 * @author Marcus Nyeholt <[email protected]>
7
 * @license BSD http://silverstripe.org/bsd-license/
8
 */
9
class ProcessJobQueueTask extends BuildTask
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...
10
{
11
    /**
12
     * @return string
13
     */
14
    public function getDescription()
15
    {
16
        return _t(
17
            'ProcessJobQueueTask.Description',
18
            'Used via a cron job to execute queued jobs that need to be run.'
19
        );
20
    }
21
22
    /**
23
     * @param SS_HTTPRequest $request
24
     */
25
    public function run($request)
26
    {
27
        if ($request->getVar('list')) {
28
            // List helper
29
            $this->listJobs();
0 ignored issues
show
Documentation Bug introduced by
The method listJobs does not exist on object<ProcessJobQueueTask>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
30
            return;
31
        }
32
33
        // Check if there is a job to run
34
        $service = $this->getService();
35
        if (($job = $request->getVar('job')) && strpos($job, '-')) {
36
            // Run from a isngle job
37
            $parts = explode('-', $job);
38
            $id = $parts[1];
39
            $service->runJob($id);
40
            return;
41
        }
42
43
        // Run the queue
44
        $queue = $this->getQueue($request);
45
        $service->runQueue($queue);
46
    }
47
48
    /**
49
     * Resolves the queue name to one of a few aliases.
50
     *
51
     * @todo Solve the "Queued"/"queued" mystery!
52
     *
53
     * @param SS_HTTPRequest $request
54
     * @return string
55
     */
56
    protected function getQueue($request)
57
    {
58
        $queue = $request->getVar('queue');
59
60
        if (!$queue) {
61
            $queue = 'Queued';
62
        }
63
64
        switch (strtolower($queue)) {
65
            case 'immediate': {
0 ignored issues
show
Coding Style introduced by
case statements should not use curly braces.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
66
                $queue = QueuedJob::IMMEDIATE;
67
                break;
68
            }
69
            case 'queued': {
0 ignored issues
show
Coding Style introduced by
case statements should not use curly braces.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
70
                $queue = QueuedJob::QUEUED;
71
                break;
72
            }
73
            case 'large': {
0 ignored issues
show
Coding Style introduced by
case statements should not use curly braces.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
74
                $queue = QueuedJob::LARGE;
75
                break;
76
            }
77
        }
78
79
        return $queue;
80
    }
81
82
    /**
83
     * Returns an instance of the QueuedJobService.
84
     *
85
     * @return QueuedJobService
86
     */
87
    public function getService()
88
    {
89
        return singleton('QueuedJobService');
90
    }
91
}
92