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

AbstractExternalIdentity   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 5
dl 0
loc 61
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A displayName() 0 4 1
A hasContentColumn() 0 4 1
A normalizeValue() 0 8 2
A getInputHtml() 0 4 1
A getStaticHtml() 0 22 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 */
6
7
namespace flipbox\saml\core\fields;
8
9
use craft\base\ElementInterface;
10
use craft\base\Field;
11
use craft\elements\User;
12
use flipbox\saml\core\records\ProviderIdentityInterface;
13
use flipbox\saml\core\traits\EnsureSamlPlugin;
14
use yii\db\Query;
15
16
abstract class AbstractExternalIdentity extends Field
17
{
18
    use EnsureSamlPlugin;
19
20
    public static function displayName(): string
21
    {
22
        return 'External Identity';
23
    }
24
25
    /**
26
     * @inheritdoc
27
     */
28
    public static function hasContentColumn(): bool
29
    {
30
        return false;
31
    }
32
33
    public function normalizeValue($value, ElementInterface $element = null)
34
    {
35
        if (! ($element instanceof User)) {
36
            return null;
37
        }
38
        /** @var User $element */
39
        return $this->getSamlPlugin()->getProviderIdentity()->findByUser($element);
40
    }
41
42
    public function getInputHtml($value, ElementInterface $element = null): string
43
    {
44
        return $this->getStaticHtml($value, $element);
0 ignored issues
show
Bug introduced by
It seems like $element defined by parameter $element on line 42 can be null; however, flipbox\saml\core\fields...entity::getStaticHtml() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
45
    }
46
47
    /**
48
     * @param $value
49
     * @param ElementInterface $element
50
     * @return string
51
     * @throws \Twig_Error_Loader
52
     * @throws \yii\base\Exception
53
     */
54
    public function getStaticHtml($value, ElementInterface $element): string
55
    {
56
        if (! ($value instanceof Query)) {
57
            return '';
58
        }
59
        return \Craft::$app->getView()->renderTemplate(
60
            'saml-core/_cp/fields/external-id',
61
            [
62
                'identities' => $value,
63
                'element'    => $element,
64
            ]
65
        );
66
//        return "
67
//<div class='meta'>
68
//    NameID: {$value->nameId}
69
//    <br/>
70
//    Issuer: {$value->provider->getEntityId()}
71
//    <br/>
72
//    Last Login: {$value->lastLoginDate}
73
//</div >
74
//";
75
    }
76
}
77