Passed
Push — master ( 02c72e...05e7db )
by vistart
04:22
created

OrganizationController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 12
c 2
b 0
f 1
lcom 1
cbo 4
dl 0
loc 117
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B getOrganization() 0 17 7
A actions() 0 23 1
B behaviors() 0 35 1
A actionIndex() 0 4 1
A actionView() 0 7 1
A actionUpdate() 0 4 1
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\organization\web\user\controllers;
14
15
use rhosocial\organization\forms\SetUpForm;
16
use rhosocial\organization\Organization;
17
use Yii;
18
use yii\filters\AccessControl;
19
use yii\filters\VerbFilter;
20
use yii\web\BadRequestHttpException;
21
use yii\web\Controller;
22
use yii\web\UnauthorizedHttpException;
23
24
/**
25
 * Organization Controller, designed for user module.
26
 *
27
 * @version 1.0
28
 * @author vistart <[email protected]>
29
 */
30
class OrganizationController extends Controller
31
{
32
    public $layout = '@rhosocial/organization/web/user/views/layouts/organization';
33
    const RESULT_SUCCESS = 'success';
34
    const RESULT_FAILED = 'failed';
35
    const SESSION_KEY_MESSAGE = 'session_key_message';
36
    const SESSION_KEY_RESULT = 'session_key_result';
37
38
    public $viewBasePath = '@rhosocial/organization/web/user/views/organization/';
39
40
    /**
41
     * Get organization by specific parameter.
42
     * @param Organization|string|integer $organization
43
     * @return Organization
44
     */
45
    public function getOrganization($organization)
46
    {
47
        if (!$organization) {
48
            return null;
49
        }
50
        $class = Yii::$app->user->identity->organizationClass;
51
        if ($organization instanceof $class) {
52
            $organization = $organization->getID();
53
        }
54
        if (is_numeric($organization) || is_int($organization)) {
55
            return $class::find()->id($organization)->one();
56
        }
57
        if (is_string($organization) && strlen($organization) == 16) {
58
            return $class::find()->guid($organization)->one();
59
        }
60
        return null;
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66
    public function actions()
67
    {
68
        return [
69
            'add-new-member' => [
70
                'class' => 'rhosocial\organization\web\user\controllers\organization\AddNewMemberAction',
71
            ],
72
            'list' => [
73
                'class' => 'rhosocial\organization\web\user\controllers\organization\ListAction',
74
            ],
75
            'revoke' => [
76
                'class' => 'rhosocial\organization\web\user\controllers\organization\RevokeAction',
77
            ],
78
            'view-members' => [
79
                'class' => 'rhosocial\organization\web\user\controllers\organization\ViewMembersAction',
80
            ],
81
            'set-up-organization' => [
82
                'class' => 'rhosocial\organization\web\user\controllers\organization\SetUpOrganizationAction',
83
            ],
84
            'set-up-department' => [
85
                'class' => 'rhosocial\organization\web\user\controllers\organization\SetUpDepartmentAction',
86
            ],
87
        ];
88
    }
89
90
    /**
91
     * @inheritdoc
92
     */
93
    public function behaviors()
94
    {
95
        return [
96
            'access' => [
97
                'class' => AccessControl::class,
98
                'rules' => [
99
                    [ // Disallow all unauthorized users to access this controller.
100
                        'allow' => false,
101
                        'roles' => ['?'],
102
                    ],
103
                    [ // Disallow user who does not have `setUpOrganization` permission to access this `set-up-organization` action.
104
                        'actions' => ['set-up-organization'],
105
                        'allow' => false,
106
                        'matchCallback' => function ($rule, $action) {
0 ignored issues
show
Unused Code introduced by
The parameter $rule is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $action is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
107
                            return !Yii::$app->user->can('setUpOrganization');
108
                        },
109
                        'denyCallback' => function ($rule, $action) {
0 ignored issues
show
Unused Code introduced by
The parameter $rule is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $action is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
110
                            throw new UnauthorizedHttpException(Yii::t('organization', 'You do not have access to set up new organization.'));
111
                        },
112
                    ],
113
                    [
114
                        'allow' => true,
115
                        'roles' => ['@'],
116
                    ]
117
                ],
118
            ],
119
            'verbs' => [
120
                'class' => VerbFilter::class,
121
                'actions' => [
122
                    'deregister' => ['post'],
123
                    'revoke' => ['post'],
124
                ],
125
            ],
126
        ];
127
    }
128
129
    public function actionIndex()
130
    {
131
        return $this->render($this->viewBasePath . 'index');
132
    }
133
134
    public function actionView($id)
135
    {
136
        $user = Yii::$app->user->identity;
137
        $organization = $user->getAtOrganizations()->id($id)->one();
138
        $profile = $organization->profile;
0 ignored issues
show
Unused Code introduced by
$profile is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
139
        return $this->render($this->viewBasePath . 'view');
140
    }
141
142
    public function actionUpdate($id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
143
    {
144
        return $this->render($this->viewBasePath . 'update');
145
    }
146
}
147