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

DefaultController::actionUpsert()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 63
ccs 0
cts 47
cp 0
rs 8.185
c 0
b 0
f 0
cc 6
nc 24
nop 2
crap 42

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace flipbox\patron\cp\controllers\view\providers;
4
5
use Craft;
6
use flipbox\craft\assets\card\Card;
7
use flipbox\craft\assets\circleicon\CircleIcon;
8
use flipbox\patron\helpers\ProviderHelper as ProviderHelper;
9
use flipbox\patron\records\Provider;
10
use flipbox\patron\services\ManageProviders as ProviderService;
11
use flipbox\patron\web\assets\providers\ProvidersAsset;
12
13
class DefaultController extends AbstractViewController
14
{
15
    /**
16
     * The index view template path
17
     */
18
    const TEMPLATE_INDEX = AbstractViewController::TEMPLATE_BASE;
19
20
    /**
21
     * The upsert view template path
22
     */
23
    const TEMPLATE_UPSERT = self::TEMPLATE_INDEX . '/upsert';
24
25
    /**
26
     * @return ProviderService
27
     */
28
    protected function providerService(): ProviderService
29
    {
30
        return $this->module->module->manageProviders();
31
    }
32
33
    /**
34
     * @return \yii\web\Response
35
     * @throws \yii\base\InvalidConfigException
36
     */
37
    public function actionIndex()
38
    {
39
        Craft::$app->getView()->registerAssetBundle(Card::class);
40
        Craft::$app->getView()->registerAssetBundle(CircleIcon::class);
41
42
        // Empty variables for template
43
        $variables = [];
44
45
        // apply base view variables
46
        $this->baseVariables($variables);
47
48
        // Full page form in the CP
49
        $variables['fullPageForm'] = true;
50
51
        // Configured providers
52
        $variables['providers'] = $this->providerService()->findAll();
53
54
        return $this->renderTemplate(static::TEMPLATE_INDEX, $variables);
55
    }
56
57
    /**
58
     * @param null $identifier
59
     * @param Provider|null $provider
60
     * @return \yii\web\Response
61
     * @throws \craft\errors\InvalidPluginException
62
     * @throws \flipbox\ember\exceptions\NotFoundException
63
     * @throws \yii\base\InvalidConfigException
64
     */
65
    public function actionUpsert($identifier = null, Provider $provider = null)
66
    {
67
        $this->getView()->registerAssetBundle(ProvidersAsset::class);
68
69
        // Empty variables for template
70
        $variables = [];
71
72
        if (null === $provider) {
73
            if (null === $identifier) {
74
                $provider = $this->providerService()->create();
75
            } else {
76
                $provider = $this->providerService()->get($identifier);
77
            }
78
        }
79
80
        // Template variables
81
        if (!$provider->getId()) {
82
            $this->insertVariables($variables);
83
        } else {
84
            $this->updateVariables($variables, $provider);
85
        }
86
87
        // Available providers options
88
        $providerOptions = [];
89
        $providers = $this->module->getProviders();
90
        foreach ($providers as $availableProvider) {
91
            $providerOptions[] = [
92
                'label' => ProviderHelper::displayName($availableProvider),
93
                'value' => $availableProvider
94
            ];
95
        }
96
        $variables['providerOptions'] = $providerOptions;
97
        $variables['provider'] = $provider;
98
99
        $pluginLocks = [];
100
        $pluginHandles = $provider->getLocks()
101
            ->alias('locks')
102
            ->leftJoin('{{%plugins}} plugins', 'plugins.id=locks.pluginId')
103
            ->select(['handle'])->column();
104
105
        foreach ($pluginHandles as $pluginHandle) {
106
            $pluginLocks[] = array_merge(
107
                Craft::$app->getPlugins()->getPluginInfo($pluginHandle),
108
                [
109
                    'icon' => Craft::$app->getPlugins()->getPluginIconSvg($pluginHandle)
110
                ]
111
            );
112
        }
113
114
        // Plugins that have locked this provider
115
        $variables['pluginLocks'] = $pluginLocks;
116
        $variables['availableEnvironments'] = $this->availableEnvironments($provider);
117
118
        // Full page form in the CP
119
        $variables['fullPageForm'] = true;
120
121
        // Tabs
122
        $variables['tabs'] = $this->getTabs($provider);
123
        $variables['selectedTab'] = 'general';
124
        $variables['baseActionInstancePath'] = $this->getBaseActionPath() . '/instances';
125
126
        return $this->renderTemplate(static::TEMPLATE_UPSERT, $variables);
127
    }
128
}
129