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

TokensController::actionUpsert()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 49

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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