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

SetUpDepartmentAction   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 51
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initMessages() 0 9 3
A init() 0 5 1
B run() 0 24 6
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\organization;
14
15
use rhosocial\organization\forms\SetUpForm;
16
use rhosocial\organization\web\user\controllers\OrganizationController;
17
use Yii;
18
use yii\base\Action;
19
20
/**
21
 * @version 1.0
22
 * @author vistart <[email protected]>
23
 */
24
class SetUpDepartmentAction extends Action
25
{
26
    public $departmentSetUpSuccessMessage;
27
    public $departmentSetUpFailedMessage;
28
29
    protected function initMessages()
30
    {
31
        if (!is_string($this->departmentSetUpSuccessMessage)) {
32
            $this->departmentSetUpSuccessMessage = Yii::t('organization' ,'Department Set Up.');
33
        }
34
        if (!is_string($this->departmentSetUpFailedMessage)) {
35
            $this->departmentSetUpFailedMessage = Yii::t('organization', 'Department Set Up Failed.');
36
        }
37
    }
38
39
    public function init()
40
    {
41
        $this->initMessages();
42
        parent::init();
43
    }
44
45
    /**
46
     * Set up department.
47
     * @param string $parent Parent organization or department ID.
48
     * @return string the rendering result.
49
     */
50
    public function run($parent)
51
    {
52
        $model = new SetUpForm(['user' => Yii::$app->user->identity, 'parent' => $parent]);
53
        if (!$model->getParent()) {
54
            throw new BadRequestHttpException(Yii::t('organization', 'Parent Organization/Department Not Exist.'));
55
        }
56
        if ($model->load(Yii::$app->request->post())) {
57
            try {
58
                if (($result = $model->setUpDepartment()) === true) {
59
                    Yii::$app->session->setFlash(OrganizationController::SESSION_KEY_RESULT, OrganizationController::RESULT_SUCCESS);
60
                    Yii::$app->session->setFlash(OrganizationController::SESSION_KEY_MESSAGE, '(' . $model->getUser()->lastSetUpOrganization->getID() . ') ' . $this->departmentSetUpSuccessMessage);
61
                    return $this->controller->redirect(['list']);
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...
62
                }
63
                if ($result instanceof \Exception) {
64
                    throw $result;
65
                }
66
            } catch (\Exception $ex) {
67
                Yii::error($ex->getMessage(), __METHOD__);
68
                Yii::$app->session->setFlash(OrganizationController::SESSION_KEY_RESULT, OrganizationController::RESULT_FAILED);
69
                Yii::$app->session->setFlash(OrganizationController::SESSION_KEY_MESSAGE, $this->departmentSetUpFailedMessage);
70
            }
71
        }
72
        return $this->controller->render($this->controller->viewBasePath . 'set-up-organization', ['model' => $model]);
73
    }
74
}
75