AbstractSettings::getEndpointPrefix()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: dsmrt
5
 * Date: 1/23/18
6
 * Time: 9:26 PM
7
 */
8
9
namespace flipbox\saml\core\models;
10
11
use craft\base\Model;
12
use craft\config\GeneralConfig;
13
use craft\helpers\UrlHelper;
14
15
abstract class AbstractSettings extends Model implements SettingsInterface
16
{
17
18
    const ENDPOINT_PREFIX = 'sso';
19
20
    /**
21
     * @var string
22
     */
23
    protected $myType;
24
25
    /**
26
     * This is the system's entity id. Environmental Variables are allowed, ie, '$ENTITY_ID' (as a string)
27
     * @var string
28
     */
29
    protected $entityId;
30
31
    /**
32
     * @var string
33
     */
34
    public $endpointPrefix = self::ENDPOINT_PREFIX;
35
36
    /**
37
     * This setting will destroy sessions when the Name Id matches a user with existing sessions.
38
     * A current user session doesn't have to exist, ie, `\Craft::$app->user->isGuest === true`.
39
     *
40
     * This can be useful if the LogoutRequest is sent over AJAX.
41
     *
42
     * Warning: this will delete all current sessions for the user
43
     *
44
     * @var bool
45
     */
46
    public $sloDestroySpecifiedSessions = false;
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function attributes()
52
    {
53
        return array_merge(
54
            parent::attributes(),
55
            [
56
                'entityId',
57
            ]
58
        );
59
    }
60
61
    /*******************************************
62
     * ENTITY ID
63
     *******************************************/
64
65
    /**
66
     * @param $entityId
67
     * @return $this
68
     */
69
    public function setEntityId($entityId)
70
    {
71
        $this->entityId = $entityId;
72
73
        return $this;
74
    }
75
76
    /**
77
     * @return string
78
     * @throws \craft\errors\SiteNotFoundException
79
     */
80
    public function getEntityId()
81
    {
82
        if (! $this->entityId) {
83
            $this->entityId = UrlHelper::baseUrl();
84
        }
85
86
        return \Craft::parseEnv($this->entityId);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Craft::parseEnv($this->entityId); (null|boolean|string|array) is incompatible with the return type declared by the interface flipbox\saml\core\models...sInterface::getEntityId of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
87
    }
88
89
    /**
90
     * @return string
91
     * @throws \craft\errors\SiteNotFoundException
92
     */
93
    public function getEntityIdRaw()
94
    {
95
        if (! $this->entityId) {
96
            $this->entityId = UrlHelper::baseUrl();
97
        }
98
99
        return $this->entityId;
100
    }
101
102
    public function getEndpointPrefix()
103
    {
104
        return \Craft::parseEnv($this->endpointPrefix);
105
    }
106
107
    //@todo get rid of my type here. Should be back on the plugin
108
    /**
109
     * @param $myType
110
     * @return $this
111
     */
112
    public function setMyType($myType)
113
    {
114
        $this->myType = $myType;
115
        return $this;
116
    }
117
118
    /**
119
     * @return string
120
     */
121
    public function getMyType()
122
    {
123
        return $this->myType;
124
    }
125
126
    /**
127
     * @return string
128
     */
129
    public function getRemoteType()
130
    {
131
        return $this->getMyType() === self::IDP ? self::SP : self::IDP;
132
    }
133
134
    /**
135
     * @return bool
136
     */
137
    public function isIDP()
138
    {
139
        return $this->getMyType() === self::IDP;
140
    }
141
142
    /**
143
     * @return bool
144
     */
145
    public function isSP()
146
    {
147
        return $this->getMyType() === self::SP;
148
    }
149
}
150