UpdateAction::initMessages()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 4
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\exceptions\UnauthorizedManageProfileException;
16
use rhosocial\organization\Organization;
17
use rhosocial\organization\Profile;
18
use rhosocial\organization\rbac\permissions\ManageProfile;
19
use rhosocial\organization\web\organization\Module;
20
use rhosocial\user\User;
21
use Yii;
22
use yii\base\Action;
23
24
/**
25
 * Class UpdateAction
26
 * @package rhosocial\organization\web\organization\controllers\my
27
 * @version 1.0
28
 * @author vistart <[email protected]>
29
 */
30
class UpdateAction extends Action
31
{
32
    public $updateSuccessMessage;
33
    public $updateFailedMessage;
34
35
    /**
36
     * Initialize messages.
37
     */
38
    protected function initMessages()
39
    {
40
        if (!is_string($this->updateSuccessMessage)) {
41
            $this->updateSuccessMessage = Yii::t('user' ,'Updated.');
42
        }
43
        if (!is_string($this->updateFailedMessage)) {
44
            $this->updateFailedMessage = Yii::t('user', 'Failed to Update.');
45
        }
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function init()
52
    {
53
        $this->initMessages();
54
        parent::init();
55
    }
56
57
    /**
58
     * Check access.
59
     * @param Organization $org
60
     * @param User $user
61
     * @return bool
62
     * @throws UnauthorizedManageProfileException
63
     */
64
    public static function checkAccess($org, $user)
65
    {
66
        MemberAction::checkAccess($org, $user);
67
        if (!Yii::$app->authManager->checkAccess($user->getGUID(), (new ManageProfile)->name, ['organization' => $org])) {
68
            throw new UnauthorizedManageProfileException();
69
        }
70
        return true;
71
    }
72
73
    /**
74
     * Run action
75
     * @param string|integer $id
76
     * @return string
77
     */
78
    public function run($id)
79
    {
80
        $org = Module::getOrganization($id);
81
        $user = Yii::$app->user->identity;
82
        static::checkAccess($org, $user);
83
84
        $profile = $org->profile;
85
        if (!$profile) {
86
            $profile = $org->createProfile();
87
        }
88
        $profile->scenario = Profile::SCENARIO_UPDATE;
89
        if ($profile->load(Yii::$app->request->post())) {
90
            if ($profile->save()) {
91
                Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_SUCCESS);
92
                Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, '(' . $org->profile->name . ' ' . $org->getID() . ') ' . $this->updateSuccessMessage);
93
                return $this->controller->redirect(['update', 'id' => $org->getID()]);
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...
94
            }
95
            Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_FAILED);
96
            Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, '(' . $org->profile->name . ' ' . $org->getID() . ') ' . $this->updateFailedMessage);
97
        }
98
        return $this->controller->render('update', ['organization' => $org, 'model' => $profile]);
99
    }
100
}
101