Completed
Push — master ( 582962...aabf15 )
by Damien
08:08
created

AbstractSettings   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 246
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 3

Importance

Changes 0
Metric Value
wmc 22
lcom 3
cbo 3
dl 0
loc 246
rs 10
c 0
b 0
f 0

19 Methods

Rating   Name   Duplication   Size   Complexity  
A attributes() 0 9 1
A setEntityId() 0 6 1
A getEntityId() 0 8 2
A getEntityIdRaw() 0 8 2
A getEndpointPrefix() 0 4 1
A setMyType() 0 5 1
A getMyType() 0 4 1
A getRemoteType() 0 4 2
A isIDP() 0 4 1
A isSP() 0 4 1
A buildEndpointUrl() 0 4 1
A getDefaultLoginEndpoint() 0 8 1
A getDefaultLogoutEndpoint() 0 6 1
A getDefaultLogoutRequestEndpoint() 0 6 1
A getDefaultLoginRequestEndpoint() 0 6 1
A getDefaultLoginPath() 0 4 1
A getDefaultLogoutPath() 0 4 1
A getDefaultLogoutRequestPath() 0 4 1
A getDefaultLoginRequestPath() 0 5 1
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
    /**
122
     * @return string
123
     * @throws \craft\errors\SiteNotFoundException
124
     */
125
    public function getEntityIdRaw()
126
    {
127
        if (! $this->entityId) {
128
            $this->entityId = UrlHelper::baseUrl();
129
        }
130
131
        return $this->entityId;
132
    }
133
134
    public function getEndpointPrefix()
135
    {
136
        return \Craft::parseEnv($this->endpointPrefix);
137
    }
138
139
    /**
140
     * @param $myType
141
     * @return $this
142
     */
143
    public function setMyType($myType)
144
    {
145
        $this->myType = $myType;
146
        return $this;
147
    }
148
149
    /**
150
     * @return string
151
     */
152
    public function getMyType()
153
    {
154
        return $this->myType;
155
    }
156
157
    /**
158
     * @return string
159
     */
160
    public function getRemoteType()
161
    {
162
        return $this->getMyType() === self::IDP ? self::SP : self::IDP;
163
    }
164
165
    /**
166
     * @return bool
167
     */
168
    public function isIDP()
169
    {
170
        return $this->getMyType() === self::IDP;
171
    }
172
173
    /**
174
     * @return bool
175
     */
176
    public function isSP()
177
    {
178
        return $this->getMyType() === self::SP;
179
    }
180
181
    protected function buildEndpointUrl($url)
182
    {
183
        return sprintf('/%s/%s', $this->getEndpointPrefix(), $url);
184
    }
185
186
    /**
187
     * @inheritdoc
188
     */
189
    public function getDefaultLoginEndpoint()
190
    {
191
        return UrlHelper::siteUrl(
192
            $this->buildEndpointUrl(
193
                $this->loginEndpoint
194
            )
195
        );
196
    }
197
198
    /**
199
     * @inheritdoc
200
     */
201
    public function getDefaultLogoutEndpoint()
202
    {
203
        return UrlHelper::siteUrl(
204
            $this->buildEndpointUrl($this->logoutEndpoint)
205
        );
206
    }
207
208
    /**
209
     * @inheritdoc
210
     */
211
    public function getDefaultLogoutRequestEndpoint()
212
    {
213
        return UrlHelper::siteUrl(
214
            $this->buildEndpointUrl($this->logoutRequestEndpoint)
215
        );
216
    }
217
218
    /**
219
     * @inheritdoc
220
     */
221
    public function getDefaultLoginRequestEndpoint()
222
    {
223
        return UrlHelper::siteUrl(
224
            $this->buildEndpointUrl($this->loginRequestEndpoint)
225
        );
226
    }
227
228
    /**
229
     * @inheritdoc
230
     */
231
    public function getDefaultLoginPath()
232
    {
233
        return $this->buildEndpointUrl($this->loginEndpoint);
234
    }
235
236
    /**
237
     * @inheritdoc
238
     */
239
    public function getDefaultLogoutPath()
240
    {
241
        return $this->buildEndpointUrl($this->logoutEndpoint);
242
    }
243
244
    /**
245
     * @inheritdoc
246
     */
247
    public function getDefaultLogoutRequestPath()
248
    {
249
        return $this->buildEndpointUrl($this->logoutRequestEndpoint);
250
    }
251
252
    /**
253
     * @inheritdoc
254
     */
255
    public function getDefaultLoginRequestPath()
256
    {
257
        return
258
            $this->buildEndpointUrl($this->loginRequestEndpoint);
259
    }
260
}
261