Passed
Push — master ( 013f43...62ccc4 )
by Roman
02:06
created

PushQuery::deprecated()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 DateInterval;
11
use DateTime;
12
use yii\db\ActiveQuery;
13
use yii\db\Query;
14
15
/**
16
 * Push Query
17
 *
18
 * @author Roman Zhuravlev <[email protected]>
19
 */
20
class PushQuery extends ActiveQuery
21
{
22
    /**
23
     * @inheritdoc
24
     */
25
    public function init()
26
    {
27
        parent::init();
28
        $this->alias('push');
29
    }
30
31
    /**
32
     * @param int $id
33
     * @return $this
34
     */
35
    public function byId($id)
36
    {
37
        return $this->andWhere(['push.id' => $id]);
38
    }
39
40
    /**
41
     * @param string $senderName
42
     * @param string $jobUid
43
     * @return $this
44
     */
45
    public function byJob($senderName, $jobUid)
46
    {
47
        return $this
48
            ->andWhere(['push.sender_name' => $senderName])
49
            ->andWhere(['push.job_uid' => $jobUid])
50
            ->orderBy(['push.id' => SORT_DESC])
51
            ->limit(1);
52
    }
53
54
    /**
55
     * @return $this
56
     */
57
    public function waiting()
58
    {
59
        return $this
60
            ->joinLastExec()
61
            ->andWhere(['or', ['push.last_exec_id' => null], ['last_exec.retry' => true]])
62
            ->andWhere(['push.stopped_at' => null]);
63
    }
64
65
    /**
66
     * @return $this
67
     */
68
    public function inProgress()
69
    {
70
        return $this
71
            ->andWhere(['is not', 'push.last_exec_id', null])
72
            ->joinLastExec()
73
            ->andWhere(['last_exec.finished_at' => null]);
74
    }
75
76
    /**
77
     * @return $this
78
     */
79
    public function done()
80
    {
81
        return $this
82
            ->joinLastExec()
83
            ->andWhere(['is not', 'last_exec.finished_at', null])
84
            ->andWhere(['last_exec.retry' => false]);
85
    }
86
87
    /**
88
     * @return $this
89
     */
90
    public function success()
91
    {
92
        return $this
93
            ->done()
94
            ->andWhere(['last_exec.error' => null]);
95
    }
96
97
    /**
98
     * @return $this
99
     */
100
    public function buried()
101
    {
102
        return $this
103
            ->done()
104
            ->andWhere(['is not', 'last_exec.error', null]);
105
    }
106
107
    /**
108
     * @return $this
109
     */
110
    public function hasFails()
111
    {
112
        return $this
113
            ->andWhere(['exists', new Query([
114
                'from' => ['exec' => ExecRecord::tableName()],
115
                'where' => '{{exec}}.[[push_id]] = {{push}}.[[id]] AND {{exec}}.[[error]] IS NOT NULL',
116
            ])]);
117
    }
118
119
    /**
120
     * @return $this
121
     */
122
    public function stopped()
123
    {
124
        return $this->andWhere(['is not', 'push.stopped_at', null]);
125
    }
126
127
    /**
128
     * @return $this
129
     */
130
    public function joinFirstExec()
131
    {
132
        return $this->leftJoin(
133
            ['first_exec' => ExecRecord::tableName()],
134
            '{{first_exec}}.[[id]] = {{push}}.[[first_exec_id]]'
135
        );
136
    }
137
138
    /**
139
     * @return $this
140
     */
141
    public function joinLastExec()
142
    {
143
        return $this->leftJoin(
144
            ['last_exec' => ExecRecord::tableName()],
145
            '{{last_exec}}.[[id]] = {{push}}.[[last_exec_id]]'
146
        );
147
    }
148
149
    /**
150
     * @param string $interval
151
     * @link https://www.php.net/manual/en/dateinterval.construct.php
152
     * @return $this
153
     */
154
    public function deprecated($interval)
155
    {
156
        $min = (new DateTime())->sub(new DateInterval($interval))->getTimestamp();
157
        return $this->andWhere(['<', 'push.pushed_at', $min]);
158
    }
159
160
    /**
161
     * @inheritdoc
162
     * @return PushRecord[]|array
163
     */
164
    public function all($db = null)
165
    {
166
        return parent::all($db);
167
    }
168
169
    /**
170
     * @inheritdoc
171
     * @return PushRecord|array|null
172
     */
173
    public function one($db = null)
174
    {
175
        return parent::one($db);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::one($db) also could return the type yii\db\ActiveRecord which is incompatible with the return type mandated by yii\db\QueryInterface::one() of array|boolean.
Loading history...
176
    }
177
}
178