SchedulerTask::beforeSave()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
/**
3
 * @copyright Copyright(c) 2016 Webtools Ltd
4
 * @copyright Copyright(c) 2018 Thamtech, LLC
5
 * @link https://github.com/thamtech/yii2-scheduler
6
 * @license https://opensource.org/licenses/MIT
7
**/
8
9
namespace thamtech\scheduler\models;
10
11
use Yii;
12
use yii\helpers\Inflector;
13
14
/**
15
 * This is the model class for table "scheduler_task".
16
 */
17
class SchedulerTask extends \thamtech\scheduler\models\base\SchedulerTask
18
{
19
    const STATUS_INACTIVE = 0;
20
    const STATUS_PENDING = 10;
21
    const STATUS_DUE = 20;
22
    const STATUS_RUNNING = 30;
23
    const STATUS_OVERDUE = 40;
24
    const STATUS_ERROR = 50;
25
26
    public $active = true;
27
28
    /**
29
     * @var array
30
     */
31
    private static $_statuses = [
32
        self::STATUS_INACTIVE => 'Inactive',
33
        self::STATUS_PENDING => 'Pending',
34
        self::STATUS_DUE => 'Due',
35
        self::STATUS_RUNNING => 'Running',
36
        self::STATUS_OVERDUE => 'Overdue',
37
        self::STATUS_ERROR => 'Error',
38
    ];
39
40
    /**
41
     * Return Taskname
42
     *
43
     * @return string
44
     */
45
    public function __toString()
46
    {
47
        return Inflector::camel2words($this->name);
48
    }
49
50
    /**
51
     *
52
     * @param string $name
53
     *
54
     * @param Task $task
0 ignored issues
show
Bug introduced by
The type thamtech\scheduler\models\Task 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...
55
     *
56
     * @return array|null|SchedulerTask|\yii\db\ActiveRecord
57
     */
58 1
    public static function createTaskModel($name, $task)
59
    {
60 1
        $model = SchedulerTask::find()
61 1
            ->where(['name' => $name])
62 1
            ->one();
63
64 1
        if (!$model) {
65 1
            $model = new SchedulerTask();
66 1
            $model->name = $name;
67 1
            $model->display_name = $task->displayName;
68 1
            $model->next_run = $task->getNextRunDate();
69 1
            $model->last_run = NULL;
70 1
            $model->status_id = self::STATUS_PENDING;
71 1
            $model->active = $task->active;
72
        }
73
74 1
        $model->description = $task->description;
75 1
        $model->schedule = $task->schedule;
76 1
        $model->save(false);
77
78 1
        return $model;
79
    }
80
81
    /**
82
     * Save the next run time
83
     *
84
     * @param int|null $time unix timestamp of next run time or null to imply NOW()
85
     *
86
     * @return bool whether the saving succeeded
87
     */
88
    public function saveNextRunTime($time = null)
89
    {
90
        if ($time === null) {
91
            $time = time();
92
        }
93
        $this->next_run = date('Y-m-d H:i:s', $time);
94
        return $this->save();
95
    }
96
97
    /**
98
     * @return string|null
99
     */
100
    public function getStatus()
101
    {
102
        return isset(self::$_statuses[$this->status_id]) ? self::$_statuses[$this->status_id] : null;
103
    }
104
105
106
    /**
107
     * Update the status of the task based on various factors.
108
     */
109 1
    public function updateStatus()
110
    {
111 1
        $status = $this->status_id;
112 1
        $isDue = in_array(
113 1
            $status,
114
            [
115 1
                self::STATUS_PENDING,
116 1
                self::STATUS_DUE,
117 1
                self::STATUS_OVERDUE,
118
            ]
119 1
        ) && strtotime($this->next_run) <= time();
120
121 1
        if ($isDue && $this->started_at == null) {
122
            $status = self::STATUS_DUE;
123 1
        } elseif ($this->started_at !== null) {
124
            $status = self::STATUS_RUNNING;
125 1
        } elseif ($this->status_id == self::STATUS_ERROR) {
126
            $status = $this->status_id;
127 1
        } elseif (!$isDue) {
128 1
            $status = self::STATUS_PENDING;
129
        }
130
131 1
        if (!$this->active) {
132
            $status = self::STATUS_INACTIVE;
133
        }
134
135 1
        $this->status_id = $status;
136 1
    }
137
138 1
    public function beforeSave($insert)
139
    {
140 1
        $this->updateStatus();
141 1
        return parent::beforeSave($insert);
142
    }
143
}
144