Completed
Push — master ( 6f2475...9a35ef )
by Nate
16:01
created

OrganizationsController::behaviors()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 35
ccs 0
cts 35
cp 0
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 22
nc 1
nop 0
crap 2
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;
10
11
use Craft;
12
use craft\helpers\ArrayHelper;
13
use flipbox\organizations\actions\organizations\Create;
14
use flipbox\organizations\actions\organizations\Delete;
15
use flipbox\organizations\actions\organizations\Update;
16
use flipbox\organizations\cp\actions\organization\SwitchType;
17
18
/**
19
 * @author Flipbox Factory <[email protected]>
20
 * @since 1.0.0
21
 */
22
class OrganizationsController 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' => 'organization'
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', "Organization successfully created."),
47
                            401 => Craft::t('organizations', "Failed to create organization.")
48
                        ],
49
                        'update' => [
50
                            200 => Craft::t('organizations', "Organization successfully updated."),
51
                            401 => Craft::t('organizations', "Failed to update organization.")
52
                        ],
53
                        'delete' => [
54
                            204 => Craft::t('organizations', "Organization successfully deleted."),
55
                            401 => Craft::t('organizations', "Failed to delete organization.")
56
                        ]
57
                    ]
58
                ]
59
            ]
60
        );
61
    }
62
63
    /**
64
     * @return array
65
     */
66
    protected function verbs(): array
67
    {
68
        return [
69
            'switch-type' => ['post'],
70
            'create' => ['post'],
71
            'update' => ['post', 'put'],
72
            'delete' => ['post', 'delete']
73
        ];
74
    }
75
76
    /**
77
     * @param null $organization
78
     * @return mixed
79
     * @throws \yii\base\InvalidConfigException
80
     */
81
    public function actionSwitchType($organization = null)
82
    {
83
        if (null === $organization) {
84
            $organization = Craft::$app->getRequest()->getBodyParam('organization');
85
        }
86
87
        /** @var SwitchType $action */
88
        $action = Craft::createObject([
89
            'class' => SwitchType::class,
90
            'checkAccess' => [$this, 'checkSwitchTypeAccess']
91
        ], [
92
            'switchType',
93
            $this
94
        ]);
95
96
        return $action->runWithParams([
97
            'organization' => $organization
98
        ]);
99
    }
100
101
    /**
102
     * @return mixed
103
     * @throws \yii\base\InvalidConfigException
104
     */
105
    public function actionCreate()
106
    {
107
        /** @var Create $action */
108
        $action = Craft::createObject([
109
            'class' => Create::class,
110
            'checkAccess' => [$this, 'checkCreateAccess']
111
        ], [
112
            'create',
113
            $this
114
        ]);
115
116
        return $action->runWithParams([]);
117
    }
118
119
    /**
120
     * @param string|int|null $organization
121
     * @return mixed
122
     * @throws \yii\base\InvalidConfigException
123
     */
124
    public function actionUpdate($organization = null)
125
    {
126
        if (null === $organization) {
127
            $organization = Craft::$app->getRequest()->getBodyParam('organization');
128
        }
129
130
        /** @var Update $action */
131
        $action = Craft::createObject([
132
            'class' => Update::class,
133
            'checkAccess' => [$this, 'checkUpdateAccess']
134
        ], [
135
            'update',
136
            $this
137
        ]);
138
139
        return $action->runWithParams([
140
            'organization' => $organization
141
        ]);
142
    }
143
144
    /**
145
     * @param string|int|null $organization
146
     * @return mixed
147
     * @throws \yii\base\InvalidConfigException
148
     */
149
    public function actionDelete($organization = null)
150
    {
151
        if (null === $organization) {
152
            $organization = Craft::$app->getRequest()->getBodyParam('organization');
153
        }
154
155
        /** @var Delete $action */
156
        $action = Craft::createObject([
157
            'class' => Delete::class,
158
            'checkAccess' => [$this, 'checkDeleteAccess']
159
        ], [
160
            'delete',
161
            $this
162
        ]);
163
164
        return $action->runWithParams([
165
            'organization' => $organization
166
        ]);
167
    }
168
169
    /**
170
     * @return bool
171
     */
172
    public function checkSwitchTypeAccess(): bool
173
    {
174
        return $this->checkAdminAccess();
175
    }
176
177
    /**
178
     * @return bool
179
     */
180
    public function checkCreateAccess(): bool
181
    {
182
        return $this->checkAdminAccess();
183
    }
184
185
    /**
186
     * @return bool
187
     */
188
    public function checkUpdateAccess(): bool
189
    {
190
        return $this->checkAdminAccess();
191
    }
192
193
    /**
194
     * @return bool
195
     */
196
    public function checkDeleteAccess(): bool
197
    {
198
        return $this->checkAdminAccess();
199
    }
200
201
    /**
202
     * @return bool
203
     */
204
    protected function checkAdminAccess()
205
    {
206
        $this->requireLogin();
207
        return Craft::$app->getUser()->getIsAdmin();
208
    }
209
}
210