Completed
Push — master ( 04ac75...81967b )
by Nate
17:06
created

TokensController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 5
dl 0
loc 180
ccs 0
cts 127
cp 0
rs 10
c 0
b 0
f 0

7 Methods

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