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

DefaultController::actionUpsert()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 62

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

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