ProvidersController::behaviors()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 0
cts 45
cp 0
rs 9.2
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace flipbox\patron\cp\controllers;
4
5
use Craft;
6
use craft\helpers\ArrayHelper;
7
use flipbox\patron\actions\provider\CreateProvider;
8
use flipbox\patron\actions\provider\DeleteProvider;
9
use flipbox\patron\actions\provider\DisableProvider;
10
use flipbox\patron\actions\provider\EnableProvider;
11
use flipbox\patron\actions\provider\UpdateProvider;
12
13
class ProvidersController extends AbstractController
14
{
15
    /**
16
     * @return array
17
     */
18
    public function behaviors()
19
    {
20
        return ArrayHelper::merge(
21
            parent::behaviors(),
22
            [
23
                'error' => [
24
                    'default' => 'provider'
25
                ],
26
                'redirect' => [
27
                    'only' => ['create', 'update', 'delete', 'enable', 'disable'],
28
                    'actions' => [
29
                        'create' => [201],
30
                        'update' => [200],
31
                        'delete' => [204],
32
                        'enable' => [200],
33
                        'disable' => [200]
34
                    ]
35
                ],
36
                'flash' => [
37
                    'actions' => [
38
                        'create' => [
39
                            201 => Craft::t('patron', "Provider successfully created."),
40
                            400 => Craft::t('patron', "Failed to create provider.")
41
                        ],
42
                        'update' => [
43
                            200 => Craft::t('patron', "Provider successfully updated."),
44
                            400 => Craft::t('patron', "Failed to update provider.")
45
                        ],
46
                        'delete' => [
47
                            204 => Craft::t('patron', "Provider successfully deleted."),
48
                            400 => Craft::t('patron', "Failed to delete provider.")
49
                        ],
50
                        'enable' => [
51
                            200 => Craft::t('patron', "Provider successfully enabled."),
52
                            400 => Craft::t('patron', "Failed to enabled provider.")
53
                        ],
54
                        'disable' => [
55
                            200 => Craft::t('patron', "Provider successfully disable."),
56
                            400 => Craft::t('patron', "Failed to disable provider.")
57
                        ],
58
                    ]
59
                ]
60
            ]
61
        );
62
    }
63
64
    /**
65
     * @return array
66
     */
67
    protected function verbs(): array
68
    {
69
        return [
70
            'index' => ['get'],
71
            'view' => ['get'],
72
            'create' => ['post'],
73
            'update' => ['post', 'put'],
74
            'enable' => ['post'],
75
            'disable' => ['post'],
76
            'delete' => ['post', 'delete']
77
        ];
78
    }
79
80
    /**
81
     * @return array
82
     */
83
    public function actions()
84
    {
85
        return [
86
            'create' => [
87
                'class' => CreateProvider::class,
88
                'checkAccess' => [$this, 'checkAdminChanges']
89
            ]
90
        ];
91
    }
92
93
    /**
94
     * @param string|int|null $provider
95
     * @return mixed
96
     * @throws \yii\base\InvalidConfigException
97
     * @throws \yii\web\BadRequestHttpException
98
     */
99
    public function actionUpdate($provider = null)
100
    {
101
        if ($provider === null) {
102
            $provider = Craft::$app->getRequest()->getRequiredBodyParam('provider');
103
        }
104
105
        $action = Craft::createObject([
106
            'class' => UpdateProvider::class,
107
            'checkAccess' => [$this, 'checkAdminChanges']
108
        ], [
109
            'update',
110
            $this
111
        ]);
112
113
        return $action->runWithParams([
114
            'provider' => $provider
115
        ]);
116
    }
117
118
    /**
119
     * @param int|null $provider
120
     * @return mixed
121
     * @throws \yii\base\InvalidConfigException
122
     * @throws \yii\web\BadRequestHttpException
123
     */
124
    public function actionDelete(int $provider = null)
125
    {
126
        if ($provider === null) {
127
            $provider = Craft::$app->getRequest()->getRequiredBodyParam('provider');
128
        }
129
130
        $action = Craft::createObject([
131
            'class' => DeleteProvider::class,
132
            'checkAccess' => [$this, 'checkAdminChanges']
133
        ], [
134
            'delete',
135
            $this
136
        ]);
137
138
        return $action->runWithParams([
139
            'provider' => $provider
140
        ]);
141
    }
142
143
    /**
144
     * @param int|null $provider
145
     * @return mixed
146
     * @throws \yii\base\InvalidConfigException
147
     * @throws \yii\web\BadRequestHttpException
148
     */
149
    public function actionEnable(int $provider = null)
150
    {
151
        if ($provider === null) {
152
            $provider = Craft::$app->getRequest()->getRequiredBodyParam('provider');
153
        }
154
155
        $action = Craft::createObject([
156
            'class' => EnableProvider::class,
157
            'checkAccess' => [$this, 'checkAdminChanges']
158
        ], [
159
            'enable',
160
            $this
161
        ]);
162
163
        return $action->runWithParams([
164
            'provider' => $provider
165
        ]);
166
    }
167
168
    /**
169
     * @param int|null $provider
170
     * @return mixed
171
     * @throws \yii\base\InvalidConfigException
172
     * @throws \yii\web\BadRequestHttpException
173
     */
174
    public function actionDisable(int $provider = null)
175
    {
176
        if ($provider === null) {
177
            $provider = Craft::$app->getRequest()->getRequiredBodyParam('provider');
178
        }
179
180
        $action = Craft::createObject([
181
            'class' => DisableProvider::class,
182
            'checkAccess' => [$this, 'checkAdminChanges']
183
        ], [
184
            'disable',
185
            $this
186
        ]);
187
188
        return $action->runWithParams([
189
            'provider' => $provider
190
        ]);
191
    }
192
}
193