Completed
Push — master ( a3f58b...5e1e7e )
by Luca
02:14
created

Settings::setSecret()   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 1
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\Wrappers\AbstractRegistryWrapper;
17
18
/**
19
 * Class Settings
20
 * @package Gnello\OpenFireRestAPI\Settings
21
 */
22
class Settings extends AbstractRegistryWrapper
23
{
24
    /**
25
     * Default Settings
26
     * Edit this section to configure your client
27
     */
28
    const HOST          = 'localhost';
29
    const PORT          = '9090';
30
    const PLUGIN        = '/plugins/restapi/v1';
31
    const AUTH          = 'secret_key';
32
33
    /**
34
     * Settings that you need to change
35
     * Edit this section to configure your client
36
     */
37
    const SERVER_NAME   = 'your_server_name';
38
    const SECRET_KEY    = 'your_secret_key';    //if you use secret_key authentication
39
    const USER          = 'your_username';      //if you use basic authentication
40
    const PSW           = 'your_password';      //if you use basic authentication
41
42
    /**
43
     * Authentication constants, do not touch ;)
44
     */
45
    const AUTH_BASE         = 'basic';
46
    const AUTH_SECRET_KEY   = 'secret_key';
47
48
    /**
49
     * @var Settings
50
     */
51
    private static $instance;
52
53
    /**
54
     * Settings constructor.
55
     */
56
    private function __construct() {}
57
58
    /**
59
     * @return Settings
60
     */
61
    public static function getInstance()
62
    {
63
        if (is_null(self::$instance)) {
64
            $settings = new Settings();
65
            $settings->setAuth(self::AUTH);
66
            $settings->setHost(self::HOST);
67
            $settings->setPort(self::PORT);
68
            $settings->setPlugin(self::PLUGIN);
69
            $settings->setSecretKey(self::SECRET_KEY);
70
            $settings->setServerName(self::SERVER_NAME);
71
            $settings->setSSL(false);
72
            $settings->setDebug(false);
73
74
            self::$instance = $settings;
75
        }
76
77
        return self::$instance;
78
    }
79
80
    /**
81
     * Set type of authentication.
82
     * Possible values are 'basic' or 'secret_key'
83
     * @param $auth
84
     * @return mixed
85
     * @throws \Exception
86
     */
87
    public function setAuth($auth)
88
    {
89
        return $this->set('auth', $auth);
90
    }
91
92
    /**
93
     * @param $host
94
     * @return string
95
     */
96
    public function setHost($host)
97
    {
98
        return $this->set('host', $host);
99
    }
100
101
    /**
102
     * @param $port
103
     * @return string
104
     */
105
    public function setPort($port)
106
    {
107
        return $this->set('port', $port);
108
    }
109
110
    /**
111
     * @param $plugin
112
     * @return string
113
     */
114
    public function setPlugin($plugin)
115
    {
116
        return $this->set('plugin', $plugin);
117
    }
118
119
    /**
120
     * @param $useSSL
121
     * @return bool
122
     */
123
    public function setSSL($useSSL)
124
    {
125
        return $this->set('useSSL', $useSSL);
126
    }
127
128
    /**
129
     * @param $secretKey
130
     * @return string
131
     */
132
    public function setSecretKey($secretKey)
133
    {
134
        return $this->set('secret_key', $secretKey);
135
    }
136
137
    /**
138
     * @param $user
139
     * @return string
140
     */
141
    public function setUser($user)
142
    {
143
        return $this->set('user', $user);
144
    }
145
146
    /**
147
     * @param $psw
148
     * @return string
149
     */
150
    public function setPsw($psw)
151
    {
152
        return $this->set('psw', $psw);
153
    }
154
155
    /**
156
     * @param $serverName
157
     * @return string
158
     */
159
    public function setServerName($serverName)
160
    {
161
        return $this->set('serverName', $serverName);
162
    }
163
164
    /**
165
     * @param $bool
166
     * @return string
167
     */
168
    public function setDebug($bool)
169
    {
170
        return $this->set('debug', $bool);
171
    }
172
173
    /**
174
     * Return type of authentication.
175
     * @return string
176
     */
177
    public function getAuth()
178
    {
179
        return $this->get('auth');
180
    }
181
182
    /**
183
     * @return string
184
     */
185
    public function getHost()
186
    {
187
        return $this->get('host');
188
    }
189
190
    /**
191
     * @return string
192
     */
193
    public function getPort()
194
    {
195
        return $this->get('port');
196
    }
197
198
    /**
199
     * @return string
200
     */
201
    public function getPlugin()
202
    {
203
        return $this->get('plugin');
204
    }
205
206
    /**
207
     * @return bool
208
     */
209
    public function isSSL()
210
    {
211
        return $this->get('useSSL');
212
    }
213
214
    /**
215
     * @return string
216
     */
217
    public function getSecretKey()
218
    {
219
        return $this->get('secret_key');
220
    }
221
222
    /**
223
     * @return string
224
     */
225
    public function getUser()
226
    {
227
        return $this->get('user');
228
    }
229
230
    /**
231
     * @return string
232
     */
233
    public function getPsw()
234
    {
235
        return $this->get('psw');
236
    }
237
238
    /**
239
     * @return string
240
     */
241
    public function getServerName()
242
    {
243
        return $this->get('serverName');
244
    }
245
246
    /**
247
     * @return bool
248
     */
249
    public function isDebug()
250
    {
251
        return $this->get('debug');
252
    }
253
254
    /**
255
     * Returns the URL under which query the webservice
256
     * @return string
257
     */
258
    public function getBaseURL()
259
    {
260
        $base = ($this->isSSL()) ? "https" : "http";
261
        return $base . "://" . $this->getHost() . ":" . $this->getPort() . $this->getPlugin();
262
    }
263
264
    /**
265
     * Returns the headers to be sent to web service
266
     * @return array
267
     * @throws \Exception
268
     */
269
    public function getHeaders()
270
    {
271
        $auth = $this->getAuth();
272
273
        switch ($auth) {
274
            case self::AUTH_BASE:
275
                $authHeader = "basic " . base64_encode($this->getUser() . ":" . $this->getPsw());
276
                break;
277
            case self::AUTH_SECRET_KEY:
278
                $authHeader = $this->getSecretKey();
279
                break;
280
            default:
281
                $authHeader = "Unrecognized auth [{$auth}]! Must be 'basic' or 'secret_key'";
282
                break;
283
        }
284
285
        return array(
286
            'Accept: application/json',
287
            'Authorization: ' . $authHeader,
288
            'Content-Type: application/json',
289
        );
290
    }
291
}
292