Passed
Push — master ( b313ea...47c9db )
by Roman
03:00
created

JobController::actionViewData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
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\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\filters\JobFilter;
16
use zhuravljov\yii\queue\monitor\Module;
17
use zhuravljov\yii\queue\monitor\records\PushRecord;
18
19
/**
20
 * Class JobController
21
 *
22
 * @author Roman Zhuravlev <[email protected]>
23
 */
24
class JobController extends Controller
25
{
26
    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...ntrollers\JobController.
Loading history...
27
28
    /**
29
     * @var Module
30
     */
31
    public $module;
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public function behaviors()
37
    {
38
        return [
39
            'verbs' => [
40
                'class' => VerbFilter::class,
41
                'actions' => [
42
                    'push' => ['post'],
43
                    'stop' => ['post'],
44
                ],
45
            ],
46
        ];
47
    }
48
49
    /**
50
     * Pushed jobs
51
     *
52
     * @return mixed
53
     */
54
    public function actionIndex()
55
    {
56
        return $this->render('index', [
57
            'filter' => JobFilter::ensure(),
58
        ]);
59
    }
60
61
    /**
62
     * Job view
63
     *
64
     * @param int $id
65
     * @return mixed
66
     */
67
    public function actionView($id)
68
    {
69
        $record = $this->findRecord($id);
70
        if ($record->lastExec && $record->lastExec->isFailed()) {
71
            return $this->redirect(['view-attempts', 'id' => $record->id]);
72
        }
73
        return $this->redirect(['view-details', 'id' => $record->id]);
74
    }
75
76
    /**
77
     * Push details
78
     *
79
     * @param int $id
80
     * @return mixed
81
     */
82
    public function actionViewDetails($id)
83
    {
84
        return $this->render('view-details', [
85
            'record' => $this->findRecord($id),
86
        ]);
87
    }
88
89
    /**
90
     * Push environment
91
     *
92
     * @param int $id
93
     * @return mixed
94
     */
95
    public function actionViewContext($id)
96
    {
97
        return $this->render('view-context', [
98
            'record' => $this->findRecord($id),
99
        ]);
100
    }
101
102
    /**
103
     * Job object data
104
     *
105
     * @param int $id
106
     * @return mixed
107
     */
108
    public function actionViewData($id)
109
    {
110
        return $this->render('view-data', [
111
            'record' => $this->findRecord($id),
112
        ]);
113
    }
114
115
    /**
116
     * Attempts
117
     *
118
     * @param int $id
119
     * @return mixed
120
     */
121
    public function actionViewAttempts($id)
122
    {
123
        return $this->render('view-attempts', [
124
            'record' => $this->findRecord($id),
125
        ]);
126
    }
127
128
    /**
129
     * Pushes a job again
130
     *
131
     * @param int $id
132
     * @throws
133
     * @return mixed
134
     */
135
    public function actionPush($id)
136
    {
137
        if (!$this->module->canPushAgain) {
138
            throw new ForbiddenHttpException('Push is forbidden.');
139
        }
140
141
        $record = $this->findRecord($id);
142
143
        if (!$record->isSenderValid()) {
144
            return $this
145
                ->error("The job isn't pushed because $record->sender_name component isn't found.")
146
                ->redirect(['view-details', 'id' => $record->id]);
147
        }
148
149
        if (!$record->isJobValid()) {
150
            return $this
151
                ->error('The job isn\'t pushed because it must be JobInterface instance.')
152
                ->redirect(['view-data', 'id' => $record->id]);
153
        }
154
155
        $uid = $record->getSender()->push($record->createJob());
156
        $newRecord = PushRecord::find()->byJob($record->sender_name, $uid)->one();
157
158
        return $this
159
            ->success('The job is pushed again.')
160
            ->redirect(['view', 'id' => $newRecord->id]);
161
    }
162
163
    /**
164
     * Stop a job
165
     *
166
     * @param int $id
167
     * @throws
168
     * @return mixed
169
     */
170
    public function actionStop($id)
171
    {
172
        if (!$this->module->canExecStop) {
173
            throw new ForbiddenHttpException('Stop is forbidden.');
174
        }
175
176
        $record = $this->findRecord($id);
177
178
        if ($record->isStopped()) {
179
            return $this
180
                ->error('The job is already stopped.')
181
                ->redirect(['view-details', 'id' => $record->id]);
182
        }
183
184
        if (!$record->canStop()) {
185
            return $this
186
                ->error('The job is already done.')
187
                ->redirect(['view-attempts', 'id' => $record->id]);
188
        }
189
190
        $record->stop();
191
192
        return $this
193
            ->success('The job will be stopped.')
194
            ->redirect(['view-details', 'id' => $record->id]);
195
    }
196
197
    /**
198
     * @param int $id
199
     * @throws NotFoundHttpException
200
     * @return PushRecord
201
     */
202
    protected function findRecord($id)
203
    {
204
        if ($record = PushRecord::find()->byId($id)->one()) {
205
            return $record;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $record returns the type array which is incompatible with the documented return type zhuravljov\yii\queue\monitor\records\PushRecord.
Loading history...
206
        }
207
        throw new NotFoundHttpException('Record not found.');
208
    }
209
}
210