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

AbstractEditController::actionNewSp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 9.1563
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: dsmrt
5
 * Date: 3/10/18
6
 * Time: 8:40 PM
7
 */
8
9
namespace flipbox\saml\core\controllers\cp\view\metadata;
10
11
use Craft;
12
use flipbox\keychain\KeyChain;
13
use flipbox\saml\core\AbstractPlugin;
14
use flipbox\saml\core\controllers\cp\view\AbstractController;
15
use craft\helpers\UrlHelper;
16
use flipbox\saml\core\records\ProviderInterface;
17
18
/**
19
 * Class AbstractEditController
20
 * @package flipbox\saml\core\controllers\cp\view\metadata
21
 */
22
abstract class AbstractEditController extends AbstractController
23
{
24
    const TEMPLATE_INDEX = DIRECTORY_SEPARATOR . '_cp' . DIRECTORY_SEPARATOR . 'metadata';
25
26
    /**
27
     * @param string|null $providerId
28
     * @return \yii\web\Response
29
     */
30
    public function actionIndex($providerId = null, $overwriteVariables = [])
31
    {
32
        $variables = $this->prepVariables($providerId);
33
        $provider = $variables['provider'];
34
35
        $variables['title'] = Craft::t(
36
            $this->getSamlPlugin()->getHandle(),
37
            'Edit ' . $this->getTitle($provider->getType())
38
        );
39
40
        $variables['createType'] = $variables['remoteType'];
41
42
        if (isset($variables['provider']) && $variables['provider'] instanceof ProviderInterface) {
43
44
            /**
45
             * Actions
46
             */
47
            $variables['actions'] = $this->getActions($variables['provider']);
48
        }
49
50
        $variables = array_merge($variables, $overwriteVariables);
51
        return $this->renderTemplate(
52
            $this->getTemplateIndex() . static::TEMPLATE_INDEX . DIRECTORY_SEPARATOR . 'edit',
53
            $variables
54
        );
55
    }
56
57
    /**
58
     * @return \yii\web\Response
59
     */
60
    public function actionNewIdp()
61
    {
62
        /** @var AbstractPlugin $plugin */
63
        $plugin = $this->getSamlPlugin();
64
        return $this->actionIndex(null, [
65
            'title'      => 'New ' . $this->getTitle($plugin::IDP),
66
            'createType' => $plugin::IDP,
67
            'crumbs'     => [
68
                [
69
                    'url'   => UrlHelper::cpUrl(
70
                        implode(
71
                            '/',
72
                            [
73
                                $plugin->getHandle(),
74
                            ]
75
                        )
76
                    ),
77
                    'label' => $plugin->name,
78
                ],
79
                [
80
                    'url'   => UrlHelper::cpUrl(
81
                        implode(
82
                            '/',
83
                            [
84
                                $plugin->getHandle(),
85
                                'metadata',
86
                            ]
87
                        )
88
                    ),
89
                    'label' => 'Provider List',
90
                ],
91
                [
92
                    'url'   => UrlHelper::cpUrl(
93
                        implode(
94
                            '/',
95
                            [
96
                                $plugin->getHandle(),
97
                                'metadata',
98
                                'new-idp'
99
                            ]
100
                        )
101
                    ),
102
                    'label' => 'New IDP',
103
                ],
104
            ],
105
        ]);
106
    }
107
108
    /**
109
     * @return \yii\web\Response
110
     */
111
    public function actionNewSp()
112
    {
113
        /** @var AbstractPlugin $plugin */
114
        $plugin = $this->getSamlPlugin();
115
        return $this->actionIndex(null, [
116
            'title'      => 'New ' . $this->getTitle($plugin::SP),
117
            'createType' => $plugin::SP,
118
            'crumbs'     => [
119
                [
120
                    'url'   => UrlHelper::cpUrl(
121
                        implode(
122
                            '/',
123
                            [
124
                                $plugin->getHandle(),
125
                            ]
126
                        )
127
                    ),
128
                    'label' => $plugin->name,
129
                ],
130
                [
131
                    'url'   => UrlHelper::cpUrl(
132
                        implode(
133
                            '/',
134
                            [
135
                                $plugin->getHandle(),
136
                                'metadata',
137
                            ]
138
                        )
139
                    ),
140
                    'label' => 'Provider List',
141
                ],
142
                [
143
                    'url'   => UrlHelper::cpUrl(
144
                        implode(
145
                            '/',
146
                            [
147
                                $plugin->getHandle(),
148
                                'metadata',
149
                                'new-sp'
150
                            ]
151
                        )
152
                    ),
153
                    'label' => 'New SP',
154
                ],
155
            ],
156
        ]);
157
    }
158
159
    /**
160
     * @return \yii\web\Response
161
     */
162
    public function actionMyProvider()
163
    {
164
        $provider = $this->getSamlPlugin()->getProvider()->findOwn();
165
        $variables = $this->prepVariables(
166
            $provider ? $provider->id : null
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<flipbox\saml\core...cords\AbstractProvider>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
167
        );
168
169
        if ($provider) {
170
            $variables['provider'] = $provider;
171
172
            $variables = array_merge(
173
                $variables,
174
                $this->addUrls($provider)
175
            );
176
        } else {
177
            $provider = $variables['provider'];
178
            $variables['provider']->entityId = $this->getSamlPlugin()->getSettings()->getEntityId();
179
            $variables['provider']->providerType = $this->getSamlPlugin()->getMyType();
180
            $variables['provider']->label = 'My Provider';
181
        }
182
183
        /**
184
         * Actions
185
         */
186
        $variables['actions'] = $this->getActions($provider);
187
188
        /**
189
         * Edit Title
190
         */
191
        $variables['title'] = Craft::t(
192
            $this->getSamlPlugin()->getHandle(),
193
            'My Provider (' . strtoupper($variables['provider']->providerType) . ')'
194
        );
195
196
        $variables['createType'] = $variables['myType'];
197
198
        return $this->renderTemplate(
199
            $this->getTemplateIndex() . static::TEMPLATE_INDEX . DIRECTORY_SEPARATOR . 'edit',
200
            $variables
201
        );
202
    }
203
204
    /**
205
     * @param ProviderInterface $provider
206
     * @return array
207
     */
208
    protected function getActions(ProviderInterface $provider)
209
    {
210
        $actions = [];
211
212
        if ($provider->id) {
0 ignored issues
show
Bug introduced by
Accessing id on the interface flipbox\saml\core\records\ProviderInterface 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...
213
            $actions = [
214
                [
215
                    //action list 1
216
                    [
217
                        'action' => $this->getSamlPlugin()->getHandle() . '/metadata/change-status',
218
                        'label'  => $provider->enabled ? 'Disable' : 'Enable',
0 ignored issues
show
Bug introduced by
Accessing enabled on the interface flipbox\saml\core\records\ProviderInterface 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...
219
                    ],
220
                    [
221
                        'action' => $this->getSamlPlugin()->getHandle() . '/metadata/delete',
222
                        'label'  => 'Delete',
223
                    ],
224
                ],
225
            ];
226
        }
227
        return $actions;
228
    }
229
230
    /**
231
     * @return array
232
     */
233
    protected function getBaseVariables()
234
    {
235
236
        return array_merge(
237
            parent::getBaseVariables(),
238
            [
239
                'autoCreate' => false,
240
                'myEntityId' => $this->getSamlPlugin()->getSettings()->getEntityId(),
241
                'myType'     => $this->getSamlPlugin()->getSettings(),
242
            ]
243
        );
244
    }
245
246
    /**
247
     * @param string|null $providerId
248
     * @return array
249
     */
250
    protected function prepVariables($providerId = null)
251
    {
252
        $variables = $this->getBaseVariables();
253
254
        $variables['title'] = Craft::t(
255
            $this->getSamlPlugin()->getHandle(),
256
            $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...
257
        );
258
259
        /**
260
         * TYPES
261
         */
262
        $variables['myType'] = $this->getSamlPlugin()->getMyType();
263
        $variables['remoteType'] = $this->getSamlPlugin()->getRemoteType();
264
265
        if ($providerId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $providerId of type string|null is loosely compared to true; this is ambiguous if the string can be empty. 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 string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
266
            /**
267
             * @var ProviderInterface $provider
268
             */
269
            $provider = $variables['provider'] = $provider = $this->getProviderRecord()::find()->where([
0 ignored issues
show
Bug introduced by
The method find cannot be called on $this->getProviderRecord() (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
270
                'id' => $providerId,
271
            ])->one();
272
273
            $variables['title'] .= ': Edit';
274
275
            $crumb = [
276
                [
277
278
                    'url'   => UrlHelper::cpUrl(
279
                        implode(
280
                            '/',
281
                            [
282
                                $this->getSamlPlugin()->getHandle(),
283
                                'metadata',
284
                            ]
285
                        )
286
                    ),
287
                    'label' => 'Provider List',
288
                ], [
289
                    'url'   => UrlHelper::cpUrl(
290
                        $this->getSamlPlugin()->getHandle() . '/metadata/' . $providerId
291
                    ),
292
                    'label' => $provider->label ?: $provider->entityId,
0 ignored issues
show
Bug introduced by
Accessing label on the interface flipbox\saml\core\records\ProviderInterface 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...
Bug introduced by
Accessing entityId on the interface flipbox\saml\core\records\ProviderInterface 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...
293
                ]
294
            ];
295
            $variables['keypair'] = $provider->keychain;
0 ignored issues
show
Bug introduced by
Accessing keychain on the interface flipbox\saml\core\records\ProviderInterface 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...
296
297
            $variables = array_merge(
298
                $variables,
299
                $this->addUrls($provider)
300
            );
301
        } else {
302
            $record = $this->getProviderRecord();
303
304
            $provider = $variables['provider'] = new $record([
0 ignored issues
show
Unused Code introduced by
$provider 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...
305
                'providerType' => 'idp',
306
            ]);
307
308
            $variables['title'] .= ': Create';
309
310
            $crumb = [
311
                [
312
                    'url'   => UrlHelper::cpUrl(
313
                        $this->getSamlPlugin()->getHandle() . '/new'
314
                    ),
315
                    'label' => 'New',
316
                ]
317
            ];
318
        }
319
320
        $variables['allkeypairs'] = [];
321
322
        $keypairs = KeyChain::getInstance()->getService()->findByPlugin($this->getSamlPlugin())->all();
323
324
        foreach ($keypairs as $keypair) {
325
            $variables['allkeypairs'][] = [
326
                'label' => $keypair->description,
327
                'value' => $keypair->id,
328
            ];
329
        }
330
331
        $variables['crumbs'] = array_merge([
332
            [
333
                'url'   => UrlHelper::cpUrl($this->getSamlPlugin()->getHandle()),
334
                'label' => Craft::t($this->getSamlPlugin()->getHandle(), $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...
335
            ],
336
        ], $crumb);
337
338
        return $variables;
339
    }
340
}
341