Completed
Push — master ( 9eb8f3...27c704 )
by vistart
05:33
created

UpdateAction   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 7
dl 0
loc 64
rs 10
c 0
b 0
f 0

4 Methods

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