WorkerController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 24
c 2
b 0
f 0
dl 0
loc 79
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A actionStop() 0 13 2
A behaviors() 0 7 1
A __construct() 0 4 1
A actionIndex() 0 4 1
A findRecord() 0 6 2
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\controllers;
9
10
use yii\filters\VerbFilter;
11
use yii\web\Controller;
12
use yii\web\ForbiddenHttpException;
13
use yii\web\NotFoundHttpException;
14
use zhuravljov\yii\queue\monitor\base\FlashTrait;
15
use zhuravljov\yii\queue\monitor\Env;
16
use zhuravljov\yii\queue\monitor\filters\WorkerFilter;
17
use zhuravljov\yii\queue\monitor\Module;
18
use zhuravljov\yii\queue\monitor\records\WorkerRecord;
19
20
/**
21
 * Worker Controller
22
 *
23
 * @author Roman Zhuravlev <[email protected]>
24
 */
25
class WorkerController extends Controller
26
{
27
    use FlashTrait;
0 ignored issues
show
Bug introduced by
The trait zhuravljov\yii\queue\monitor\base\FlashTrait requires the property $session which is not provided by zhuravljov\yii\queue\mon...ollers\WorkerController.
Loading history...
28
29
    /**
30
     * @var Module
31
     */
32
    public $module;
33
    /**
34
     * @var Env
35
     */
36
    protected $env;
37
38
    public function __construct($id, $module, Env $env, array $config = [])
39
    {
40
        $this->env = $env;
41
        parent::__construct($id, $module, $config);
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function behaviors()
48
    {
49
        return [
50
            'verb' => [
51
                'class' => VerbFilter::class,
52
                'actions' => [
53
                    'stop' => ['post'],
54
                ],
55
            ],
56
        ];
57
    }
58
59
    /**
60
     * Worker List
61
     *
62
     * @return string
63
     */
64
    public function actionIndex()
65
    {
66
        return $this->render('index', [
67
            'filter' => WorkerFilter::ensure(),
68
        ]);
69
    }
70
71
    /**
72
     * Stops a worker
73
     *
74
     * @param int $id
75
     * @throws ForbiddenHttpException
76
     * @return \yii\web\Response
77
     */
78
    public function actionStop($id)
79
    {
80
        if (!$this->module->canWorkerStop) {
81
            throw new ForbiddenHttpException(Module::t('notice', 'Stop is forbidden.'));
82
        }
83
84
        $record = $this->findRecord($id);
85
        $record->stop();
86
        return $this
87
            ->success(Module::t('notice', 'The worker will be stopped within {timeout} sec.', [
88
                'timeout' => $record->pinged_at + $this->env->workerPingInterval - time(),
89
            ]))
90
            ->redirect(['index']);
91
    }
92
93
    /**
94
     * @param int $id
95
     * @throws NotFoundHttpException
96
     * @return WorkerRecord
97
     */
98
    protected function findRecord($id)
99
    {
100
        if ($record = WorkerRecord::findOne($id)) {
101
            return $record;
102
        }
103
        throw new NotFoundHttpException(Module::t('notice', 'Record not found.'));
104
    }
105
}
106