Completed
Push — master ( 9d0cba...630902 )
by Paweł
02:59
created

AccountController::actionUpdateNote()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 17
rs 9.7
1
<?php
2
3
namespace app\modules\admin\controllers;
4
5
use app\components\AccountManager;
6
use app\components\stats\AccountDaily;
7
use app\components\stats\AccountDailyDiff;
8
use app\components\stats\AccountMonthlyDiff;
9
use app\components\TagManager;
10
use app\models\AccountNote;
11
use app\models\Media;
12
use app\models\Tag;
13
use app\modules\admin\controllers\actions\FavoriteAction;
14
use app\modules\admin\controllers\actions\MonitoringAction;
15
use app\modules\admin\models\Account;
16
use app\modules\admin\models\AccountStats;
17
use Carbon\Carbon;
18
use Yii;
19
use yii\data\ActiveDataProvider;
20
use yii\db\Query;
21
use yii\helpers\Url;
22
use yii\web\Controller;
23
use yii\web\NotFoundHttpException;
24
use yii\filters\VerbFilter;
25
26
/**
27
 * AccountController implements the CRUD actions for Account model.
28
 */
29
class AccountController extends Controller
30
{
31
    /**
32
     * @inheritdoc
33
     */
34
    public function behaviors()
35
    {
36
        return [
37
            'verbs' => [
38
                'class' => VerbFilter::class,
39
                'actions' => [
40
                    'delete' => ['POST'],
41
                    'delete-stats' => ['POST'],
42
                    'delete-associated' => ['POST'],
43
                    'monitoring' => ['POST'],
44
                    'tags' => ['POST'],
45
                    'favorite' => ['POST'],
46
                    'update-note' => ['POST'],
47
                ],
48
            ],
49
        ];
50
    }
51
52
    public function actions()
53
    {
54
        return [
55
            'favorite' => FavoriteAction::class,
56
            'monitoring' => MonitoringAction::class,
57
        ];
58
    }
59
60
    public function beforeAction($action)
61
    {
62
        if (parent::beforeAction($action)) {
63
            if (in_array($this->action->id, ['stats', 'media-tags', 'media-accounts'])) {
64
                Url::remember(Url::current());
65
            }
66
67
            return true;
68
        }
69
70
        return false;
71
    }
72
73
    public function actionDashboard($id)
74
    {
75
        $model = $this->findModel($id);
76
77
        $dailyDiff = Yii::createObject([
78
            'class' => AccountDailyDiff::class,
79
            'models' => $model,
80
        ]);
81
        $dailyDiff->initDiff(Carbon::now()->subMonth());
82
        $dailyChanges = $dailyDiff->getDiff($model->id);
83
        $dailyDiff->initLastDiff();
84
        $lastDailyChange = $dailyDiff->getLastDiff($model->id);
85
86
87
        $monthlyDiff = Yii::createObject([
88
            'class' => AccountMonthlyDiff::class,
89
            'models' => $model,
90
        ]);
91
        $monthlyDiff->initDiff(Carbon::now()->subYear());
92
        $monthlyChanges = $monthlyDiff->getDiff($model->id);
93
        $monthlyDiff->initLastDiff();
94
        $lastMonthlyChange = $monthlyDiff->getLastDiff($model->id);
95
96
        $dailyStats = Yii::createObject(AccountDaily::class, [$model]);
97
        $dailyStats->initDiff(Carbon::now()->subMonth());
98
        $dailyStats = $dailyStats->get();
99
100
        return $this->render('dashboard', [
101
            'model' => $model,
102
            'lastDailyChange' => current($lastDailyChange),
103
            'lastMonthlyChange' => current($lastMonthlyChange),
104
            'dailyStats' => $dailyStats,
105
            'dailyChanges' => $dailyChanges,
106
            'monthlyChanges' => $monthlyChanges,
107
        ]);
108
    }
109
110
    public function actionUpdateNote($id)
111
    {
112
        $model = $this->findModel($id);
113
        $user = Yii::$app->user;
114
115
        AccountNote::deleteAll([
116
            'account_id' => $model->id,
117
            'user_id' => $user->id,
118
        ]);
119
120
        $note = new AccountNote();
121
        $note->load(Yii::$app->request->post());
122
        $note->account_id = $model->id;
123
        $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...
124
        $note->save();
125
126
        return $this->redirect(['account/dashboard', 'id' => $id]);
127
    }
128
129
    public function actionSettings($id)
130
    {
131
        $model = $this->findModel($id);
132
        $model->setScenario(Account::SCENARIO_UPDATE);
133
134
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
135
            return $this->redirect(['account/dashboard', 'id' => $model->id]);
136
        }
137
138
        return $this->render('settings', [
139
            'model' => $model,
140
        ]);
141
    }
142
143
    public function actionDeleteStats($id)
144
    {
145
        AccountStats::deleteAll(['account_id' => $id]);
146
147
        return $this->redirect(['account/dashboard', 'id' => $id]);
148
    }
149
150
    public function actionDeleteAssociated($id)
151
    {
152
        Media::deleteAll(['account_id' => $id]);
153
154
        return $this->redirect(['account/dashboard', 'id' => $id]);
155
    }
156
157
    public function actionDelete($id)
158
    {
159
        $this->findModel($id)->delete();
160
161
        return $this->redirect(['monitoring/accounts']);
162
    }
163
164
    public function actionTags($id)
165
    {
166
        $model = $this->findModel($id);
167
        $tags = Yii::$app->request->post('account_tags', []);
168
169
        $manager = Yii::createObject(TagManager::class);
170
        $manager->setForAccount($model, $tags);
171
172
        return $this->redirect(Url::previous());
173
    }
174
175
    public function actionStats($id)
176
    {
177
        $model = $this->findModel($id);
178
179
        $dataProvider = new ActiveDataProvider([
180
            'query' => $model->getAccountStats(),
181
            'sort' => [
182
                'defaultOrder' => ['created_at' => SORT_DESC],
183
            ],
184
        ]);
185
186
        return $this->render('stats', [
187
            'model' => $model,
188
            'dataProvider' => $dataProvider,
189
        ]);
190
    }
191
192
    public function actionMediaTags($id)
193
    {
194
        $model = $this->findModel($id);
195
196
        $dataProvider = new ActiveDataProvider([
197
            'query' => Tag::find()
198
                ->select([
199
                    'tag.*',
200
                    'count(tag.id) as occurs',
201
                ])
202
                ->innerJoinWith(['media' => function (Query $q) use ($model) {
203
                    $q->andWhere(['media.account_id' => $model->id]);
204
                }])
205
                ->groupBy('tag.id'),
206
        ]);
207
208
        $dataProvider->sort->attributes['occurs'] = [
209
            'asc' => ['occurs' => SORT_ASC],
210
            'desc' => ['occurs' => SORT_DESC],
211
        ];
212
        $dataProvider->sort->defaultOrder = [
213
            'occurs' => SORT_DESC,
214
            'name' => SORT_ASC,
215
        ];
216
217
218
        return $this->render('media-tags', [
219
            'model' => $model,
220
            'dataProvider' => $dataProvider,
221
        ]);
222
    }
223
224
    public function actionMediaAccounts($id)
225
    {
226
        $model = $this->findModel($id);
227
228
        $dataProvider = new ActiveDataProvider([
229
            'query' => Account::find()
230
                ->select([
231
                    'account.*',
232
                    'count(account.id) as occurs',
233
                ])
234
                ->innerJoinWith(['mediaAccounts.media' => function (Query $q) use ($model) {
235
                    $q->andWhere(['media.account_id' => $model->id]);
236
                }])
237
                ->groupBy('account.id'),
238
        ]);
239
240
        $dataProvider->sort->attributes['occurs'] = [
241
            'asc' => ['occurs' => SORT_ASC],
242
            'desc' => ['occurs' => SORT_DESC],
243
        ];
244
        $dataProvider->sort->defaultOrder = [
245
            'occurs' => SORT_DESC,
246
            'username' => SORT_ASC,
247
        ];
248
249
250
        return $this->render('media-accounts', [
251
            'model' => $model,
252
            'dataProvider' => $dataProvider,
253
        ]);
254
    }
255
256
257
    /**
258
     * Finds the Account model based on its primary key value.
259
     * If the model is not found, a 404 HTTP exception will be thrown.
260
     *
261
     * @param integer $id
262
     * @return Account the loaded model
263
     * @throws NotFoundHttpException if the model cannot be found
264
     */
265
    public function findModel($id)
266
    {
267
        if (($model = Account::findOne($id)) !== null) {
268
            return $model;
269
        } else {
270
            throw new NotFoundHttpException('The requested page does not exist.');
271
        }
272
    }
273
}
274