TagController::actionStats()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 19
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 28
rs 9.6333
1
<?php
2
3
namespace app\modules\admin\controllers;
4
5
use app\components\JobFactory;
6
use app\components\builders\TagBuilder;
7
use app\models\TagStats;
8
use app\modules\admin\models\Proxy;
9
use app\modules\admin\models\Tag;
10
use app\modules\admin\models\tag\StatsSearch;
11
use Yii;
12
use yii\filters\VerbFilter;
13
use yii\web\Controller;
14
use yii\web\NotFoundHttpException;
15
use yii2tech\csvgrid\CsvGrid;
16
17
/**
18
 * TagController implements the CRUD actions for Tag model.
19
 */
20
class TagController extends Controller
21
{
22
    /**
23
     * @inheritdoc
24
     */
25
    public function behaviors()
26
    {
27
        return [
28
            'verbs' => [
29
                'class' => VerbFilter::class,
30
                'actions' => [
31
                    'monitoring' => ['POST'],
32
                    'delete-stats' => ['POST'],
33
                    'force-update' => ['POST'],
34
                ],
35
            ],
36
        ];
37
    }
38
39
    public function actionForceUpdate($id)
40
    {
41
        $model = $this->findModel($id);
42
        if (!$model->is_valid) {
43
            $tagUpdater = Yii::createObject([
44
                'class' => TagBuilder::class,
45
                'tag' => $model,
46
            ]);
47
            $tagUpdater->setIsValid()
48
                ->save();
49
50
            $job = JobFactory::updateTag($model);
51
            /** @var \yii\queue\Queue $queue */
52
            $queue = Yii::$app->queue;
53
            $queue->push($job);
54
        }
55
    }
56
57
    public function actionDeleteStats($id)
58
    {
59
        TagStats::deleteAll(['account_id' => $id]);
60
61
        return $this->redirect(['tag/stats', 'id' => $id]);
62
    }
63
64
    public function actionSettings($id)
65
    {
66
        $model = $this->findModel($id);
67
        $model->setScenario(Tag::SCENARIO_UPDATE);
68
69
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
70
            if ($model->is_valid) {
71
                $tagUpdater = Yii::createObject([
72
                    'class' => TagBuilder::class,
73
                    'tag' => $model,
74
                ]);
75
                $tagUpdater
76
                    ->setIsValid()
77
                    ->setNextStatsUpdate(null)
78
                    ->save();
79
            } else {
80
                $model->save();
81
            }
82
83
            return $this->redirect(['tag/stats', 'id' => $model->id]);
84
        }
85
86
        $proxies = Proxy::find()
87
            ->active()
88
            ->all();
89
90
        return $this->render('settings', [
91
            'model' => $model,
92
            'proxies' => $proxies,
93
        ]);
94
    }
95
96
    public function actionStats($id)
97
    {
98
        $model = $this->findModel($id);
99
100
        $searchModel = new StatsSearch();
101
        $dataProvider = $searchModel->search($model);
102
103
        if (Yii::$app->request->get('export')) {
104
            $csv = new CsvGrid([
105
                'dataProvider' => $dataProvider,
106
                'columns' => [
107
                    'media',
108
                    'likes',
109
                    'min_likes',
110
                    'max_likes',
111
                    'comments',
112
                    'min_comments',
113
                    'max_comments',
114
                    'created_at',
115
                ],
116
            ]);
117
118
            return $csv->export()->send(sprintf('%s_stats_%s.csv', mb_strtolower($model->slug), date('Y-m-d')));
119
        }
120
121
        return $this->render('stats', [
122
            'model' => $model,
123
            'dataProvider' => $dataProvider,
124
        ]);
125
    }
126
127
    /**
128
     * Finds the Tag model based on its primary key value.
129
     * If the model is not found, a 404 HTTP exception will be thrown.
130
     *
131
     * @param integer $id
132
     * @return Tag the loaded model
133
     * @throws NotFoundHttpException if the model cannot be found
134
     */
135
    protected function findModel($id)
136
    {
137
        if (($model = Tag::findOne($id)) !== null) {
138
            return $model;
139
        } else {
140
            throw new NotFoundHttpException('The requested page does not exist.');
141
        }
142
    }
143
}
144