Passed
Push — master ( 8cf58c...3a8f57 )
by Paweł
02:46
created

TagController::actionDeleteStats()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace app\modules\admin\controllers;
4
5
use app\components\TagManager;
6
use app\models\TagStats;
7
use app\modules\admin\models\Tag;
8
use Yii;
9
use yii\data\ActiveDataProvider;
10
use yii\web\Controller;
11
use yii\web\NotFoundHttpException;
12
use yii\filters\VerbFilter;
13
14
/**
15
 * TagController implements the CRUD actions for Tag model.
16
 */
17
class TagController extends Controller
18
{
19
    /**
20
     * @inheritdoc
21
     */
22
    public function behaviors()
23
    {
24
        return [
25
            'verbs' => [
26
                'class' => VerbFilter::class,
27
                'actions' => [
28
                    'monitoring' => ['POST'],
29
                    'delete-stats' => ['POST'],
30
                ],
31
            ],
32
        ];
33
    }
34
35
    public function actionDeleteStats($id)
36
    {
37
        TagStats::deleteAll(['account_id' => $id]);
38
39
        return $this->redirect(['tag/stats', 'id' => $id]);
40
    }
41
42
    public function actionSettings($id)
43
    {
44
        $model = $this->findModel($id);
45
        $model->setScenario(Tag::SCENARIO_UPDATE);
46
47
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
48
            if ($model->disabled) {
49
                $model->monitoring = 0;
50
                $model->save();
51
            } elseif ($model->is_valid) {
52
                $accountManager = Yii::createObject(TagManager::class);
53
                $accountManager->markAsValid($model, 1); // save model
54
            } else {
55
                $model->save();
56
            }
57
58
            return $this->redirect(['tag/stats', 'id' => $model->id]);
59
        }
60
61
        return $this->render('settings', [
62
            'model' => $model,
63
        ]);
64
    }
65
66
    public function actionStats($id)
67
    {
68
        $model = $this->findModel($id);
69
70
        $dataProvider = new ActiveDataProvider([
71
            'query' => $model->getTagStats(),
72
            'sort' => [
73
                'defaultOrder' => ['created_at' => SORT_DESC],
74
            ],
75
        ]);
76
77
        return $this->render('stats', [
78
            'model' => $model,
79
            'dataProvider' => $dataProvider,
80
        ]);
81
    }
82
83
    /**
84
     * Finds the Tag model based on its primary key value.
85
     * If the model is not found, a 404 HTTP exception will be thrown.
86
     *
87
     * @param integer $id
88
     * @return Tag the loaded model
89
     * @throws NotFoundHttpException if the model cannot be found
90
     */
91
    protected function findModel($id)
92
    {
93
        if (($model = Tag::findOne($id)) !== null) {
94
            return $model;
95
        } else {
96
            throw new NotFoundHttpException('The requested page does not exist.');
97
        }
98
    }
99
}
100