TokensController::tokenUpdateVariables()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 6
cp 0
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
1
<?php
2
3
namespace flipbox\patron\cp\controllers\view\providers;
4
5
use Craft;
6
use craft\helpers\UrlHelper;
7
use flipbox\patron\web\assets\circleIcon\CircleIcon;
8
use flipbox\patron\records\Provider;
9
use flipbox\patron\records\Token;
10
11
class TokensController extends AbstractViewController
12
{
13
    /**
14
     * The index view template path
15
     */
16
    const TEMPLATE_INDEX = AbstractViewController::TEMPLATE_BASE . '/tokens';
17
18
    /**
19
     * The upsert view template path
20
     */
21
    const TEMPLATE_UPSERT = self::TEMPLATE_INDEX . '/upsert';
22
23
    /**
24
     * @param $provider
25
     * @return \yii\web\Response
26
     * @throws \ReflectionException
27
     * @throws \flipbox\craft\ember\exceptions\RecordNotFoundException
28
     * @throws \yii\base\InvalidConfigException
29
     */
30
    public function actionIndex($provider)
31
    {
32
        Craft::$app->getView()->registerAssetBundle(CircleIcon::class);
33
34
        // Empty variables for template
35
        $variables = [];
36
37
        $provider = Provider::getOne([
38
            'id' => $provider,
39
            'enabled' => null
40
        ]);
41
42
        // Template variables
43
        $this->tokenVariables($variables, $provider);
44
45
        $variables['provider'] = $provider;
46
47
        // Tabs
48
        $variables['tabs'] = $this->getTabs($provider);
49
        $variables['selectedTab'] = 'tokens';
50
51
        return $this->renderTemplate(static::TEMPLATE_INDEX, $variables);
52
    }
53
54
    /**
55
     * @param $provider
56
     * @param $identifier
57
     * @param Token|null $token
58
     * @return \yii\web\Response
59
     * @throws \ReflectionException
60
     * @throws \flipbox\craft\ember\exceptions\RecordNotFoundException
61
     * @throws \yii\base\InvalidConfigException
62
     */
63
    public function actionUpsert($provider, $identifier, Token $token = null)
64
    {
65
        Craft::$app->getView()->registerAssetBundle(CircleIcon::class);
66
67
        // Empty variables for template
68
        $variables = [];
69
70
        $provider = Provider::getOne([
71
            'id' => $provider,
72
            'enabled' => null
73
        ]);
74
75
        if (null === $token) {
76
            $token = Token::getOne([
77
                is_numeric($identifier) ? 'id' : 'accessToken' => $identifier,
78
                'enabled' => null,
79
            ]);
80
        }
81
82
        // Template variables
83
        $this->tokenUpdateVariables($variables, $provider, $token);
84
85
        $variables['provider'] = $provider;
86
        $variables['token'] = $token;
87
88
        // Full page form in the CP
89
        $variables['fullPageForm'] = true;
90
91
        // Tabs
92
        $variables['tabs'] = $this->getTabs($provider);
93
        $variables['selectedTab'] = 'tokens';
94
95
        return $this->renderTemplate(static::TEMPLATE_UPSERT, $variables);
96
    }
97
98
    /*******************************************
99
     * PATHS
100
     *******************************************/
101
102
    /**
103
     * @inheritdoc
104
     */
105
    protected function getBaseCpPath(): string
106
    {
107
        return parent::getBaseCpPath() . '/' . Craft::$app->getRequest()->getSegment(3) .
108
            '/' . Craft::$app->getRequest()->getSegment(4);
109
    }
110
111
    /**
112
     * @inheritdoc
113
     */
114
    protected function getBaseActionPath(): string
115
    {
116
        return parent::getBaseActionPath() . '/tokens';
117
    }
118
119
120
    /*******************************************
121
     * VARIABLES
122
     *******************************************/
123
124
    /**
125
     * @param array $variables
126
     * @param Provider $provider
127
     * @throws \ReflectionException
128
     */
129
    protected function tokenVariables(array &$variables, Provider $provider)
130
    {
131
        $this->updateVariables($variables, $provider);
132
133
        // Set the "Continue Editing" URL
134
        $variables['continueEditingUrl'] = $this->getBaseContinueEditingUrl();
135
136
        // Title
137
        $variables['title'] .= ' ' . Craft::t('patron', "Tokens");
138
139
        // Breadcrumbs
140
        $variables['crumbs'][] = [
141
            'label' => Craft::t('patron', "Tokens"),
142
            'url' => UrlHelper::url(
143
                $variables['baseCpPath']
144
            )
145
        ];
146
    }
147
148
    /**
149
     * @param array $variables
150
     * @param Provider $provider
151
     * @param Token $token
152
     * @throws \ReflectionException
153
     */
154
    protected function tokenUpdateVariables(array &$variables, Provider $provider, Token $token)
155
    {
156
        $this->tokenVariables($variables, $provider);
157
158
        // Set the "Continue Editing" URL
159
        $variables['continueEditingUrl'] = $this->getBaseContinueEditingUrl('/' . $token->getId());
160
161
        $variables['title'] .= ' ' . Craft::t('patron', "Edit");
162
163
        // Breadcrumbs
164
        $variables['crumbs'][] = [
165
            'label' => Craft::t('patron', "Edit"),
166
            'url' => UrlHelper::url(
167
                $variables['baseCpPath'] . '/' . $token->getId()
168
            )
169
        ];
170
    }
171
}
172