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

RevokeAction::run()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 12
nc 4
nop 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\organization\controllers\my;
14
15
use rhosocial\organization\exceptions\RevokePreventedException;
16
use rhosocial\organization\web\organization\Module;
17
use Yii;
18
use yii\base\Action;
19
use yii\base\InvalidParamException;
20
use yii\web\BadRequestHttpException;
21
use yii\web\ServerErrorHttpException;
22
23
/**
24
 * @version 1.0
25
 * @author vistart <[email protected]>
26
 */
27
class RevokeAction extends Action
28
{
29
    public $organizationRevokeSuccessMessage;
30
    public $organizationRevokeFailedMessage;
31
32
    protected function initMessages()
33
    {
34
        if (!is_string($this->organizationRevokeSuccessMessage)) {
35
            $this->organizationRevokeSuccessMessage = Yii::t('organization', 'Successfully revoked.');
36
        }
37
        if (!is_string($this->organizationRevokeFailedMessage)) {
38
            $this->organizationRevokeFailedMessage = Yii::t('organization', 'Failed to revoke.');
39
        }
40
    }
41
42
    public function init()
43
    {
44
        $this->initMessages();
45
        parent::init();
46
    }
47
    /**
48
     * Revoke organization or department.
49
     * @param string|integer $id
50
     * @throws ServerErrorHttpException
51
     */
52
    public function run($id)
53
    {
54
        try {
55
            Yii::$app->user->identity->revokeOrganization($id, true);
56
        } catch (InvalidParamException $ex) {
57
            throw new BadRequestHttpException(Yii::t('organization', $ex->getMessage()));
58
        } catch (RevokePreventedException $ex) {
59
            throw new BadRequestHttpException(Yii::t('organization', $ex->getMessage()));
60
        } catch (\Exception $ex) {
61
            throw new ServerErrorHttpException($ex->getMessage());
62
        }
63
        Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_SUCCESS);
64
        Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, "($id) " . $this->organizationRevokeSuccessMessage);
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
    }
67
}
68