Passed
Push — master ( 00c958...ca3e5a )
by vistart
04:09
created

AddMemberAction::addMember()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
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\UnauthorizedManageMemberException;
16
use rhosocial\organization\Organization;
17
use rhosocial\organization\rbac\permissions\ManageMember;
18
use rhosocial\organization\web\organization\Module;
19
use rhosocial\user\User;
20
use Yii;
21
use yii\base\Action;
22
use yii\web\ServerErrorHttpException;
23
24
/**
25
 *
26
 * @version 1.0
27
 * @author vistart <[email protected]>
28
 */
29
class AddMemberAction extends Action
30
{
31
    /**
32
     * Check access.
33
     * @param Organization $org
34
     * @param User $user
35
     * @return boolean
36
     * @throws UnauthorizedManageMemberException
37
     */
38
    public static function checkAccess($org, $user)
39
    {
40
        MemberAction::checkAccess($org, $user);
41
        if (!Yii::$app->authManager->checkAccess($user->getGUID(), (new ManageMember)->name, ['organization' => $org])) {
42
            throw new UnauthorizedManageMemberException();
43
        }
44
        return true;
45
    }
46
47
    /**
48
     * Add member.
49
     * @param Organization $org
50
     * @param User|string|integer $user
51
     * @return boolean
52
     */
53
    protected function addMember($org, &$user)
54
    {
55
        return $org->addMember($user);
56
    }
57
58
    public function run($org, $u = null)
59
    {
60
        $organization = Module::getOrganization($org);
61
        $user = Yii::$app->user->identity;
62
        static::checkAccess($organization, $user);
63
64
        // If $u is not empty and the method is Post, it is considered to be a adding member operation.
65
        if (!empty($u) && Yii::$app->request->isPost) {
66
            $member = $u;
67
            if ($this->addMember($organization, $member)) {
68
                Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_SUCCESS);
69
                Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, "($u) " . Yii::t('organization', 'Member added.'));
70
                return $this->controller->redirect(['add-member', 'org' => $org]);
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...
71
            }
72
            Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_FAILED);
73
            Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, Yii::t('organization', 'Failed to add member.'));
74
            return $this->controller->redirect(['add-member','org' => $org]);
75
        }
76
        if (!class_exists($this->controller->userProfileSearchClass)) {
77
            throw new ServerErrorHttpException('Unknown User Profile View.');
78
        }
79
        $class = $this->controller->userProfileSearchClass;
80
        $searchModel = new $class();
81
        $dataProvider = $searchModel->search(Yii::$app->request->post());
82
        return $this->controller->render('add-member', [
83
            'organization' => $organization,
84
            'dataProvider' => $dataProvider,
85
            'searchModel' => $searchModel
86
        ]);
87
    }
88
}
89