Completed
Push — master ( 72ecff...582962 )
by Damien
11:32
created

AbstractSettings::isIDP()   A

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
     * @var string
27
     */
28
    protected $entityId;
29
30
    /**
31
     * @var string
32
     */
33
    public $endpointPrefix = self::ENDPOINT_PREFIX;
34
35
    /**
36
     * This is the endpoint used to initiate login. Set the general.php config for `loginPath` to this.
37
     * @see GeneralConfig::$loginPath
38
     *
39
     * @var string
40
     */
41
    protected $loginEndpoint = 'login';
42
43
    /**
44
     * This is the endpoint used to initiate login. Set the general.php config for `loginPath` to this.
45
     * @see GeneralConfig::$loginPath
46
     *
47
     * @var string
48
     */
49
    protected $logoutEndpoint = 'logout';
50
51
    /**
52
     * This is the endpoint used to initiate login. Set the general.php config for `loginPath` to this.
53
     * @see GeneralConfig::$loginPath
54
     *
55
     * @var string
56
     */
57
    protected $loginRequestEndpoint = 'login/request';
58
59
    /**
60
     * This is the endpoint used to initiate logout. In this case, `logoutPath` cannot be used.
61
     * Point your logout button to this endpoint.
62
     *
63
     * @var string
64
     */
65
    protected $logoutRequestEndpoint = 'logout/request';
66
67
68
    /**
69
     * This setting will destroy sessions when the Name Id matches a user with existing sessions.
70
     * A current user session doesn't have to exist, ie, `\Craft::$app->user->isGuest === true`.
71
     *
72
     * This can be useful if the LogoutRequest is sent over AJAX.
73
     *
74
     * Warning: this will delete all current sessions for the user
75
     *
76
     * @var bool
77
     */
78
    public $sloDestroySpecifiedSessions = false;
79
80
    /**
81
     * @inheritdoc
82
     */
83
    public function attributes()
84
    {
85
        return array_merge(
86
            parent::attributes(),
87
            [
88
                'entityId',
89
            ]
90
        );
91
    }
92
93
    /*******************************************
94
     * ENTITY ID
95
     *******************************************/
96
97
    /**
98
     * @param $entityId
99
     * @return $this
100
     */
101
    public function setEntityId($entityId)
102
    {
103
        $this->entityId = $entityId;
104
105
        return $this;
106
    }
107
108
    /**
109
     * @return string
110
     * @throws \craft\errors\SiteNotFoundException
111
     */
112
    public function getEntityId()
113
    {
114
        if (! $this->entityId) {
115
            $this->entityId = UrlHelper::baseUrl();
116
        }
117
118
        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...
119
    }
120
121
    public function getEndpointPrefix()
122
    {
123
        return \Craft::parseEnv($this->endpointPrefix);
124
    }
125
126
    /**
127
     * @param $myType
128
     * @return $this
129
     */
130
    public function setMyType($myType)
131
    {
132
        $this->myType = $myType;
133
        return $this;
134
    }
135
136
    /**
137
     * @return string
138
     */
139
    public function getMyType()
140
    {
141
        return $this->myType;
142
    }
143
144
    /**
145
     * @return string
146
     */
147
    public function getRemoteType()
148
    {
149
        return $this->getMyType() === self::IDP ? self::SP : self::IDP;
150
    }
151
152
    /**
153
     * @return bool
154
     */
155
    public function isIDP()
156
    {
157
        return $this->getMyType() === self::IDP;
158
    }
159
160
    /**
161
     * @return bool
162
     */
163
    public function isSP()
164
    {
165
        return $this->getMyType() === self::SP;
166
    }
167
168
    protected function buildEndpointUrl($url)
169
    {
170
        return sprintf('/%s/%s', $this->getEndpointPrefix(), $url);
171
    }
172
173
    /**
174
     * @inheritdoc
175
     */
176
    public function getDefaultLoginEndpoint()
177
    {
178
        return UrlHelper::siteUrl(
179
            $this->buildEndpointUrl(
180
                $this->loginEndpoint
181
            )
182
        );
183
    }
184
185
    /**
186
     * @inheritdoc
187
     */
188
    public function getDefaultLogoutEndpoint()
189
    {
190
        return UrlHelper::siteUrl(
191
            $this->buildEndpointUrl($this->logoutEndpoint)
192
        );
193
    }
194
195
    /**
196
     * @inheritdoc
197
     */
198
    public function getDefaultLogoutRequestEndpoint()
199
    {
200
        return UrlHelper::siteUrl(
201
            $this->buildEndpointUrl($this->logoutRequestEndpoint)
202
        );
203
    }
204
205
    /**
206
     * @inheritdoc
207
     */
208
    public function getDefaultLoginRequestEndpoint()
209
    {
210
        return UrlHelper::siteUrl(
211
            $this->buildEndpointUrl($this->loginRequestEndpoint)
212
        );
213
    }
214
215
    /**
216
     * @inheritdoc
217
     */
218
    public function getDefaultLoginPath()
219
    {
220
        return $this->buildEndpointUrl($this->loginEndpoint);
221
    }
222
223
    /**
224
     * @inheritdoc
225
     */
226
    public function getDefaultLogoutPath()
227
    {
228
        return $this->buildEndpointUrl($this->logoutEndpoint);
229
    }
230
231
    /**
232
     * @inheritdoc
233
     */
234
    public function getDefaultLogoutRequestPath()
235
    {
236
        return $this->buildEndpointUrl($this->logoutRequestEndpoint);
237
    }
238
239
    /**
240
     * @inheritdoc
241
     */
242
    public function getDefaultLoginRequestPath()
243
    {
244
        return
245
            $this->buildEndpointUrl($this->loginRequestEndpoint);
246
    }
247
}
248