Passed
Push — master ( e5a117...444e05 )
by Paweł
09:40
created

AccountController::actionDeleteAssociated()   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\CategoryManager;
6
use app\components\JobFactory;
7
use app\components\builders\AccountBuilder;
8
use app\models\AccountNote;
9
use app\models\Media;
10
use app\modules\admin\models\Account;
11
use app\modules\admin\models\account\MediaAccountSearch;
12
use app\modules\admin\models\account\MediaTagSearch;
13
use app\modules\admin\models\account\StatsSearch;
14
use app\modules\admin\models\AccountStats;
15
use app\modules\admin\models\Proxy;
16
use Yii;
17
use yii\filters\VerbFilter;
18
use yii\web\Controller;
19
use yii\web\NotFoundHttpException;
20
use yii2tech\csvgrid\CsvGrid;
21
22
/**
23
 * AccountController implements the CRUD actions for Account model.
24
 */
25
class AccountController extends Controller
26
{
27
    /**
28
     * @inheritdoc
29
     */
30
    public function behaviors()
31
    {
32
        return [
33
            'verbs' => [
34
                'class' => VerbFilter::class,
35
                'actions' => [
36
                    'delete' => ['POST'],
37
                    'delete-stats' => ['POST'],
38
                    'delete-associated' => ['POST'],
39
                    'categories' => ['POST'],
40
                    'update-note' => ['POST'],
41
                    'force-update' => ['POST'],
42
                ],
43
            ],
44
        ];
45
    }
46
47
    public function actionForceUpdate($id)
48
    {
49
        $model = $this->findModel($id);
50
        if (!$model->is_valid) {
51
            $accountUpdater = Yii::createObject([
52
                'class' => AccountBuilder::class,
53
                'account' => $model,
54
            ]);
55
            $accountUpdater->setIsValid()
56
                ->save();
57
58
            $job = JobFactory::updateAccount($model);
59
            /** @var \yii\queue\Queue $queue */
60
            $queue = Yii::$app->queue;
61
            $queue->push($job);
62
        }
63
    }
64
65
    public function actionDashboard($id)
66
    {
67
        $model = $this->findModel($id);
68
69
        return $this->render('dashboard', [
70
            'model' => $model,
71
        ]);
72
    }
73
74
    public function actionUpdateNote($id)
75
    {
76
        $model = $this->findModel($id);
77
        $user = Yii::$app->user;
78
79
        AccountNote::deleteAll([
80
            'account_id' => $model->id,
81
            'user_id' => $user->id,
82
        ]);
83
84
        $note = new AccountNote();
85
        $note->load(Yii::$app->request->post());
86
        $note->account_id = $model->id;
87
        $note->user_id = $user->id;
0 ignored issues
show
Documentation Bug introduced by
It seems like $user->id can also be of type string. However, the property $user_id is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
88
        $note->save();
89
90
        return $this->redirect(['account/dashboard', 'id' => $id]);
91
    }
92
93
    public function actionSettings($id)
94
    {
95
        $model = $this->findModel($id);
96
        $model->setScenario(Account::SCENARIO_UPDATE);
97
98
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
99
            if ($model->is_valid) {
100
                $accountUpdater = Yii::createObject([
101
                    'class' => AccountBuilder::class,
102
                    'account' => $model,
103
                ]);
104
                $accountUpdater
105
                    ->setIsValid()
106
                    ->setNextStatsUpdate(null)
107
                    ->save();
108
            } else {
109
                $model->save();
110
            }
111
112
            return $this->redirect(['account/dashboard', 'id' => $model->id]);
113
        }
114
115
        $proxies = Proxy::find()
116
            ->active()
117
            ->all();
118
119
        return $this->render('settings', [
120
            'model' => $model,
121
            'proxies' => $proxies,
122
        ]);
123
    }
124
125
    public function actionDeleteStats($id)
126
    {
127
        AccountStats::deleteAll(['account_id' => $id]);
128
129
        return $this->redirect(['account/dashboard', 'id' => $id]);
130
    }
131
132
    public function actionDeleteAssociated($id)
133
    {
134
        Media::deleteAll(['account_id' => $id]);
135
136
        return $this->redirect(['account/dashboard', 'id' => $id]);
137
    }
138
139
    public function actionDelete($id)
140
    {
141
        $this->findModel($id)->delete();
142
143
        return $this->redirect(['monitoring/accounts']);
144
    }
145
146
    public function actionCategories($id)
147
    {
148
        /** @var \app\models\User $identity */
149
        $identity = Yii::$app->user->identity;
150
        $model = $this->findModel($id);
151
        $tags = Yii::$app->request->post('account_tags', []);
152
153
        $manager = Yii::createObject(CategoryManager::class);
154
        $manager->saveForAccount($model, $tags, $identity);
155
156
        return $this->redirect(['account/dashboard', 'id' => $id]);
157
    }
158
159
    public function actionStats($id)
160
    {
161
        $model = $this->findModel($id);
162
163
        $searchModel = new StatsSearch();
164
        $dataProvider = $searchModel->search($model);
165
166
        if (Yii::$app->request->get('export')) {
167
            $csv = new CsvGrid([
168
                'dataProvider' => $dataProvider,
169
                'columns' => [
170
                    'followed_by',
171
                    'follows',
172
                    'media',
173
                    'er',
174
                    'created_at',
175
                ],
176
            ]);
177
178
            return $csv->export()->send(sprintf('%s_stats_%s.csv', mb_strtolower($model->username), date('Y-m-d')));
179
        }
180
181
        return $this->render('stats', [
182
            'model' => $model,
183
            'dataProvider' => $dataProvider,
184
        ]);
185
    }
186
187
    public function actionMediaTags($id)
188
    {
189
        $model = $this->findModel($id);
190
191
        $searchModel = new MediaTagSearch();
192
        $dataProvider = $searchModel->search($model);
193
194
        if (Yii::$app->request->get('export')) {
195
            $csv = new CsvGrid([
196
                'dataProvider' => $dataProvider,
197
                'columns' => [
198
                    'name',
199
                    'occurs',
200
                    'ts_avg_likes',
201
                ],
202
            ]);
203
204
            return $csv->export()->send(sprintf('%s_media-tags_%s.csv', mb_strtolower($model->username), date('Y-m-d')));
205
        }
206
207
208
        return $this->render('media-tags', [
209
            'model' => $model,
210
            'dataProvider' => $dataProvider,
211
        ]);
212
    }
213
214
    public function actionMediaAccounts($id)
215
    {
216
        $model = $this->findModel($id);
217
218
        $searchModel = new MediaAccountSearch();
219
        $dataProvider = $searchModel->search($model);
220
221
        if (Yii::$app->request->get('export')) {
222
            $csv = new CsvGrid([
223
                'dataProvider' => $dataProvider,
224
                'columns' => [
225
                    'username',
226
                    'occurs',
227
                    'er',
228
                    'followed_by',
229
                ],
230
            ]);
231
232
            return $csv->export()->send(sprintf('%s_media-accounts_%s.csv', mb_strtolower($model->username), date('Y-m-d')));
233
        }
234
235
        /** @var \app\models\User $identity */
236
        $identity = Yii::$app->user->identity;
237
        $categoryManager = Yii::createObject(CategoryManager::class);
238
        $categories = $categoryManager->getForUserAccounts($identity, $model);
239
240
        return $this->render('media-accounts', [
241
            'model' => $model,
242
            'dataProvider' => $dataProvider,
243
            'categories' => $categories,
244
        ]);
245
    }
246
247
248
    /**
249
     * Finds the Account model based on its primary key value.
250
     * If the model is not found, a 404 HTTP exception will be thrown.
251
     *
252
     * @param integer $id
253
     * @return Account the loaded model
254
     * @throws NotFoundHttpException if the model cannot be found
255
     */
256
    public function findModel($id)
257
    {
258
        if (($model = Account::findOne($id)) !== null) {
259
            return $model;
260
        } else {
261
            throw new NotFoundHttpException('The requested page does not exist.');
262
        }
263
    }
264
}
265