Passed
Push — master ( 04a8aa...fbd881 )
by Roman
01:38
created

ExecRecord::isDone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
/**
3
 * @link https://github.com/zhuravljov/yii2-queue-monitor
4
 * @copyright Copyright (c) 2017 Roman Zhuravlev
5
 * @license http://opensource.org/licenses/BSD-3-Clause
6
 */
7
8
namespace zhuravljov\yii\queue\monitor\records;
9
10
use Yii;
11
use yii\db\ActiveRecord;
12
use zhuravljov\yii\queue\monitor\Env;
13
14
/**
15
 * Class ExecRecord
16
 *
17
 * @property int $id
18
 * @property int $push_id
19
 * @property null|int $worker_id
20
 * @property int $attempt
21
 * @property int $reserved_at
22
 * @property null|int $done_at
23
 * @property null|string $error
24
 * @property null|bool $retry
25
 *
26
 * @property PushRecord $push
27
 * @property null|WorkerRecord $worker
28
 *
29
 * @property int $duration
30
 * @property false|string $errorMessage
31
 *
32
 * @author Roman Zhuravlev <[email protected]>
33
 */
34
class ExecRecord extends ActiveRecord
35
{
36
    /**
37
     * @inheritdoc
38
     * @return ExecQuery the active query used by this AR class.
39
     */
40
    public static function find()
41
    {
42
        return new ExecQuery(get_called_class());
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public static function getDb()
49
    {
50
        return Yii::$container->get(Env::class)->db;
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56
    public static function tableName()
57
    {
58
        return Yii::$container->get(Env::class)->execTableName;
59
    }
60
61
    /**
62
     * @return PushQuery
63
     */
64
    public function getPush()
65
    {
66
        return $this->hasOne(PushRecord::class, ['id' => 'push_id']);
67
    }
68
69
    /**
70
     * @return WorkerQuery
71
     */
72
    public function getWorker()
73
    {
74
        return $this->hasOne(WorkerRecord::class, ['id' => 'worker_id']);
75
    }
76
77
    /**
78
     * @return int
79
     */
80
    public function getDuration()
81
    {
82
        if ($this->done_at) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->done_at of type null|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
83
            return $this->done_at - $this->reserved_at;
84
        } else {
85
            return time() - $this->reserved_at;
86
        }
87
    }
88
89
    /**
90
     * @return bool
91
     */
92
    public function isDone()
93
    {
94
        return $this->done_at !== null;
95
    }
96
97
    /**
98
     * @return bool
99
     */
100
    public function isFailed()
101
    {
102
        return $this->error !== null;
103
    }
104
105
    /**
106
     * @return false|string first error line
107
     */
108
    public function getErrorMessage()
109
    {
110
        if ($this->_errorMessage === null) {
111
            $this->_errorMessage = false;
112
            if ($this->error !== null) {
113
                $this->_errorMessage = trim(explode("\n", $this->error, 2)[0]);
114
            }
115
        }
116
        return $this->_errorMessage;
117
    }
118
119
    private $_errorMessage;
120
}
121