DevTaskRunnerCronTask::process()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 9.0488
c 0
b 0
f 0
cc 5
nc 3
nop 0
1
<?php
2
3
namespace Webtorque\DevTaskRunner;
4
5
use SilverStripe\CronTask\Interfaces\CronTask;
6
use SilverStripe\Core\Config\Config;
7
use Webtorque\DevTaskRunner\Model\DevTaskRun;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\Control\HTTPRequest;
10
use SilverStripe\ORM\FieldType\DBDatetime;
11
12
class DevTaskRunnerCronTask implements CronTask
13
{
14
    private static $schedule = '*/2 * * * *';
0 ignored issues
show
Unused Code introduced by
The property $schedule 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...
15
16
    public function getSchedule()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
17
    {
18
        return Config::inst()->get('DevTaskRunnerCronTask', 'schedule');
19
    }
20
21
    public function process()
22
    {
23
        $nextTask = DevTaskRun::get_next_task();
24
        if ($nextTask) {
25
            //create task instance
26
            $task = Injector::inst()->create($nextTask->Task);
27
28
            //get params
29
            $params = explode(' ', $nextTask->Params);
30
            $paramList = array();
31
            if ($params) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $params of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
32
                foreach ($params as $param) {
33
                    $parts = explode('=', $param);
34
                    if (count($parts) === 2) {
35
                        $paramList[$parts[0]] = $parts[1];
36
                    }
37
                }
38
            }
39
40
            //set starting flag
41
            $nextTask->Status = 'Running';
42
            $nextTask->write();
43
            echo 'Starting task ' . $task->getTitle() . "\n";
44
45
            //execute task
46
            $request = new HTTPRequest('GET', 'dev/tasks/' . $nextTask->Task, $paramList);
47
            $task->run($request);
48
49
            //set finished flag
50
            $nextTask->Status = 'Finished';
51
            $nextTask->FinishDate = DBDatetime::now()->getValue();
52
            $nextTask->write();
53
            echo 'Finished task ' . $task->getTitle() . "\n";
54
        }
55
    }
56
}