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

SetUpOrganizationAction   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 43
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initMessages() 0 9 3
A init() 0 5 1
B run() 0 21 5
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\web\organization\Module;
17
use Yii;
18
use yii\base\Action;
19
20
/**
21
 * @version 1.0
22
 * @author vistart <[email protected]>
23
 */
24
class SetUpOrganizationAction extends Action
25
{
26
    public $organizationSetUpSuccessMessage;
27
    public $organizationSetUpFailedMessage;
28
29
    protected function initMessages()
30
    {
31
        if (!is_string($this->organizationSetUpSuccessMessage)) {
32
            $this->organizationSetUpSuccessMessage = Yii::t('organization' ,'Organization Set Up.');
33
        }
34
        if (!is_string($this->organizationSetUpFailedMessage)) {
35
            $this->organizationSetUpFailedMessage = Yii::t('organization', 'Organization Set Up Failed.');
36
        }
37
    }
38
39
    public function init()
40
    {
41
        $this->initMessages();
42
        parent::init();
43
    }
44
45
    public function run()
46
    {
47
        $model = new SetUpForm(['user' => Yii::$app->user->identity]);
48
        if ($model->load(Yii::$app->request->post())) {
49
            try {
50
                if (($result = $model->setUpOrganization()) === true) {
51
                    Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_SUCCESS);
52
                    Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, '(' . $model->getUser()->lastSetUpOrganization->getID() . ') ' . $this->organizationSetUpSuccessMessage);
53
                    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...
54
                }
55
                if ($result instanceof \Exception) {
56
                    throw $result;
57
                }
58
            } catch (\Exception $ex) {
59
                Yii::error($ex->getMessage(), __METHOD__);
60
                Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_FAILED);
61
                Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, $this->organizationSetUpFailedMessage);
62
            }
63
        }
64
        return $this->controller->render('set-up-organization', ['model' => $model]);
65
    }
66
}
67