Completed
Push — master ( 72ecff...582962 )
by Damien
11:32
created

AbstractDefaultController::actionIndex()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 8.6925
c 0
b 0
f 0
cc 5
nc 9
nop 0

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
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 */
6
7
namespace flipbox\saml\core\controllers\cp\view\metadata;
8
9
use Craft;
10
use craft\helpers\UrlHelper;
11
use flipbox\saml\core\AbstractPlugin;
12
use flipbox\saml\core\controllers\cp\view\AbstractController;
13
use flipbox\saml\core\EnsureSAMLPlugin;
14
use flipbox\saml\core\models\SettingsInterface;
15
16
/**
17
 * Class AbstractDefaultController
18
 * @package flipbox\saml\core\controllers\cp\view\metadata
19
 */
20
abstract class AbstractDefaultController extends AbstractController implements EnsureSAMLPlugin
21
{
22
23
    const TEMPLATE_INDEX = DIRECTORY_SEPARATOR . '_cp' . DIRECTORY_SEPARATOR . 'metadata';
24
25
    /**
26
     * @return \yii\web\Response
27
     * @throws \Exception
28
     */
29
    public function actionIndex()
30
    {
31
        $variables = $this->getBaseVariables();
32
        $plugin = $this->getPlugin();
33
        $variables['myProvider'] = null;
34
        $variables['spProviders'] = [];
35
        $variables['idpProviders'] = [];
36
        $variables['idpListInstructions'] = $this->getListInstructions(SettingsInterface::IDP);
37
        $variables['spListInstructions'] = $this->getListInstructions(SettingsInterface::SP);
38
39
        /**
40
         * Breadcrumbs
41
         */
42
        $variables['crumbs'] = [
43
            [
44
                'url' => UrlHelper::cpUrl($this->getPlugin()->getHandle()),
45
                'label' => $this->getPlugin()->name,
46
            ],
47
            [
48
                'url' => UrlHelper::cpUrl($this->getPlugin()->getHandle()) . '/metadata',
49
                'label' => 'Provider List',
50
            ],
51
        ];
52
53
        /**
54
         * Get IDPs
55
         */
56
        foreach ($plugin->getProvider()->findByIdp([
57
            'enabled' => [true, false],
58
        ])->all() as $provider) {
59
            $variables['idpProviders'][] = $provider;
60
            if ($provider->getEntityId() == $this->getPlugin()->getSettings()->getEntityId()) {
61
                $variables['myProvider'] = $provider;
62
            }
63
        }
64
65
        /**
66
         * Get SPs
67
         */
68
        foreach ($plugin->getProvider()->findBySp([
69
            'enabled' => [true, false],
70
        ])->all() as $provider) {
71
            $variables['spProviders'][] = $provider;
72
            if ($provider->getEntityId() == $this->getPlugin()->getSettings()->getEntityId()) {
73
                $variables['myProvider'] = $provider;
74
            }
75
        }
76
77
        $variables['title'] = Craft::t($this->getPlugin()->getHandle(), $this->getPlugin()->name);
78
        return $this->renderTemplate(
79
            $this->getTemplateIndex() . static::TEMPLATE_INDEX . DIRECTORY_SEPARATOR . 'list',
80
            $variables
81
        );
82
    }
83
84
    /**
85
     * @param $providerType
86
     * @return string
87
     * @throws \Exception
88
     */
89
    protected function getListInstructions($providerType)
90
    {
91
        /** @var AbstractPlugin $plugin */
92
        $plugin = $this->getPlugin();
0 ignored issues
show
Unused Code introduced by
$plugin is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
93
        if (! in_array($providerType, [
94
            SettingsInterface::SP,
95
            SettingsInterface::IDP,
96
        ])) {
97
            throw new \Exception($providerType . ' is not a valid type.');
98
        }
99
100
        /**
101
         * Base return off of getMyType and the provider type passed
102
         */
103
        switch ($this->getPlugin()->getMyType()) {
104
            case SettingsInterface::IDP:
105
                $return = SettingsInterface::SP === $providerType ? Craft::t(
106
                    $this->getPlugin()->getHandle(),
107
                    'These are the remote providers using this Craft CMS instance to authenticate. '
108
                ) : Craft::t(
109
                    $this->getPlugin()->getHandle(),
110
                    'Your provider configuration(s).'
111
                );
112
                break;
113
            case SettingsInterface::SP:
114
                $return = SettingsInterface::SP === $providerType ? Craft::t(
115
                    $this->getPlugin()->getHandle(),
116
                    'Your provider configuration(s). '
117
                ) : Craft::t(
118
                    $this->getPlugin()->getHandle(),
119
                    'These are the remote providers where the user ' .
120
                    'authenticates, ie, OKTA, Microsoft AD, or Google, etc. To configure and IDP, simply obtain the metadata.'
121
                );
122
                break;
123
        }
124
125
        return $return;
0 ignored issues
show
Bug introduced by
The variable $return does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
126
    }
127
}
128