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

InstancesController::actionUpsert()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 55

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 55
ccs 0
cts 40
cp 0
rs 8.9818
c 0
b 0
f 0
cc 4
nc 6
nop 3
crap 20

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\circleicon\CircleIcon;
8
use flipbox\patron\Patron;
9
use flipbox\patron\records\Provider;
10
use flipbox\patron\records\ProviderInstance;
11
12
class InstancesController extends AbstractViewController
13
{
14
15
    /**
16
     * The index view template path
17
     */
18
    const TEMPLATE_INDEX = AbstractViewController::TEMPLATE_BASE . '/instances';
19
20
    /**
21
     * The upsert view template path
22
     */
23
    const TEMPLATE_UPSERT = self::TEMPLATE_INDEX . '/upsert';
24
25
    /**
26
     * @param null $provider
27
     * @param null $identifier
28
     * @param ProviderInstance|null $instance
29
     * @return \yii\web\Response
30
     * @throws \flipbox\ember\exceptions\NotFoundException
31
     * @throws \yii\base\InvalidConfigException
32
     */
33
    public function actionUpsert($provider = null, $identifier = null, ProviderInstance $instance = null)
34
    {
35
        Craft::$app->getView()->registerAssetBundle(CircleIcon::class);
36
37
        $provider = Patron::getInstance()->manageProviders->getByCondition([
38
            'id' => $provider,
39
            'enabled' => null
40
        ]);
41
42
        // Empty variables for template
43
        $variables = [];
44
45
        if (null === $instance) {
46
            if (null === $identifier) {
47
                $instance = new ProviderInstance();
48
            } else {
49
                $instance = ProviderInstance::findOne($identifier);
50
            }
51
        }
52
53
        $instance->setProvider($provider);
54
55
        // Template variables
56
        $this->instanceVariables($variables, $provider, $instance);
0 ignored issues
show
Documentation introduced by
$instance is of type object<yii\db\ActiveRecordInterface>|array|null, but the function expects a object<flipbox\patron\records\ProviderInstance>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
57
58
        $availableEnvironments = array_merge(
59
            $this->availableEnvironments($provider),
60
            $instance->getEnvironments()
61
                ->indexBy(null)
62
                ->select(['environment'])
63
                ->column()
64
        );
65
66
        $instanceOptions = [];
67
        foreach (Patron::getInstance()->getSettings()->getEnvironments() as $env) {
68
            $instanceOptions[] = [
69
                'label' => Craft::t('patron', $env),
70
                'value' => $env,
71
                'disabled' => !in_array($env, $availableEnvironments, true)
72
            ];
73
        }
74
75
        $variables['provider'] = $provider;
76
        $variables['instance'] = $instance;
77
        $variables['environmentOptions'] = $instanceOptions;
78
79
        // Full page form in the CP
80
        $variables['fullPageForm'] = true;
81
82
        // Tabs
83
        $variables['tabs'] = $this->getTabs($provider);
84
        $variables['selectedTab'] = 'general';
85
86
        return $this->renderTemplate(static::TEMPLATE_UPSERT, $variables);
87
    }
88
89
90
    /*******************************************
91
     * BASE VARIABLES
92
     *******************************************/
93
    
94
    /**
95
     * @inheritdoc
96
     */
97
    protected function getBaseCpPath(): string
98
    {
99
        return $this->getBaseCpProviderPath() . '/' . Craft::$app->getRequest()->getSegment(4);
100
    }
101
102
    /**
103
     * @inheritdoc
104
     */
105
    protected function getBaseActionPath(): string
106
    {
107
        return parent::getBaseActionPath() . '/instances';
108
    }
109
110
111
    /*******************************************
112
     * VARIABLES
113
     *******************************************/
114
115
    /**
116
     * @param array $variables
117
     * @param Provider $provider
118
     */
119
    protected function instanceVariables(array &$variables, Provider $provider, ProviderInstance $instance)
120
    {
121
        $this->updateVariables($variables, $provider);
122
123
        // Set the "Continue Editing" URL
124
        $continueEditingPath = $instance->getId() ?: '{id}';
125
        $variables['continueEditingUrl'] = $this->getBaseContinueEditingUrl('/' . $continueEditingPath);
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