DefaultController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 7
eloc 29
c 3
b 0
f 1
dl 0
loc 70
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 0 10 1
A actionIndex() 0 24 3
A actionGetDemoData() 0 11 3
1
<?php
2
3
namespace modules\main\controllers\backend;
4
5
use Yii;
6
use yii\web\Controller;
7
use yii\filters\AccessControl;
8
use yii\web\NotFoundHttpException;
9
use yii\web\Response;
10
use modules\users\models\User;
11
use modules\rbac\models\Permission;
12
use modules\main\Module;
13
use backend\components\Demo;
14
15
/**
16
 * Class DefaultController
17
 * @package modules\main\controllers\backend
18
 */
19
class DefaultController extends Controller
20
{
21
    /**
22
     * @inheritdoc
23
     * @return array
24
     */
25
    public function behaviors()
26
    {
27
        return [
28
            'access' => [
29
                'class' => AccessControl::class,
30
                'rules' => [
31
                    [
32
                        'actions' => ['index', 'get-demo-data'],
33
                        'allow' => true,
34
                        'roles' => [Permission::PERMISSION_VIEW_ADMIN_PAGE]
35
                    ]
36
                ]
37
            ]
38
        ];
39
    }
40
41
    /**
42
     * Displays homepage.
43
     * @return mixed|Response
44
     */
45
    public function actionIndex()
46
    {
47
        /** @var yii\web\User $user */
48
        $user = Yii::$app->user;
49
        if (!$user->can(Permission::PERMISSION_VIEW_ADMIN_PAGE)) {
50
            /** @var yii\web\Session $session */
51
            $session = Yii::$app->session;
52
            $session->setFlash('error', Module::translate('module', 'You are not allowed access!'));
53
            return $this->goHome();
54
        }
55
        //Greeting in the admin panel :)
56
        /** @var yii\web\Session $session */
57
        $session = Yii::$app->session;
58
        $key = 'msgHello';
59
        if ($session->get($key) !== 1) {
60
            /** @var User $identity */
61
            $identity = Yii::$app->user->identity;
62
            $session->setFlash('info', Module::translate('module', 'Welcome, {:username}!', [
63
                ':username' => $identity->username
64
            ]));
65
            $session->set($key, 1);
66
        }
67
        return $this->render('index', [
68
                'usersCount' => User::find()->count()
69
            ]);
70
    }
71
72
    /**
73
     * Get Demo Data
74
     *
75
     * @return array
76
     * @throws NotFoundHttpException
77
     */
78
    public function actionGetDemoData()
79
    {
80
        if (Yii::$app->request->isAjax) {
81
            Yii::$app->response->format = Response::FORMAT_JSON;
82
            $post = Yii::$app->request->post();
83
            $data = (!empty($post['data'])) ? $post['data'] : [];
84
            return [
85
                'result' => Demo::getRandomData($data),
86
            ];
87
        }
88
        throw new NotFoundHttpException('The requested page does not exist.');
89
    }
90
}
91