Completed
Push — master ( 0768ef...ea365c )
by Luca
03:38
created

Settings::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * OpenFireRestAPI is based entirely on official documentation of the REST API
4
 * Plugin and you can extend it by following the directives of the documentation
5
 *
6
 * For the full copyright and license information, please read the LICENSE
7
 * file that was distributed with this source code. For the full list of
8
 * contributors, visit https://github.com/gnello/PHPOpenFireRestAPI/contributors
9
 *
10
 * @author Luca Agnello <[email protected]>
11
 * @link https://www.igniterealtime.org/projects/openfire/plugins/restapi/readme.html
12
 */
13
14
namespace Gnello\OpenFireRestAPI\Settings;
15
16
use Gnello\OpenFireRestAPI\AuthenticationToken;
17
use Gnello\OpenFireRestAPI\Wrappers\AbstractRegistryWrapper;
18
19
/**
20
 * Class Settings
21
 * @package Gnello\OpenFireRestAPI\Settings
22
 */
23
class Settings extends AbstractRegistryWrapper
24
{
25
    /**
26
     * Default Settings
27
     * Edit this section to configure your client
28
     */
29
    const PLUGIN_PATH   = '/plugins/restapi/v1';
30
31
    /**
32
     * @var Settings
33
     */
34
    private static $instance;
35
36
    /**
37
     * Settings constructor.
38
     */
39
    private function __construct() {}
40
41
    /**
42
     * @return Settings
43
     */
44
    public static function getInstance()
45
    {
46
        if (is_null(self::$instance)) {
47
            $settings = new Settings();
48
            $settings->setPlugin(self::PLUGIN_PATH);
49
            $settings->setSSL(false);
50
            $settings->setDebug(false);
51
52
            self::$instance = $settings;
53
        }
54
55
        return self::$instance;
56
    }
57
58
    /**
59
     * Destroy instance
60
     */
61
    public function destroy()
62
    {
63
        self::$instance = null;
64
    }
65
66
    /**
67
     * @param $host
68
     * @return string
69
     */
70 View Code Duplication
    public function setHost($host)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        if (!is_string($host)) {
73
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Gnello\OpenFireRestAPI\Settings\Settings::setHost 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...
74
        }
75
76
        $parsed_host = parse_url($host, PHP_URL_HOST);
77
78
        if (is_null($parsed_host)) {
79
            $parsed_host = $host;
80
        } else {
81
            $scheme = parse_url($host, PHP_URL_SCHEME);
82
            $this->setSSL($scheme === 'https');
83
        }
84
85
        return $this->set('host', $parsed_host);
86
    }
87
88
    /**
89
     * @param $port
90
     * @return string
91
     */
92
    public function setPort($port)
93
    {
94
        return $this->set('port', $port);
95
    }
96
97
    /**
98
     * @param $plugin
99
     * @return string
100
     */
101
    public function setPlugin($plugin)
102
    {
103
        return $this->set('plugin', $plugin);
104
    }
105
106
    /**
107
     * @param $useSSL
108
     * @return bool
109
     */
110
    public function setSSL($useSSL)
111
    {
112
        return $this->set('useSSL', $useSSL);
113
    }
114
115
    /**
116
     * @param $serverName
117
     * @return string
118
     */
119
    public function setServerName($serverName)
120
    {
121
        return $this->set('serverName', $serverName);
122
    }
123
124
    /**
125
     * @param $host
126
     * @return string
127
     */
128 View Code Duplication
    public function setServerNameFromHost($host)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
    {
130
        if (!is_string($host)) {
131
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Gnello\OpenFireRestAPI\S...::setServerNameFromHost 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...
132
        }
133
134
        $parsed_host = parse_url($host, PHP_URL_HOST);
135
136
        if (is_null($parsed_host)) {
137
            $serverName = $host;
138
        } else {
139
            $serverName = preg_replace('/^www\./', '', $parsed_host);
140
        }
141
142
        return $this->set('serverName', $serverName);
143
    }
144
145
    /**
146
     * @param $bool
147
     * @return string
148
     */
149
    public function setDebug($bool)
150
    {
151
        return $this->set('debug', $bool);
152
    }
153
154
    /**
155
     * @param AuthenticationToken $authenticationToken
156
     * @return mixed
157
     */
158
    public function setAuthenticationToken(AuthenticationToken $authenticationToken)
159
    {
160
        return $this->set('authenticationToken', $authenticationToken);
161
    }
162
163
    /**
164
     * @return string
165
     */
166
    public function getHost()
167
    {
168
        return $this->get('host');
169
    }
170
171
    /**
172
     * @return string
173
     */
174
    public function getPort()
175
    {
176
        return $this->get('port');
177
    }
178
179
    /**
180
     * @return string
181
     */
182
    public function getPlugin()
183
    {
184
        return $this->get('plugin');
185
    }
186
187
    /**
188
     * @return bool
189
     */
190
    public function isSSL()
191
    {
192
        return $this->get('useSSL');
193
    }
194
195
    /**
196
     * @return string
197
     */
198
    public function getServerName()
199
    {
200
        return $this->get('serverName');
201
    }
202
203
    /**
204
     * @return bool
205
     */
206
    public function isDebug()
207
    {
208
        return $this->get('debug');
209
    }
210
211
    /**
212
     * @return AuthenticationToken
213
     */
214
    public function getAuthenticationToken()
215
    {
216
        return $this->get('authenticationToken');
217
    }
218
219
    /**
220
     * Returns the URL under which query the webservice
221
     * @return string
222
     */
223
    public function getBaseURL()
224
    {
225
        $base = ($this->isSSL()) ? "https" : "http";
226
        return $base . "://" . $this->getHost() . ":" . $this->getPort() . $this->getPlugin();
227
    }
228
229
    /**
230
     * Returns the headers to be sent to web service
231
     * @return array
232
     * @throws \Exception
233
     */
234
    public function getHeaders()
235
    {
236
        $authToken = $this->getAuthenticationToken()->getAuthToken();
237
238
        if (empty($authToken)) {
239
            $authToken = "Unrecognized authentication token!";
240
        }
241
242
        return array(
243
            'Accept: application/json',
244
            'Authorization: ' . $authToken,
245
            'Content-Type: application/json',
246
        );
247
    }
248
}
249