SettingsAction::run()   C
last analyzed

Complexity

Conditions 7
Paths 5

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 29
rs 6.7272
cc 7
eloc 21
nc 5
nop 1
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
use yii\bootstrap\ActiveForm;
22
use yii\web\Response;
23
24
/**
25
 * Class SettingsAction
26
 * @package rhosocial\organization\web\organization\controllers\my
27
 * @version 1.0
28
 * @author vistart <[email protected]>
29
 */
30
class SettingsAction 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
    public function init()
49
    {
50
        $this->initMessages();
51
        parent::init();
52
    }
53
54
    /**
55
     * Check access.
56
     * @param $org
57
     * @param $user
58
     * @return bool
59
     * @throws UnauthorizedManageProfileException
60
     */
61
    public static function checkAccess($org, $user)
62
    {
63
        MemberAction::checkAccess($org, $user);
64
        if (!Yii::$app->authManager->checkAccess($user->getGUID(), (new ManageProfile)->name, ['organization' => $org])) {
65
            throw new UnauthorizedManageProfileException();
66
        }
67
        return true;
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    public function run($id)
74
    {
75
        $organization = Module::getOrganization($id);
76
        $user = Yii::$app->user->identity;
77
        static::checkAccess($organization, $user);
78
        $model = new SettingsForm([
79
            'organization' => $organization,
80
        ]);
81
        if (Yii::$app->request->isAjax) {
82
            if ($model->load(Yii::$app->request->post())) {
83
                Yii::$app->response->format = Response::FORMAT_JSON;
84
                return ActiveForm::validate($model);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \yii\bootstrap\Ac...Form::validate($model); (array) is incompatible with the return type documented by rhosocial\organization\w...\my\SettingsAction::run of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
85
            }
86
            return;
87
        }
88
        if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
89
            if ($model->validate() && $model->submit()) {
90
                Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_SUCCESS);
91
                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...
92
                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...
93
            }
94
            Yii::$app->session->setFlash(Module::SESSION_KEY_RESULT, Module::RESULT_FAILED);
95
            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...
96
        }
97
        return $this->controller->render('settings', [
98
            'organization' => $organization,
99
            'model' => $model
100
        ]);
101
    }
102
}
103