Passed
Push — master ( 510a5e...0f9b0c )
by Roman
02:40 queued 14s
created

ExecRecord::getResult()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 3
nc 4
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
 * Exec Record
16
 *
17
 * @property int $id
18
 * @property int $push_id
19
 * @property null|int $worker_id
20
 * @property int $attempt
21
 * @property int $started_at
22
 * @property null|int $finished_at
23
 * @property null|int $memory_usage
24
 * @property null|string $error
25
 * @property null|string|resource $result_data
26
 * @property null|bool $retry
27
 *
28
 * @property PushRecord $push
29
 * @property null|WorkerRecord $worker
30
 *
31
 * @property int $duration
32
 * @property false|string $errorMessage
33
 *
34
 * @author Roman Zhuravlev <[email protected]>
35
 */
36
class ExecRecord extends ActiveRecord
37
{
38
    private $_errorMessage;
39
40
    /**
41
     * @inheritdoc
42
     * @return ExecQuery the active query used by this AR class.
43
     */
44
    public static function find()
45
    {
46
        return Yii::createObject(ExecQuery::class, [get_called_class()]);
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public static function getDb()
53
    {
54
        return Env::ensure()->db;
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public static function tableName()
61
    {
62
        return Env::ensure()->execTableName;
63
    }
64
65
    /**
66
     * @return PushQuery
67
     */
68
    public function getPush()
69
    {
70
        return $this->hasOne(PushRecord::class, ['id' => 'push_id']);
71
    }
72
73
    /**
74
     * @return WorkerQuery
75
     */
76
    public function getWorker()
77
    {
78
        return $this->hasOne(WorkerRecord::class, ['id' => 'worker_id']);
79
    }
80
81
    /**
82
     * @return int
83
     */
84
    public function getDuration()
85
    {
86
        if ($this->finished_at) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->finished_at of type integer|null 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...
87
            return $this->finished_at - $this->started_at;
88
        }
89
        return time() - $this->started_at;
90
    }
91
92
    /**
93
     * @return bool
94
     */
95
    public function isDone()
96
    {
97
        return $this->finished_at !== null;
98
    }
99
100
    /**
101
     * @return bool
102
     */
103
    public function isFailed()
104
    {
105
        return $this->error !== null;
106
    }
107
108
    /**
109
     * @return false|string first error line
110
     */
111
    public function getErrorMessage()
112
    {
113
        if ($this->_errorMessage === null) {
114
            $this->_errorMessage = false;
115
            if ($this->error !== null) {
116
                $this->_errorMessage = trim(explode("\n", $this->error, 2)[0]);
117
            }
118
        }
119
        return $this->_errorMessage;
120
    }
121
122
    public function getResult()
123
    {
124
        if (is_resource($this->result_data)) {
125
            $this->result_data = stream_get_contents($this->result_data);
126
        }
127
        if ($this->result_data) {
128
            return unserialize($this->result_data);
129
        }
130
        return null;
131
    }
132
}
133