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

AccountController::actionDashboard()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

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