GeneralController   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 4
dl 0
loc 76
ccs 0
cts 51
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 0 25 1
A verbs() 0 6 1
A actionSave() 0 13 1
A checkUpdateAccess() 0 4 1
A checkAdminAccess() 0 5 1
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/organization/license
6
 * @link       https://www.flipboxfactory.com/software/organization/
7
 */
8
9
namespace flipbox\organizations\cp\controllers\settings;
10
11
use Craft;
12
use craft\helpers\ArrayHelper;
13
use flipbox\organizations\cp\actions\general\Update;
14
use flipbox\organizations\cp\controllers\AbstractController;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.0.0
19
 */
20
class GeneralController extends AbstractController
21
{
22
    /**
23
     * @return array
24
     */
25
    public function behaviors()
26
    {
27
        return ArrayHelper::merge(
28
            parent::behaviors(),
29
            [
30
                'error' => [
31
                    'default' => 'save'
32
                ],
33
                'redirect' => [
34
                    'only' => ['save'],
35
                    'actions' => [
36
                        'save' => [200]
37
                    ]
38
                ],
39
                'flash' => [
40
                    'actions' => [
41
                        'save' => [
42
                            200 => Craft::t('organizations', "Settings successfully updated."),
43
                            401 => Craft::t('organizations', "Failed to update settings.")
44
                        ]
45
                    ]
46
                ]
47
            ]
48
        );
49
    }
50
51
    /**
52
     * @return array
53
     */
54
    protected function verbs(): array
55
    {
56
        return [
57
            'save' => ['post', 'put']
58
        ];
59
    }
60
61
    /**
62
     * @return mixed
63
     * @throws \yii\base\InvalidConfigException
64
     */
65
    public function actionSave()
66
    {
67
        /** @var Update $action */
68
        $action = Craft::createObject([
69
            'class' => Update::class,
70
            'checkAccess' => [$this, 'checkUpdateAccess']
71
        ], [
72
            'update',
73
            $this
74
        ]);
75
76
        return $action->runWithParams([]);
77
    }
78
79
    /**
80
     * @return bool
81
     */
82
    public function checkUpdateAccess(): bool
83
    {
84
        return $this->checkAdminAccess();
85
    }
86
87
    /**
88
     * @return bool
89
     */
90
    protected function checkAdminAccess()
91
    {
92
        $this->requireLogin();
93
        return Craft::$app->getUser()->getIsAdmin();
0 ignored issues
show
Bug introduced by
The method getUser does only exist in yii\web\Application, but not in yii\console\Application.

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
}
96