OrganizationTypesController   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 0
loc 156
ccs 0
cts 104
cp 0
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 0 35 1
A verbs() 0 8 1
A actionCreate() 0 15 1
A actionUpdate() 0 19 2
A actionDelete() 0 19 2
A checkCreateAccess() 0 4 1
A checkUpdateAccess() 0 4 1
A checkDeleteAccess() 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\actions\organizations\CreateOrganizationType;
14
use flipbox\organizations\actions\organizations\DeleteOrganizationType;
15
use flipbox\organizations\actions\organizations\UpdateOrganizationType;
16
use flipbox\organizations\cp\controllers\AbstractController;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
class OrganizationTypesController extends AbstractController
23
{
24
    /**
25
     * @return array
26
     */
27
    public function behaviors()
28
    {
29
        return ArrayHelper::merge(
30
            parent::behaviors(),
31
            [
32
                'error' => [
33
                    'default' => 'type'
34
                ],
35
                'redirect' => [
36
                    'only' => ['create', 'update', 'delete'],
37
                    'actions' => [
38
                        'create' => [201],
39
                        'update' => [200],
40
                        'delete' => [204],
41
                    ]
42
                ],
43
                'flash' => [
44
                    'actions' => [
45
                        'create' => [
46
                            201 => Craft::t('organizations', "Type successfully created."),
47
                            401 => Craft::t('organizations', "Failed to create type.")
48
                        ],
49
                        'update' => [
50
                            200 => Craft::t('organizations', "Type successfully updated."),
51
                            401 => Craft::t('organizations', "Failed to update type.")
52
                        ],
53
                        'delete' => [
54
                            204 => Craft::t('organizations', "Type successfully deleted."),
55
                            401 => Craft::t('organizations', "Failed to delete type.")
56
                        ]
57
                    ]
58
                ]
59
            ]
60
        );
61
    }
62
63
    /**
64
     * @return array
65
     */
66
    protected function verbs(): array
67
    {
68
        return [
69
            'create' => ['post'],
70
            'update' => ['post', 'put'],
71
            'delete' => ['post', 'delete']
72
        ];
73
    }
74
75
    /**
76
     * @return mixed
77
     * @throws \yii\base\InvalidConfigException
78
     */
79
    public function actionCreate()
80
    {
81
        /** @var CreateOrganizationType $action */
82
        $action = Craft::createObject([
83
            'class' => CreateOrganizationType::class,
84
            'checkAccess' => [$this, 'checkCreateAccess']
85
        ], [
86
            'create',
87
            $this
88
        ]);
89
90
        $response = $action->runWithParams([]);
91
92
        return $response;
93
    }
94
95
    /**
96
     * @param string|int|null $type
97
     * @return mixed
98
     * @throws \yii\base\InvalidConfigException
99
     */
100
    public function actionUpdate($type = null)
101
    {
102
        if (null === $type) {
103
            $type = Craft::$app->getRequest()->getBodyParam('type');
104
        }
105
106
        /** @var UpdateOrganizationType $action */
107
        $action = Craft::createObject([
108
            'class' => UpdateOrganizationType::class,
109
            'checkAccess' => [$this, 'checkUpdateAccess']
110
        ], [
111
            'update',
112
            $this
113
        ]);
114
115
        return $action->runWithParams([
116
            'type' => $type
117
        ]);
118
    }
119
120
    /**
121
     * @param string|int|null $type
122
     * @return mixed
123
     * @throws \yii\base\InvalidConfigException
124
     */
125
    public function actionDelete($type = null)
126
    {
127
        if (null === $type) {
128
            $type = Craft::$app->getRequest()->getBodyParam('type');
129
        }
130
131
        /** @var DeleteOrganizationType $action */
132
        $action = Craft::createObject([
133
            'class' => DeleteOrganizationType::class,
134
            'checkAccess' => [$this, 'checkDeleteAccess']
135
        ], [
136
            'delete',
137
            $this
138
        ]);
139
140
        return $action->runWithParams([
141
            'type' => $type
142
        ]);
143
    }
144
145
    /**
146
     * @return bool
147
     */
148
    public function checkCreateAccess(): bool
149
    {
150
        return $this->checkAdminAccess();
151
    }
152
153
    /**
154
     * @return bool
155
     */
156
    public function checkUpdateAccess(): bool
157
    {
158
        return $this->checkAdminAccess();
159
    }
160
161
    /**
162
     * @return bool
163
     */
164
    public function checkDeleteAccess(): bool
165
    {
166
        return $this->checkAdminAccess();
167
    }
168
169
    /**
170
     * @return bool
171
     */
172
    protected function checkAdminAccess()
173
    {
174
        $this->requireLogin();
175
        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...
176
    }
177
}
178