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

GcController::actionClearAll()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 2
nc 2
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\console;
9
10
use yii\console\Controller;
11
use zhuravljov\yii\queue\monitor\records\ExecRecord;
12
use zhuravljov\yii\queue\monitor\records\PushRecord;
13
use zhuravljov\yii\queue\monitor\records\WorkerRecord;
14
15
/**
16
 * Garbage Collector Commands of Queue Monitor.
17
 *
18
 * @author Roman Zhuravlev <[email protected]>
19
 */
20
class GcController extends Controller
21
{
22
    /**
23
     * @var bool verbose mode.
24
     */
25
    public $silent = false;
26
27
    /**
28
     * @inheritdoc
29
     */
30
    public function options($actionID)
31
    {
32
        return array_merge(parent::options($actionID), [
33
            'silent',
34
        ]);
35
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40
    public function optionAliases()
41
    {
42
        return array_merge(parent::optionAliases(), [
43
            's' => 'silent',
44
        ]);
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50
    public function beforeAction($action)
51
    {
52
        if ($this->silent) {
53
            $this->interactive = false;
54
        }
55
        return parent::beforeAction($action);
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61
    public function stdout($string)
62
    {
63
        if ($this->silent) {
64
            return false;
65
        }
66
        return parent::stdout($string);
67
    }
68
69
    /**
70
     * Clear deprecated records.
71
     *
72
     * @param string $interval
73
     * @link https://www.php.net/manual/en/dateinterval.construct.php
74
     */
75
    public function actionClearDeprecated($interval)
76
    {
77
        $ids = PushRecord::find()
78
            ->deprecated($interval)
79
            ->done()
80
            ->select('push.id')
81
            ->asArray()->column();
82
        $count = count($ids);
83
        if ($count && $this->confirm("Do you want to delete $count records?")) {
84
            $count = PushRecord::getDb()->transaction(function () use ($ids) {
85
                ExecRecord::deleteAll(['push_id' => $ids]);
86
                return PushRecord::deleteAll(['id' => $ids]);
87
            });
88
            $this->stdout("$count records deleted.\n");
89
        }
90
    }
91
92
    /**
93
     * Clear all records.
94
     */
95
    public function actionClearAll()
96
    {
97
        if ($this->confirm('Are you sure?')) {
98
            $count = PushRecord::getDb()->transaction(function () {
99
                WorkerRecord::deleteAll();
100
                ExecRecord::deleteAll();
101
                return PushRecord::deleteAll();
102
            });
103
            $this->stdout("$count records deleted.\n");
104
        }
105
    }
106
107
    /**
108
     * Clear lost worker records.
109
     */
110
    public function actionClearWorkers()
111
    {
112
        $count = WorkerRecord::deleteAll();
113
        $this->stdout("$count records deleted.\n");
114
    }
115
}
116