Completed
Push — master ( 9ed145...8e4a93 )
by vistart
04:04
created

RemoveMemberAction   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
lcom 1
cbo 8
dl 0
loc 63
rs 10
c 1
b 0
f 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initMessages() 0 9 3
A init() 0 5 1
A checkAccess() 0 11 2
A run() 0 16 3
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\NotMemberOfOrganizationException;
16
use rhosocial\organization\Member;
17
use rhosocial\organization\web\organization\Module;
18
use rhosocial\user\User;
19
use Yii;
20
use yii\base\Action;
21
22
/**
23
 * Class RemoveMemberAction
24
 * @package rhosocial\organization\web\organization\controllers\my
25
 * @version 1.0
26
 * @author vistart <[email protected]>
27
 */
28
class RemoveMemberAction extends Action
29
{
30
    public $removeMemberSuccessMessage;
31
    public $removeMemberFailedMessage;
32
33
    protected function initMessages()
34
    {
35
        if (!is_string($this->removeMemberSuccessMessage)) {
36
            $this->removeMemberSuccessMessage = Yii::t('organization' ,'Member Removed.');
37
        }
38
        if (!is_string($this->removeMemberFailedMessage)) {
39
            $this->removeMemberFailedMessage = Yii::t('organization', 'Failed to Remove.');
40
        }
41
    }
42
43
    public function init()
44
    {
45
        $this->initMessages();
46
        parent::init();
47
    }
48
49
    /**
50
     * Check access.
51
     * @param Organization $org
52
     * @param string|integer $id User ID. If access checking passed, it will be re-assigned with the User model.
53
     * @param User $user
54
     * @return boolean
55
     * @throws NotMemberOfOrganizationException
56
     */
57
    public static function checkAccess($org, &$id, $user)
58
    {
59
        AddMemberAction::checkAccess($org, $user);
60
        $member = Member::find()->organization($org)->user($id)->one();
61
        /* @var $member Member */
62
        if (!$member) {
63
            throw new NotMemberOfOrganizationException();
64
        }
65
        $id = $member->memberUser;
66
        return true;
67
    }
68
69
    /**
70
     * @param $org
71
     * @param $user
72
     * @return \yii\web\Response
73
     */
74
    public function run($org, $user)
75
    {
76
        $org = Module::getOrganization($org);
77
        $id = $user;
78
        if (!static::checkAccess($org, $user, Yii::$app->user->identity)) {
0 ignored issues
show
Documentation introduced by
$org is of type object<rhosocial\organization\Organization>, but the function expects a object<rhosocial\organiz...ollers\my\Organization>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
79
            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...
80
        }
81
        if ($org->removeMember($user)) {
0 ignored issues
show
Documentation introduced by
$user is of type string|integer, but the function expects a object<rhosocial\organiz...ct<rhosocial\user\User>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
82
            Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_SUCCESS);
83
            Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, '(' . $id . ') ' . $this->removeMemberSuccessMessage);
84
            return $this->controller->redirect(['member', 'org' => $org->getID()]);
85
        }
86
        Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_FAILED);
87
        Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, '(' . $id . ') ' . $this->removeMemberFailedMessage);
88
        return $this->controller->redirect(['member', 'org' => $org->getID()]);
89
    }
90
}
91