DefaultController::actionUpsert()   B
last analyzed

Complexity

Conditions 7
Paths 24

Size

Total Lines 69

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 69
ccs 0
cts 52
cp 0
rs 7.743
c 0
b 0
f 0
cc 7
nc 24
nop 2
crap 56

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\patron\web\assets\card\Card;
7
use flipbox\patron\web\assets\circleIcon\CircleIcon;
8
use flipbox\patron\helpers\ProviderHelper;
9
use flipbox\patron\records\Provider;
10
use flipbox\patron\web\assets\providers\ProvidersAsset;
11
12
class DefaultController extends AbstractViewController
13
{
14
    /**
15
     * The index view template path
16
     */
17
    const TEMPLATE_INDEX = AbstractViewController::TEMPLATE_BASE;
18
19
    /**
20
     * The upsert view template path
21
     */
22
    const TEMPLATE_UPSERT = self::TEMPLATE_INDEX . '/upsert';
23
24
    /**
25
     * @return \yii\web\Response
26
     * @throws \yii\base\InvalidConfigException
27
     */
28
    public function actionIndex()
29
    {
30
        Craft::$app->getView()->registerAssetBundle(Card::class);
31
        Craft::$app->getView()->registerAssetBundle(CircleIcon::class);
32
33
        // Empty variables for template
34
        $variables = [];
35
36
        // apply base view variables
37
        $this->baseVariables($variables);
38
39
        // Full page form in the CP
40
        $variables['fullPageForm'] = Craft::$app->getConfig()->getGeneral()->allowAdminChanges;
41
42
        // Configured providers
43
        $variables['providers'] = Provider::findAll([
44
            'enabled' => null
45
        ]);
46
47
        return $this->renderTemplate(static::TEMPLATE_INDEX, $variables);
48
    }
49
50
    /**
51
     * @param null $identifier
52
     * @param Provider|null $provider
53
     * @return \yii\web\Response
54
     * @throws \ReflectionException
55
     * @throws \craft\errors\InvalidPluginException
56
     * @throws \flipbox\craft\ember\exceptions\RecordNotFoundException
57
     * @throws \yii\base\InvalidConfigException
58
     */
59
    public function actionUpsert($identifier = null, Provider $provider = null)
60
    {
61
        $this->getView()->registerAssetBundle(ProvidersAsset::class);
62
63
        // Empty variables for template
64
        $variables = [];
65
66
        if (null === $provider) {
67
            if (null === $identifier) {
68
                $provider = new Provider();
69
            } else {
70
                $provider = Provider::getOne([
71
                    'enabled' => null,
72
                    is_numeric($identifier) ? 'id' : 'handle' => $identifier
73
                ]);
74
            }
75
        }
76
77
        // Template variables
78
        if (!$provider->getId()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $provider->getId() of type null|integer is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
79
            $this->insertVariables($variables);
80
        } else {
81
            $this->updateVariables($variables, $provider);
82
        }
83
84
        $providerInfo = $this->module->getProviderInfo();
85
86
        // Available providers options
87
        $providerOptions = [];
88
        $providers = $this->module->getProviders();
89
        foreach ($providers as $availableProvider) {
90
            $info = $providerInfo[$availableProvider] ?? [];
91
            $providerOptions[] = [
92
                'label' => $info['name'] ?? ProviderHelper::displayName($availableProvider),
93
                'value' => $availableProvider
94
            ];
95
        }
96
97
        $variables['providers'] = $providers;
98
        $variables['providerOptions'] = $providerOptions;
99
        $variables['provider'] = $provider;
100
101
        $pluginLocks = [];
102
        $pluginHandles = $provider->getLocks()
103
            ->alias('locks')
104
            ->leftJoin('{{%plugins}} plugins', '[[plugins.id]]=[[locks.pluginId]]')
105
            ->select(['handle'])->column();
106
107
        foreach ($pluginHandles as $pluginHandle) {
108
            $pluginLocks[] = array_merge(
109
                Craft::$app->getPlugins()->getPluginInfo($pluginHandle),
110
                [
111
                    'icon' => Craft::$app->getPlugins()->getPluginIconSvg($pluginHandle)
112
                ]
113
            );
114
        }
115
116
        // Plugins that have locked this provider
117
        $variables['pluginLocks'] = $pluginLocks;
118
119
        // Full page form in the CP
120
        $variables['fullPageForm'] = Craft::$app->getConfig()->getGeneral()->allowAdminChanges;
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