Completed
Push — develop ( 5e4791...90850c )
by Nate
04:00
created

TokensController::actionUpsert()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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