Completed
Push — master ( 07eda8...8e2f95 )
by Paweł
02:52
created

MonitoringController::actionAccounts()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 2
nop 0
dl 0
loc 19
rs 9.4285
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\models\Favorite;
13
use app\modules\admin\models\Account;
14
use app\modules\admin\models\AccountMonitoringForm;
15
use app\modules\admin\models\AccountSearch;
16
use app\modules\admin\models\Tag;
17
use app\modules\admin\models\TagMonitoringForm;
18
use app\modules\admin\models\TagSearch;
19
use yii\filters\VerbFilter;
20
use yii\helpers\Inflector;
21
use yii\helpers\StringHelper;
22
use yii\web\Controller;
23
24
class MonitoringController extends Controller
25
{
26
    public function behaviors()
27
    {
28
        return [
29
            'verbs' => [
30
                'class' => VerbFilter::class,
31
                'actions' => [
32
                    'create-account' => ['POST'],
33
                ],
34
            ],
35
        ];
36
    }
37
38
    public function actionCreateTag()
39
    {
40
        $form = new TagMonitoringForm();
41
42
        if ($form->load(\Yii::$app->request->post()) && $form->validate()) {
43
            $names = StringHelper::explode($form->names, ',', true, true);
44
            foreach ($names as $name) {
45
                $tag = Tag::findOne(['slug' => Inflector::slug($name)]);
46
                if ($tag === null) {
47
                    $tag = new Tag(['name' => $name]);
48
                }
49
                $tag->proxy_id = $form->proxy_id ?: null;
50
                $tag->proxy_tag_id = $form->proxy_tag_id ?: null;
51
                $tag->monitoring = 1;
52
                if ($tag->save()) {
53
                    \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

53
                    \Yii::$app->session->/** @scrutinizer ignore-call */ 
54
                                         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...
54
                } else {
55
                    \Yii::error('Validation error: ' . json_encode($tag->errors), __METHOD__);
56
                    \Yii::$app->session->setFlash('error', 'ERR!');
57
                }
58
59
            }
60
        }
61
62
        return $this->redirect(['monitoring/tags', 'sort' => '-created_at']);
63
    }
64
65
    public function actionCreateAccount()
66
    {
67
        $form = new AccountMonitoringForm();
68
69
        if ($form->load(\Yii::$app->request->post()) && $form->validate()) {
70
            $usernames = StringHelper::explode($form->names, ',', true, true);
71
72
            $accountManager = \Yii::createObject(AccountManager::class);
73
74
            foreach ($usernames as $username) {
75
                $account = $accountManager->monitor($username, $form->proxy_id, $form->proxy_tag_id);
76
                if (!$account->hasErrors()) {
77
                    \Yii::$app->session->setFlash('success', 'OK!');
78
                    $accountManager->updateTags($account, (array)$form->tags);
79
                } else {
80
                    \Yii::error('Validation error: ' . json_encode($account->errors), __METHOD__);
81
                    \Yii::$app->session->setFlash('error', "ERR! {$username}");
82
                    break;
83
                }
84
85
            }
86
        }
87
88
        return $this->redirect(['monitoring/accounts', 'sort' => '-created_at']);
89
    }
90
91
    public function actionTags()
92
    {
93
        $searchModel = new TagSearch(['monitoring' => 1]);
94
        $dataProvider = $searchModel->search(\Yii::$app->request->queryParams);
95
96
        return $this->render('tags', [
97
            'searchModel' => $searchModel,
98
            'dataProvider' => $dataProvider,
99
        ]);
100
    }
101
102
    public function actionAccounts()
103
    {
104
105
        $label = \Yii::$app->request->post('label');
106
        $url = \Yii::$app->request->post('url');
107
        if ($label && $url) {
108
            (new Favorite([
109
                'url' => $url,
110
                'label' => "<span class='fa fa-search'></span> $label",
111
            ]))->insert(false);
112
        }
113
114
        $searchModel = new AccountSearch();
115
        $dataProvider = $searchModel->search(\Yii::$app->request->queryParams);
116
        $dataProvider->query->andWhere(['account.monitoring' => 1]);
117
118
        return $this->render('accounts', [
119
            'searchModel' => $searchModel,
120
            'dataProvider' => $dataProvider,
121
        ]);
122
    }
123
}