OrganizationsController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 7
dl 0
loc 143
ccs 0
cts 99
cp 0
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 0 35 1
A verbs() 0 9 1
A actionSwitchType() 0 18 2
A actionCreate() 0 12 1
A actionUpdate() 0 18 2
A actionDelete() 0 18 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\CreateOrganization;
14
use flipbox\organizations\actions\organizations\DeleteOrganization;
15
use flipbox\organizations\actions\organizations\UpdateOrganization;
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
        ], [
91
            'switchType',
92
            $this
93
        ]);
94
95
        return $action->runWithParams([
96
            'organization' => $organization
97
        ]);
98
    }
99
100
    /**
101
     * @return mixed
102
     * @throws \yii\base\InvalidConfigException
103
     */
104
    public function actionCreate()
105
    {
106
        /** @var CreateOrganization $action */
107
        $action = Craft::createObject([
108
            'class' => CreateOrganization::class
109
        ], [
110
            'create',
111
            $this
112
        ]);
113
114
        return $action->runWithParams([]);
115
    }
116
117
    /**
118
     * @param string|int|null $organization
119
     * @return mixed
120
     * @throws \yii\base\InvalidConfigException
121
     */
122
    public function actionUpdate($organization = null)
123
    {
124
        if (null === $organization) {
125
            $organization = Craft::$app->getRequest()->getBodyParam('organization');
126
        }
127
128
        /** @var UpdateOrganization $action */
129
        $action = Craft::createObject([
130
            'class' => UpdateOrganization::class
131
        ], [
132
            'update',
133
            $this
134
        ]);
135
136
        return $action->runWithParams([
137
            'organization' => $organization
138
        ]);
139
    }
140
141
    /**
142
     * @param string|int|null $organization
143
     * @return mixed
144
     * @throws \yii\base\InvalidConfigException
145
     */
146
    public function actionDelete($organization = null)
147
    {
148
        if (null === $organization) {
149
            $organization = Craft::$app->getRequest()->getBodyParam('organization');
150
        }
151
152
        /** @var DeleteOrganization $action */
153
        $action = Craft::createObject([
154
            'class' => DeleteOrganization::class
155
        ], [
156
            'delete',
157
            $this
158
        ]);
159
160
        return $action->runWithParams([
161
            'organization' => $organization
162
        ]);
163
    }
164
}
165