Completed
Push — master ( b96e2f...be9b65 )
by vistart
19:04
created

SettingsAction::initMessages()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
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\forms\SettingsForm;
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 SettingsAction
24
 * @package rhosocial\organization\web\organization\controllers\my
25
 * @version 1.0
26
 * @author vistart <[email protected]>
27
 */
28
class SettingsAction extends Action
29
{
30
    public $updateSuccessMessage;
31
    public $updateFailedMessage;
32
33
    /**
34
     * Initialize messages.
35
     */
36
    protected function initMessages()
37
    {
38
        if (!is_string($this->updateSuccessMessage)) {
39
            $this->updateSuccessMessage = Yii::t('user' ,'Updated.');
40
        }
41
        if (!is_string($this->updateFailedMessage)) {
42
            $this->updateFailedMessage = Yii::t('user', 'Failed to Update.');
43
        }
44
    }
45
46
    public function init()
47
    {
48
        $this->initMessages();
49
        parent::init();
50
    }
51
52
    /**
53
     * Check access.
54
     * @param $org
55
     * @param $user
56
     * @return bool
57
     * @throws UnauthorizedManageProfileException
58
     */
59
    public static function checkAccess($org, $user)
60
    {
61
        MemberAction::checkAccess($org, $user);
62
        if (!Yii::$app->authManager->checkAccess($user->getGUID(), (new ManageProfile)->name, ['organization' => $org])) {
63
            throw new UnauthorizedManageProfileException();
64
        }
65
        return true;
66
    }
67
68
    /**
69
     * @return string
70
     */
71
    public function run($id)
72
    {
73
        $organization = Module::getOrganization($id);
74
        $user = Yii::$app->user->identity;
75
        static::checkAccess($organization, $user);
76
        $model = new SettingsForm([
77
            'organization' => $organization,
78
        ]);
79
        if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
80
            if ($model->validate() && $model->submit()) {
81
                Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_SUCCESS);
82
                Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, '(' . $organization->profile->name . ' ' . $organization->getID() . ') ' . $this->updateSuccessMessage);
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<rhosocial\organization\Profile>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
83
                return $this->controller->redirect(['settings', 'id' => $organization->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...
84
            }
85
            Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_FAILED);
86
            Yii::$app->session->setFlash(Module::SESSION_KEY_MESSAGE, '(' . $organization->profile->name . ' ' . $organization->getID() . ') ' . $this->updateFailedMessage);
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<rhosocial\organization\Profile>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
87
        }
88
        return $this->controller->render('settings', [
89
            'organization' => $organization,
90
            'model' => $model
91
        ]);
92
    }
93
}
94