Completed
Push — master ( fef881...1aed96 )
by vistart
20:35
created

ExitAction::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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\web\organization\Module;
16
use Yii;
17
use yii\base\Action;
18
use yii\web\UnauthorizedHttpException;
19
20
/**
21
 * Class ExitAction
22
 * @package rhosocial\organization\web\organization\controllers\my
23
 * @version 1.0
24
 * @author vistart <[email protected]>
25
 */
26
class ExitAction extends Action
27
{
28
    public $exitSuccessMessage;
29
    public $exitFailedMessage;
30
31
    /**
32
     * Initialize messages.
33
     */
34
    protected function initMessages()
35
    {
36
        if (!is_string($this->exitSuccessMessage) || empty($this->exitSuccessMessage)) {
37
            $this->exitSuccessMessage = Yii::t('organization', 'Exited.');
38
        }
39
        if (!is_string($this->exitFailedMessage) || empty($this->exitFailedMessage)) {
40
            $this->exitFailedMessage = Yii::t('organization', 'Failed to exit.');
41
        }
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function init()
48
    {
49
        $this->initMessages();
50
        parent::init();
51
    }
52
    /**
53
     * Run action
54
     * @param $id
55
     * @return \yii\web\Response
56
     * @throws UnauthorizedHttpException
57
     */
58
    public function run($id)
59
    {
60
        $organization = Module::getOrganization($id);
61
        try {
62
            if ($organization->removeMember(Yii::$app->user->identity)) {
63
                Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_SUCCESS);
64
                Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, $this->exitSuccessMessage);
65
                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...
66
            } else {
67
                Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_FAILED);
68
                Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, $this->exitFailedMessage);
69
            }
70
        } catch (\Exception $ex) {
71
            throw new UnauthorizedHttpException($ex->getMessage());
72
        }
73
        return $this->controller->redirect(['index']);
74
    }
75
}
76