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

InstancesController::getBaseActionPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace flipbox\patron\cp\controllers\view\providers;
4
5
use Craft;
6
use craft\helpers\ArrayHelper;
7
use craft\helpers\UrlHelper;
8
use flipbox\craft\assets\circleicon\CircleIcon;
9
use flipbox\ember\web\assets\rowinfomodal\RowInfoModal;
10
use flipbox\patron\Patron;
11
use flipbox\patron\records\Provider;
12
use flipbox\patron\records\ProviderInstance;
13
14
class InstancesController extends AbstractViewController
15
{
16
17
    /**
18
     * The index view template path
19
     */
20
    const TEMPLATE_INDEX = AbstractViewController::TEMPLATE_BASE . '/instances';
21
22
    /**
23
     * The upsert view template path
24
     */
25
    const TEMPLATE_UPSERT = self::TEMPLATE_INDEX . '/upsert';
26
27
    /**
28
     * @param null $provider
29
     * @param null $identifier
30
     * @param ProviderInstance|null $instance
31
     * @return \yii\web\Response
32
     * @throws \flipbox\ember\exceptions\NotFoundException
33
     * @throws \yii\base\InvalidConfigException
34
     */
35
    public function actionUpsert($provider = null, $identifier = null, ProviderInstance $instance = null)
36
    {
37
        Craft::$app->getView()->registerAssetBundle(CircleIcon::class);
38
        Craft::$app->getView()->registerAssetBundle(RowInfoModal::class);
39
40
        $provider = Patron::getInstance()->manageProviders->getByCondition([
41
            'id' => $provider,
42
            'environment' => null,
43
            'enabled' => null
44
        ]);
45
46
        // Empty variables for template
47
        $variables = [];
48
49
        if (null === $instance) {
50
            if (null === $identifier) {
51
                $instance = new ProviderInstance();
52
            } else {
53
                $instance = ProviderInstance::findOne($identifier);
54
            }
55
        }
56
57
        $instance->setProvider($provider);
58
59
        // Template variables
60
        $this->instanceVariables($variables, $provider);
61
62
        $availableEnvironments = array_merge(
63
            $this->availableEnvironments($provider),
64
            $instance->getEnvironments()
65
                ->indexBy(null)
66
                ->select(['environment'])
67
                ->column()
68
        );
69
70
        $instanceOptions = [];
71
        foreach (Patron::getInstance()->getSettings()->getEnvironments() as $env) {
72
            $instanceOptions[] = [
73
                'label' => Craft::t('patron', $env),
74
                'value' => $env,
75
                'disabled' => !in_array($env, $availableEnvironments, true)
76
            ];
77
        }
78
79
        $variables['provider'] = $provider;
80
        $variables['instance'] = $instance;
81
        $variables['environmentOptions'] = $instanceOptions;
82
83
        // Full page form in the CP
84
        $variables['fullPageForm'] = true;
85
86
        // Tabs
87
        $variables['tabs'] = $this->getTabs($provider);
88
        $variables['selectedTab'] = 'general';
89
90
        return $this->renderTemplate(static::TEMPLATE_UPSERT, $variables);
91
    }
92
93
94
    /*******************************************
95
     * BASE VARIABLES
96
     *******************************************/
97
98
    /**
99
     * @inheritdoc
100
     */
101
    protected function getBaseCpPath(): string
102
    {
103
        return parent::getBaseCpPath() . '/' . Craft::$app->getRequest()->getSegment(3);
104
    }
105
106
    /**
107
     * @inheritdoc
108
     */
109
    protected function getBaseActionPath(): string
110
    {
111
        return parent::getBaseActionPath() . '/instances';
112
    }
113
114
115
    /*******************************************
116
     * VARIABLES
117
     *******************************************/
118
119
    /**
120
     * @param array $variables
121
     * @param Provider $provider
122
     */
123
    protected function instanceVariables(array &$variables, Provider $provider)
124
    {
125
        $this->updateVariables($variables, $provider);
126
127
        $variables['title'] .= ' ' . Craft::t('patron', "Instance");
128
129
        // Breadcrumbs
130
        $variables['crumbs'][] = [
131
            'label' => Craft::t('patron', "Instance"),
132
            'url' => UrlHelper::url(
133
                $variables['baseCpPath'] . '/instances/' .
134
                Craft::$app->getRequest()->getSegment(5)
135
            )
136
        ];
137
    }
138
}
139