1
|
|
|
<?php namespace FreedomCore\TrinityCore\Console\Abstracts; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class BaseClient |
5
|
|
|
* @package FreedomCore\TrinityCore\Console\Abstracts |
6
|
|
|
*/ |
7
|
|
|
abstract class BaseClient |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Package Version |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
const VERSION = '1.0.5'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Server Address |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $serverAddress = '127.0.0.1'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Server Port |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
protected $serverPort = 7878; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* SoapClient Instance |
30
|
|
|
* @var null|\SoapClient |
31
|
|
|
*/ |
32
|
|
|
protected $client = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Username used to connect to the server |
36
|
|
|
* @var null|string |
37
|
|
|
*/ |
38
|
|
|
private $username = null; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Password used to connect to the server |
42
|
|
|
* @var null|string |
43
|
|
|
*/ |
44
|
|
|
private $password = null; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* BaseClient constructor. |
48
|
|
|
* @param string $username Username used to connect to the server |
49
|
|
|
* @param string $password Password used to connect to the server |
50
|
|
|
* @param boolean $createNow Should the connection be created as soon as possible |
51
|
|
|
* @throws \Exception |
52
|
|
|
*/ |
53
|
38 |
|
public function __construct(string $username, string $password, bool $createNow = true) |
54
|
|
|
{ |
55
|
38 |
|
$this->isSoapEnabled(); |
56
|
38 |
|
$this->username = $username; |
57
|
38 |
|
$this->password = $password; |
58
|
38 |
|
$this->validateSettings(); |
59
|
38 |
|
if ($createNow) { |
60
|
38 |
|
$this->createConnection(); |
61
|
|
|
} |
62
|
38 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Set username variable |
66
|
|
|
* @param string $username |
67
|
|
|
*/ |
68
|
2 |
|
public function setUsername(string $username) |
69
|
|
|
{ |
70
|
2 |
|
$this->username = $username; |
71
|
2 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get username variable |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
4 |
|
public function getUsername() : string |
78
|
|
|
{ |
79
|
4 |
|
return $this->username; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Set password variable |
84
|
|
|
* @param string $password |
85
|
|
|
*/ |
86
|
2 |
|
public function setPassword(string $password) |
87
|
|
|
{ |
88
|
2 |
|
$this->password = $password; |
89
|
2 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get password variable |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
4 |
|
public function getPassword() : string |
96
|
|
|
{ |
97
|
4 |
|
return $this->password; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Set Server Address |
102
|
|
|
* @param string $serverAddress |
103
|
|
|
* @return BaseClient |
104
|
|
|
*/ |
105
|
2 |
|
public function setAddress(string $serverAddress) : BaseClient |
106
|
|
|
{ |
107
|
2 |
|
$this->serverAddress = $serverAddress; |
108
|
2 |
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get Server Address |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
2 |
|
public function getAddress() : string |
116
|
|
|
{ |
117
|
2 |
|
return $this->serverAddress; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Set Server Port |
122
|
|
|
* @param int $serverPort |
123
|
|
|
* @return BaseClient |
124
|
|
|
*/ |
125
|
2 |
|
public function setPort(int $serverPort) : BaseClient |
126
|
|
|
{ |
127
|
2 |
|
$this->serverPort = $serverPort; |
128
|
2 |
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get Server Port |
133
|
|
|
* @return int |
134
|
|
|
*/ |
135
|
2 |
|
public function getPort() : int |
136
|
|
|
{ |
137
|
2 |
|
return $this->serverPort; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Get Client Version |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
2 |
|
public function getVersion() : string |
145
|
|
|
{ |
146
|
2 |
|
return BaseClient::VERSION; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Initialize Connection To The Server |
151
|
|
|
*/ |
152
|
38 |
|
public function createConnection() |
153
|
|
|
{ |
154
|
38 |
|
$this->client = new \SoapClient(null, [ |
155
|
38 |
|
'location' => 'http://' . $this->serverAddress . ':' . $this->serverPort . '/', |
156
|
38 |
|
'uri' => 'urn:TC', |
157
|
38 |
|
'login' => $this->username, |
158
|
38 |
|
'password' => $this->password, |
159
|
38 |
|
'style' => SOAP_RPC, |
160
|
|
|
'keep_alive' => false |
161
|
|
|
]); |
162
|
38 |
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Get Client Instance |
166
|
|
|
* @return \SoapClient |
167
|
|
|
*/ |
168
|
8 |
|
public function getClient() : \SoapClient |
169
|
|
|
{ |
170
|
8 |
|
return $this->client; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Check if SOAP extension is enabled |
175
|
|
|
* @throws \Exception |
176
|
|
|
* @codeCoverageIgnore |
177
|
|
|
*/ |
178
|
|
|
protected function isSoapEnabled() |
179
|
|
|
{ |
180
|
|
|
if (!extension_loaded('soap')) { |
181
|
|
|
throw new \Exception('FreedomNet requires SOAP extension to be enabled.' . PHP_EOL . 'Please enable SOAP extension'); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Validate Connection Settings |
187
|
|
|
* @codeCoverageIgnore |
188
|
|
|
*/ |
189
|
|
|
protected function validateSettings() |
190
|
|
|
{ |
191
|
|
|
if ($this->serverAddress === null) { |
192
|
|
|
throw new \RuntimeException('SOAP Address cannot be null!'); |
193
|
|
|
} |
194
|
|
|
if ($this->serverPort === null) { |
195
|
|
|
throw new \RuntimeException('SOAP Port cannot be null!'); |
196
|
|
|
} |
197
|
|
|
if ($this->username === null) { |
198
|
|
|
throw new \RuntimeException('SOAP Username cannot be null!'); |
199
|
|
|
} |
200
|
|
|
if ($this->password === null) { |
201
|
|
|
throw new \RuntimeException('SOAP Password cannot be null!'); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|