SetUpDepartmentAction   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 11
lcom 1
cbo 6
dl 0
loc 56
rs 10
c 1
b 0
f 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initMessages() 0 9 3
A init() 0 5 1
C run() 0 27 7
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\organization\controllers\my;
14
15
use rhosocial\organization\forms\SetUpForm;
16
use rhosocial\organization\rbac\permissions\SetUpDepartment;
17
use rhosocial\organization\web\organization\Module;
18
use Yii;
19
use yii\base\Action;
20
use yii\web\BadRequestHttpException;
21
use yii\web\UnauthorizedHttpException;
22
23
/**
24
 * @version 1.0
25
 * @author vistart <[email protected]>
26
 */
27
class SetUpDepartmentAction extends Action
28
{
29
    public $departmentSetUpSuccessMessage;
30
    public $departmentSetUpFailedMessage;
31
32
    protected function initMessages()
33
    {
34
        if (!is_string($this->departmentSetUpSuccessMessage)) {
35
            $this->departmentSetUpSuccessMessage = Yii::t('organization' ,'Department Set Up.');
36
        }
37
        if (!is_string($this->departmentSetUpFailedMessage)) {
38
            $this->departmentSetUpFailedMessage = Yii::t('organization', 'Department Set Up Failed.');
39
        }
40
    }
41
42
    public function init()
43
    {
44
        $this->initMessages();
45
        parent::init();
46
    }
47
48
    /**
49
     * Set up department.
50
     * @param string $parent Parent organization or department ID.
51
     * @return string the rendering result.
52
     * @throws BadRequestHttpException
53
     * @throws UnauthorizedHttpException
54
     */
55
    public function run($parent)
56
    {
57
        $model = new SetUpForm(['user' => Yii::$app->user->identity, 'parent' => $parent]);
58
        if (!$model->getParent()) {
59
            throw new BadRequestHttpException(Yii::t('organization', 'Parent Organization/Department Not Exist.'));
60
        }
61
        if (!Yii::$app->authManager->checkAccess(Yii::$app->user->identity, (new SetUpDepartment)->name, ['organization' => $model->getParent()])) {
62
            throw new UnauthorizedHttpException(Yii::t('organization', 'You do not have access to set up new department.'));
63
        }
64
        if ($model->load(Yii::$app->request->post())) {
65
            try {
66
                if (($result = $model->setUpDepartment()) === true) {
67
                    Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_SUCCESS);
68
                    Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, '(' . $model->getUser()->lastSetUpOrganization->getID() . ') ' . $this->departmentSetUpSuccessMessage);
69
                    return $this->controller->redirect(['index']);
0 ignored issues
show
Bug introduced by
The method redirect does only exist in yii\web\Controller, but not in yii\base\Controller.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
70
                }
71
                if ($result instanceof \Exception) {
72
                    throw $result;
73
                }
74
            } catch (\Exception $ex) {
75
                Yii::error($ex->getMessage(), __METHOD__);
76
                Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_FAILED);
77
                Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, $this->departmentSetUpFailedMessage);
78
            }
79
        }
80
        return $this->controller->render('set-up-organization', ['model' => $model]);
81
    }
82
}
83