Completed
Push — master ( 8e4a64...521cc3 )
by Paweł
04:47
created

MonitoringController::actionDeleteAccount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 19.01.2018
6
 */
7
8
namespace app\modules\admin\controllers;
9
10
11
use app\components\AccountManager;
12
use app\components\stats\AccountDailyDiff;
13
use app\components\stats\AccountMonthlyDiff;
14
use app\components\stats\TagDailyDiff;
15
use app\components\stats\TagMonthlyDiff;
16
use app\components\TagManager;
17
use app\dictionaries\TrackerType;
18
use app\models\Account;
19
use app\models\Tag;
20
use app\modules\admin\models\MonitoringForm;
21
use app\modules\admin\models\AccountSearch;
22
use app\modules\admin\models\TagSearch;
23
use yii\filters\VerbFilter;
24
use yii\helpers\StringHelper;
25
use yii\helpers\Url;
26
use yii\web\Controller;
27
28
class MonitoringController extends Controller
29
{
30
    public function behaviors()
31
    {
32
        return [
33
            'verbs' => [
34
                'class' => VerbFilter::class,
35
                'actions' => [
36
                    'create-account' => ['POST'],
37
                    'delete-account' => ['POST'],
38
                    'create-tag' => ['POST'],
39
                    'delete-tag' => ['POST'],
40
                ],
41
            ],
42
        ];
43
    }
44
45
    public function actionDeleteTag($id)
46
    {
47
        $model = Tag::findOne($id);
48
        $model->monitoring = 0;
49
        if ($model->save()) {
50
            \Yii::$app->session->setFlash('success', 'OK!');
0 ignored issues
show
Bug introduced by
The method setFlash() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
            \Yii::$app->session->/** @scrutinizer ignore-call */ 
51
                                 setFlash('success', 'OK!');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
51
52
            return Url::to(['monitoring/tags']);
53
        } else {
54
            \Yii::$app->session->setFlash('error', 'ERROR!');
55
        }
56
    }
57
58
    public function actionDeleteAccount($id)
59
    {
60
        $model = Account::findOne($id);
61
        $model->monitoring = 0;
0 ignored issues
show
Documentation Bug introduced by
The property $monitoring was declared of type boolean, but 0 is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
62
        if ($model->save()) {
63
            \Yii::$app->session->setFlash('success', 'OK!');
64
65
            return Url::to(['monitoring/accounts']);
66
        } else {
67
            \Yii::$app->session->setFlash('error', 'ERROR!');
68
        }
69
    }
70
71
    public function actionCreateTag()
72
    {
73
        $form = new MonitoringForm();
74
        $form->setScenario(TrackerType::TAG);
75
76
        if ($form->load(\Yii::$app->request->post()) && $form->validate()) {
77
            $names = StringHelper::explode($form->names, ',', true, true);
78
79
            $tagManager = \Yii::createObject(TagManager::class);
80
81
            foreach ($names as $name) {
82
                $tag = $tagManager->monitor($name, $form->proxy_id, $form->proxy_tag_id);
83
                if (!$tag->hasErrors()) {
84
                    \Yii::$app->session->setFlash('success', 'OK!');
85
                } else {
86
                    \Yii::error('Validation error: ' . json_encode($tag->errors), __METHOD__);
87
                    \Yii::$app->session->setFlash('error', "ERR! {$name}");
88
                    break;
89
                }
90
91
            }
92
        }
93
94
        return $this->redirect(['monitoring/tags', 'sort' => '-created_at']);
95
    }
96
97
    public function actionCreateAccount()
98
    {
99
        $form = new MonitoringForm();
100
        $form->setScenario(TrackerType::ACCOUNT);
101
102
        if ($form->load(\Yii::$app->request->post()) && $form->validate()) {
103
            $usernames = StringHelper::explode($form->names, ',', true, true);
104
105
            $accountManager = \Yii::createObject(AccountManager::class);
106
107
            foreach ($usernames as $username) {
108
                $account = $accountManager->monitor($username, $form->proxy_id, $form->proxy_tag_id);
109
                if (!$account->hasErrors()) {
110
                    \Yii::$app->session->setFlash('success', 'OK!');
111
                    $tags = array_filter((array)$form->tags);
112
                    if ($tags) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $tags of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
113
                        $tagManager = \Yii::createObject(TagManager::class);
114
                        $tagManager->saveForAccount($account, $tags, \Yii::$app->user->id);
115
                    }
116
                } else {
117
                    \Yii::error('Validation error: ' . json_encode($account->errors), __METHOD__);
118
                    \Yii::$app->session->setFlash('error', "ERR! {$username}");
119
                    break;
120
                }
121
122
            }
123
        }
124
125
        return $this->redirect(['monitoring/accounts', 'sort' => '-created_at']);
126
    }
127
128
    public function actionTags()
129
    {
130
        $searchModel = new TagSearch();
131
        $dataProvider = $searchModel->search(\Yii::$app->request->queryParams);
132
        $dataProvider->query->andWhere(['tag.monitoring' => 1]);
133
134
        $dailyDiff = \Yii::createObject([
135
            'class' => TagDailyDiff::class,
136
            'models' => $dataProvider->models,
137
        ]);
138
        $dailyDiff->initLastDiff();
139
140
        $monthlyDiff = \Yii::createObject([
141
            'class' => TagMonthlyDiff::class,
142
            'models' => $dataProvider->models,
143
        ]);
144
        $monthlyDiff->initLastDiff();
145
146
        return $this->render('tags', [
147
            'searchModel' => $searchModel,
148
            'dataProvider' => $dataProvider,
149
            'dailyDiff' => $dailyDiff,
150
            'monthlyDiff' => $monthlyDiff,
151
        ]);
152
    }
153
154
    public function actionAccounts()
155
    {
156
        $searchModel = new AccountSearch();
157
        $dataProvider = $searchModel->search(\Yii::$app->request->queryParams);
158
        $dataProvider->query->andWhere(['account.monitoring' => 1]);
159
160
        $dailyDiff = \Yii::createObject([
161
            'class' => AccountDailyDiff::class,
162
            'models' => $dataProvider->models,
163
        ]);
164
        $dailyDiff->initLastDiff();
165
166
        $monthlyDiff = \Yii::createObject([
167
            'class' => AccountMonthlyDiff::class,
168
            'models' => $dataProvider->models,
169
        ]);
170
        $monthlyDiff->initLastDiff();
171
172
        return $this->render('accounts', [
173
            'searchModel' => $searchModel,
174
            'dataProvider' => $dataProvider,
175
            'dailyDiff' => $dailyDiff,
176
            'monthlyDiff' => $monthlyDiff,
177
        ]);
178
    }
179
}