Completed
Push — develop ( 856b14...45ab37 )
by Nate
17:44
created

TokensController::behaviors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 35

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 9.36
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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\cp\controllers\AbstractController;
11
use flipbox\patron\Patron;
12
13
class TokensController 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' => ['delete', 'enable', 'disable'],
28
                    'actions' => [
29
                        'delete' => [204],
30
                        'enable' => [200],
31
                        'disable' => [200]
32
                    ]
33
                ],
34
                'flash' => [
35
                    'actions' => [
36
                        'delete' => [
37
                            204 => Craft::t('patron', "Token successfully deleted."),
38
                            400 => Craft::t('patron', "Failed to delete token.")
39
                        ],
40
                        'enable' => [
41
                            200 => Craft::t('patron', "Token successfully enabled."),
42
                            400 => Craft::t('patron', "Failed to enabled token.")
43
                        ],
44
                        'disable' => [
45
                            200 => Craft::t('patron', "Token successfully disable."),
46
                            400 => Craft::t('patron', "Failed to disable token.")
47
                        ],
48
                    ]
49
                ]
50
            ]
51
        );
52
    }
53
54
    /**
55
     * @return array
56
     */
57
    public function verbs(): array
58
    {
59
        return [
60
            'enable' => ['post'],
61
            'disable' => ['post'],
62
            'delete' => ['post', 'delete']
63
        ];
64
    }
65
66
    /**
67
     * @param null $token
68
     * @return array
69
     * @throws \flipbox\ember\exceptions\NotFoundException
70
     */
71
    public function actionModal($token = null): array
72
    {
73
        if (null === $token) {
74
            $token = Craft::$app->getRequest()->getBodyParam('token');
75
        }
76
77
        $view = $this->getView();
78
        return [
79
            'html' => Patron::getInstance()->getSettings()->getTokenView()->render([
80
                'token' => Patron::getInstance()->manageTokens()->get($token)
81
            ]),
82
            'headHtml' => $view->getHeadHtml(),
83
            'footHtml' => $view->getBodyHtml()
84
        ];
85
    }
86
87
    /**
88
     * @param null $token
89
     * @return mixed
90
     * @throws \yii\base\InvalidConfigException
91
     * @throws \yii\web\BadRequestHttpException
92
     */
93
    public function actionDisable($token = null)
94
    {
95
        if (null === $token) {
96
            $token = Craft::$app->getRequest()->getRequiredBodyParam('token');
97
        }
98
99
        $action = Craft::createObject([
100
            'class' => Disable::class,
101
            'checkAccess' => [$this, 'checkAdminAccess']
102
        ], [
103
            'revoke',
104
            $this
105
        ]);
106
107
        return $action->runWithParams([
108
            'token' => $token
109
        ]);
110
    }
111
112
    /**
113
     * @param null $token
114
     * @return mixed
115
     * @throws \yii\base\InvalidConfigException
116
     * @throws \yii\web\BadRequestHttpException
117
     */
118
    public function actionEnable($token = null)
119
    {
120
        if (null === $token) {
121
            $token = Craft::$app->getRequest()->getRequiredBodyParam('token');
122
        }
123
124
        $action = Craft::createObject([
125
            'class' => Enable::class,
126
            'checkAccess' => [$this, 'checkAdminAccess']
127
        ], [
128
            'reinstate',
129
            $this
130
        ]);
131
132
        return $action->runWithParams([
133
            'token' => $token
134
        ]);
135
    }
136
137
    /**
138
     * @param null $token
139
     * @return mixed
140
     * @throws \yii\base\InvalidConfigException
141
     * @throws \yii\web\BadRequestHttpException
142
     */
143
    public function actionDelete($token = null)
144
    {
145
        if (null === $token) {
146
            $token = Craft::$app->getRequest()->getRequiredBodyParam('token');
147
        }
148
149
        $action = Craft::createObject([
150
            'class' => Delete::class,
151
            'checkAccess' => [$this, 'checkAdminAccess']
152
        ], [
153
            'delete',
154
            $this
155
        ]);
156
157
        return $action->runWithParams([
158
            'token' => $token
159
        ]);
160
    }
161
}
162