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

AuthenticationToken   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 104
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 3
A getAuthMode() 0 4 1
A getSharedSecretKey() 0 4 1
A getUsername() 0 4 1
A getPassword() 0 4 1
A getAuthToken() 0 16 3
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;
15
16
/**
17
 * Class AuthenticationToken
18
 * @package Gnello\OpenFireRestAPI
19
 * @link https://www.igniterealtime.org/projects/openfire/plugins/restapi/readme.html
20
 */
21
class AuthenticationToken
22
{
23
    /**
24
     * Authentication constants, do not touch ;)
25
     */
26
    const AUTH_BASE         = 'basic';
27
    const AUTH_SECRET_KEY   = 'secret_key';
28
29
    /**
30
     * @var string
31
     */
32
    private $authMode;
33
34
    /**
35
     * @var string
36
     */
37
    private $username;
38
39
    /**
40
     * @var string
41
     */
42
    private $password;
43
44
    /**
45
     * @var string
46
     */
47
    private $sharedSecretKey;
48
49
    /**
50
     * AuthenticationToken constructor.
51
     */
52
    public function __construct()
53
    {
54
        $arguments = func_get_args();
55
        $num = func_num_args();
56
57
        switch ($num) {
58
            case 1:
59
                $this->authMode = self::AUTH_SECRET_KEY;
60
                $this->sharedSecretKey = $arguments[0];
61
                break;
62
            case 2:
63
                $this->authMode = self::AUTH_BASE;
64
                $this->username = $arguments[0];
65
                $this->password = $arguments[1];
66
                break;
67
            default:
68
                break;
69
        }
70
    }
71
72
    /**
73
     * Return type of authentication.
74
     * @return string
75
     */
76
    public function getAuthMode()
77
    {
78
        return $this->authMode;
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function getSharedSecretKey()
85
    {
86
        return $this->sharedSecretKey;
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    public function getUsername()
93
    {
94
        return $this->username;
95
    }
96
97
    /**
98
     * @return string
99
     */
100
    public function getPassword()
101
    {
102
        return $this->password;
103
    }
104
105
    /**
106
     * @return null|string
107
     */
108
    public function getAuthToken()
109
    {
110
        switch ($this->getAuthMode()) {
111
            case self::AUTH_BASE:
112
                $authToken = "basic " . base64_encode($this->getUsername() . ":" . $this->getPassword());
113
                break;
114
            case self::AUTH_SECRET_KEY:
115
                $authToken = $this->getSharedSecretKey();
116
                break;
117
            default:
118
                $authToken = null;
119
                break;
120
        }
121
122
        return $authToken;
123
    }
124
}