Completed
Push — master ( 525893...7edc6a )
by Damien
10:39
created

AbstractController::addUrls()   C

Complexity

Conditions 10
Paths 5

Size

Total Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 67
rs 6.8533
c 0
b 0
f 0
cc 10
nc 5
nop 1

How to fix   Long Method    Complexity   

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
 * Created by PhpStorm.
4
 * User: dsmrt
5
 * Date: 3/9/18
6
 * Time: 2:48 PM
7
 */
8
9
namespace flipbox\saml\core\controllers\cp\view;
10
11
use flipbox\saml\core\AbstractPlugin;
12
use flipbox\saml\core\controllers\AbstractController as BaseController;
13
use flipbox\saml\core\EnsureSAMLPlugin;
14
use flipbox\saml\core\models\AbstractSettings;
15
use flipbox\saml\core\records\ProviderInterface;
16
use flipbox\saml\core\web\assets\bundles\SamlCore;
17
use SAML2\XML\md\EndpointType;
18
use SAML2\XML\md\IDPSSODescriptor;
19
use SAML2\XML\md\IndexedEndpointType;
20
use SAML2\XML\md\SPSSODescriptor;
21
22
/**
23
 * Class AbstractController
24
 * @package flipbox\saml\core\controllers\cp\view
25
 */
26
abstract class AbstractController extends BaseController implements EnsureSAMLPlugin
27
{
28
29
    const TEMPLATE_INDEX = DIRECTORY_SEPARATOR . '_cp';
30
31
    /**
32
     * @throws \yii\base\InvalidConfigException
33
     */
34
    public function init()
35
    {
36
        parent::init();
37
        \Craft::$app->view->registerAssetBundle(
38
            SamlCore::class
39
        );
40
    }
41
42
    /**
43
     * @return string
44
     */
45
    protected function getTemplateIndex()
46
    {
47
        return $this->getPlugin()->getTemplateRootKey();
48
    }
49
50
    /**
51
     * @return array
52
     */
53
    protected function getBaseVariables()
54
    {
55
        $variables = [
56
            'plugin' => $this->getPlugin(),
57
            'title' => $this->getPlugin()->name,
58
//            'pluginHandle' => $this->getPlugin()->getHandle(),
59
            'pluginVariable' => $this->getPlugin()->getPluginVariableHandle(),
60
            'ownEntityId' => $this->getPlugin()->getSettings()->getEntityId(),
61
            'settings' => $this->getPlugin()->getSettings(),
62
63
            // Set the "Continue Editing" URL
64
            'continueEditingUrl' => $this->getBaseCpPath(),
65
            'baseActionPath' => $this->getBaseCpPath(),
66
            'baseCpPath' => $this->getBaseCpPath(),
67
            'templateIndex' => $this->getTemplateIndex(),
68
            'ownProvider' => $ownProvider = $this->getPlugin()->getProvider()->findOwn(),
69
70
            'actions' => [],
71
        ];
72
73
        $variables['selectedSubnavItem'] = $this->getSubNavKey();
74
75
        /** @var ProviderInterface $ownProvider */
76
        if ($ownProvider) {
77
            $variables = array_merge(
78
                $this->addUrls($ownProvider),
79
                $variables
80
            );
81
        }
82
83
        return $variables;
84
    }
85
86
    /**
87
     * @return null|string
88
     */
89
    protected function getSubNavKey()
90
    {
91
        $request = \Craft::$app->request;
92
93
        $key = null;
94
        $path = implode(
95
            '/',
96
            [
97
                $request->getSegment(2),
98
                $request->getSegment(3),
99
                $request->getSegment(4),
100
            ]
101
        );
102
103
        if (preg_match('#^/+$#', $path)) {
104
            $key = 'saml.setup';
105
        } elseif (preg_match('#metadata/my-provider/#', $path)) {
106
            $key = 'saml.myProvider';
107
        } elseif (preg_match('#metadata/+$#', $path)) {
108
            $key = 'saml.providers';
109
        } elseif (preg_match('#keychain/+$#', $path)) {
110
            $key = 'saml.keychain';
111
        } elseif (preg_match('#settings/+$#', $path)) {
112
            $key = 'saml.settings';
113
        }
114
        return $key;
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    protected function getBaseCpPath(): string
121
    {
122
        return $this->getPlugin()->getHandle();
123
    }
124
125
    /**
126
     * @param ProviderInterface $provider
127
     * @param array $variables
0 ignored issues
show
Bug introduced by
There is no parameter named $variables. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
128
     * @return array
129
     */
130
    protected function addUrls(ProviderInterface $provider)
131
    {
132
133
        $variables = [];
134
        $variables['assertionConsumerServices'] = null;
135
        $variables['singleLogoutServices'] = null;
136
        $variables['singleSignOnServices'] = null;
137
138
        if (! $provider->getMetadataModel()) {
139
            return $variables;
140
        }
141
142
        /** @var AbstractPlugin $plugin */
143
        $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...
144
145
        /**
146
         * Metadata/EntityDescriptor Model
147
         */
148
        $entityDescriptor = $provider->getMetadataModel();
149
150
        /**
151
         * Add SP URLs
152
         */
153
        if ($provider->getType() === AbstractSettings::SP) {
154
            foreach ($entityDescriptor->getRoleDescriptor() as $roleDescriptor) {
155
                if (! ($roleDescriptor instanceof SPSSODescriptor)) {
156
                    continue;
157
                }
158
159
                if ($endpoint = $this->getFirstEndpoint($roleDescriptor->getSingleLogoutService())) {
160
                    $sloBinding = $endpoint->getBinding();
161
                    $sloResponseLocation = $endpoint->getResponseLocation();
162
                    $variables['singleLogoutServices'][$sloBinding] = $sloResponseLocation;
163
                }
164
165
                /** @var IndexedEndpointType $firstACS */
166
                $firstACS = $this->getFirstEndpoint($roleDescriptor->getAssertionConsumerService());
167
                $acsBinding = $firstACS->getBinding();
168
                $acsLocation = $firstACS->getLocation();
169
                $variables['assertionConsumerServices'][$acsBinding] = $acsLocation;
170
            }
171
        }
172
173
        /**
174
         * Add IDP URLs
175
         */
176
        if ($provider->getType() === AbstractSettings::IDP) {
177
            foreach ($entityDescriptor->getRoleDescriptor() as $roleDescriptor) {
178
                if (! ($roleDescriptor instanceof IDPSSODescriptor)) {
179
                    continue;
180
                }
181
182
                if ($endpoint = $this->getFirstEndpoint($roleDescriptor->getSingleLogoutService())) {
183
                    $sloBinding = $endpoint->getBinding();
184
                    $sloResponseLocation = $endpoint->getResponseLocation();
185
                    $variables['singleLogoutServices'][$sloBinding] = $sloResponseLocation;
186
                }
187
188
                $sso = $this->getFirstEndpoint($roleDescriptor->getSingleSignOnService());
189
                $ssoBinding = $sso->getBinding();
190
                $ssoLocation = $sso->getLocation();
191
                $variables['singleSignOnServices'][$ssoBinding] = $ssoLocation;
192
            }
193
        }
194
195
        return $variables;
196
    }
197
198
    /**
199
     * @param $endpoints
200
     * @return EndpointType|null
201
     */
202
    protected function getFirstEndpoint($endpoints)
203
    {
204
        if (is_null($endpoints) || empty($endpoints)) {
205
            return null;
206
        }
207
208
        return array_shift($endpoints);
209
    }
210
211
212
    /**
213
     * @param $type
214
     * @return string
215
     */
216
    protected function getTitle($type)
217
    {
218
        return $type === AbstractSettings::SP ? 'Service Provider (SP)' : 'Identity Provider (IDP)';
219
    }
220
}
221