Completed
Push — develop ( eb3b7f...ef26fd )
by Damien
17:04 queued 06:46
created

AbstractController::addUrls()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 7.7591
c 0
b 0
f 0
cc 8
nc 5
nop 1

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
 * 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\records\ProviderInterface;
13
use flipbox\saml\core\Saml;
14
use flipbox\saml\core\traits\EnsureSamlPlugin;
15
use flipbox\saml\core\controllers\AbstractController as BaseController;
16
use flipbox\saml\core\web\assets\bundles\SamlCore;
17
18
/**
19
 * Class AbstractController
20
 * @package flipbox\saml\core\controllers\cp\view
21
 */
22
abstract class AbstractController extends BaseController
23
{
24
    use EnsureSamlPlugin;
25
26
    const TEMPLATE_INDEX = DIRECTORY_SEPARATOR . '_cp';
27
28
    /**
29
     * @throws \yii\base\InvalidConfigException
30
     */
31
    public function init()
32
    {
33
        parent::init();
34
        \Craft::$app->view->registerAssetBundle(
35
            SamlCore::class
36
        );
37
    }
38
39
    /**
40
     * @return string
41
     */
42
    abstract protected function getProviderRecord();
43
44
    /**
45
     * @return string
46
     */
47
    protected function getTemplateIndex()
48
    {
49
        return Saml::getTemplateRootKey(
50
            $this->getSamlPlugin()
0 ignored issues
show
Documentation introduced by
$this->getSamlPlugin() is of type object<flipbox\saml\core\SamlPluginInterface>, but the function expects a object<craft\base\Plugin>.

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...
51
        );
52
    }
53
54
    /**
55
     * @return array
56
     */
57
    protected function getBaseVariables()
58
    {
59
        $variables = [
60
            'title'              => $this->getSamlPlugin()->name,
0 ignored issues
show
Bug introduced by
Accessing name on the interface flipbox\saml\core\SamlPluginInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
61
            'pluginHandle'       => $this->getSamlPlugin()->getHandle(),
62
            'pluginVariable'     => $this->getSamlPlugin()->getPluginVariableHandle(),
63
            'ownEntityId'        => $this->getSamlPlugin()->getSettings()->getEntityId(),
64
            'settings'           => $this->getSamlPlugin()->getSettings(),
65
66
            // Set the "Continue Editing" URL
67
            'continueEditingUrl' => $this->getBaseCpPath(),
68
            'baseActionPath'     => $this->getBaseCpPath(),
69
            'baseCpPath'         => $this->getBaseCpPath(),
70
            'templateIndex'      => $this->getTemplateIndex(),
71
            'ownProvider'        => $ownProvider = $this->getSamlPlugin()->getProvider()->findOwn(),
72
73
            'actions' => [],
74
        ];
75
76
        $variables['selectedSubnavItem'] = $this->getSubNavKey();
77
78
        /** @var ProviderInterface $ownProvider */
79
        if ($ownProvider) {
80
            $variables = array_merge(
81
                $this->addUrls($ownProvider),
82
                $variables
83
            );
84
        }
85
86
        return $variables;
87
    }
88
89
    /**
90
     * @return null|string
91
     */
92
    protected function getSubNavKey()
93
    {
94
        $request = \Craft::$app->request;
95
96
        $key = null;
97
        $path = implode(
98
            '/',
99
            [
100
                $request->getSegment(2),
101
                $request->getSegment(3),
102
                $request->getSegment(4),
103
            ]
104
        );
105
106
        if (preg_match('#^/+$#', $path)) {
107
            $key = 'saml.setup';
108
        } elseif (preg_match('#metadata/my-provider/#', $path)) {
109
            $key = 'saml.myProvider';
110
        } elseif (preg_match('#metadata/+$#', $path)) {
111
            $key = 'saml.providers';
112
        } elseif (preg_match('#keychain/+$#', $path)) {
113
            $key = 'saml.keychain';
114
        } elseif (preg_match('#settings/+$#', $path)) {
115
            $key = 'saml.settings';
116
        }
117
        return $key;
118
    }
119
120
    /**
121
     * @return string
122
     */
123
    protected function getBaseCpPath(): string
124
    {
125
        return $this->getSamlPlugin()->getHandle();
126
    }
127
128
    /**
129
     * @param ProviderInterface $provider
130
     * @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...
131
     * @return array
132
     */
133
    protected function addUrls(ProviderInterface $provider)
134
    {
135
136
        $variables = [];
137
        $variables['assertionConsumerServices'] = null;
138
        $variables['singleLogoutServices'] = null;
139
        $variables['singleSignOnServices'] = null;
140
141
        if (! $provider->getMetadataModel()) {
142
            return $variables;
143
        }
144
145
        /** @var AbstractPlugin $plugin */
146
        $plugin = $this->getSamlPlugin();
147
148
        /**
149
         * Add SP URLs
150
         */
151
        if ($provider->getType() === $plugin::SP) {
152
            foreach ($provider->
153
            getMetadataModel()->
154
            getFirstSpSsoDescriptor()->
155
            getAllSingleLogoutServices() as $singleLogoutService) {
156
                $variables['singleLogoutServices'][$singleLogoutService->getBinding()] =
157
                    $singleLogoutService->getResponseLocation();
158
            }
159
            foreach ($provider->getMetadataModel()->
160
            getFirstSpSsoDescriptor()->
161
            getAllAssertionConsumerServices() as $assertionConsumerService) {
162
                $variables['assertionConsumerServices'][$assertionConsumerService->getBinding()] =
163
                    $assertionConsumerService->getLocation();
164
            }
165
        }
166
167
        /**
168
         * Add IDP URLs
169
         */
170
        if ($provider->getType() === $plugin::IDP) {
171
            foreach ($provider->getMetadataModel()->
172
            getFirstIdpSsoDescriptor()->
173
            getAllSingleLogoutServices() as $singleLogoutService) {
174
                $variables['singleLogoutServices'][$singleLogoutService->getBinding()] =
175
                    $singleLogoutService->getLocation();
176
            }
177
178
            foreach ($provider->getMetadataModel()->
179
            getFirstIdpSsoDescriptor()->
180
            getAllSingleSignOnServices() as $signOnService) {
181
                $variables['singleSignOnServices'][$signOnService->getBinding()] = $signOnService->getLocation();
182
            }
183
        }
184
185
        return $variables;
186
    }
187
188
189
    /**
190
     * @param $type
191
     * @return string
192
     */
193
    protected function getTitle($type)
194
    {
195
        /** @var AbstractPlugin $plugin */
196
        $plugin = $this->getSamlPlugin();
197
        return $type === $plugin::SP ? 'Service Provider (SP)' : 'Identity Provider (IDP)';
198
    }
199
}
200