Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
55 / 55 |
|
100.00% |
11 / 11 |
CRAP | |
100.00% |
1 / 1 |
| SocketConfig | |
100.00% |
55 / 55 |
|
100.00% |
11 / 11 |
29 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
| getPOSTData | |
100.00% |
25 / 25 |
|
100.00% |
1 / 1 |
19 | |||
| getSession | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSystemEntity | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setLogin | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| setOTP | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| setPassword | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| setRemoteAddress | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setSession | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| setSystemEntity | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| setUser | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | #declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * CNIC\HEXONET |
| 7 | * Copyright © CentralNic Group PLC |
| 8 | */ |
| 9 | |
| 10 | namespace CNIC\HEXONET; |
| 11 | |
| 12 | /** |
| 13 | * HEXONET SocketConfig |
| 14 | * |
| 15 | * @package CNIC\HEXONET |
| 16 | */ |
| 17 | |
| 18 | class SocketConfig |
| 19 | { |
| 20 | /** |
| 21 | * API system entity. "54cd" for LIVE system; "1234" for OT&E system |
| 22 | * @var string |
| 23 | */ |
| 24 | private $entity; |
| 25 | /** |
| 26 | * account name |
| 27 | * @var string |
| 28 | */ |
| 29 | private $login; |
| 30 | /** |
| 31 | * one time password (2FA) |
| 32 | * @var string |
| 33 | */ |
| 34 | private $otp; |
| 35 | /** |
| 36 | * account password |
| 37 | * @var string |
| 38 | */ |
| 39 | private $pw; |
| 40 | /** |
| 41 | * remote ip address (ip filter) |
| 42 | * @var string |
| 43 | */ |
| 44 | private $remoteaddr; |
| 45 | /** |
| 46 | * API session id |
| 47 | * @var string |
| 48 | */ |
| 49 | private $session; |
| 50 | /** |
| 51 | * subuser account name (subuser specific data view) |
| 52 | * @var string |
| 53 | */ |
| 54 | private $user; |
| 55 | /** |
| 56 | * list of http request parameters |
| 57 | * @var array |
| 58 | */ |
| 59 | private $parameters; |
| 60 | |
| 61 | public function __construct(array $parameters) |
| 62 | { |
| 63 | $this->parameters = $parameters; |
| 64 | $this->entity = ""; |
| 65 | $this->login = ""; |
| 66 | $this->otp = ""; |
| 67 | $this->pw = ""; |
| 68 | $this->remoteaddr = ""; |
| 69 | $this->session = ""; |
| 70 | $this->user = ""; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Create POST data string out of connection data |
| 75 | * @param array $command API Command to request |
| 76 | * @param bool $secured if password has to be returned "hidden" |
| 77 | * @return string POST data string |
| 78 | */ |
| 79 | public function getPOSTData($command = [], $secured = false) |
| 80 | { |
| 81 | $params = []; |
| 82 | if (strlen($this->entity) && isset($this->parameters["entity"])) { |
| 83 | $params[$this->parameters["entity"]] = $this->entity; |
| 84 | } |
| 85 | if (strlen($this->login)) { |
| 86 | $params[$this->parameters["login"]] = $this->login; |
| 87 | } |
| 88 | if (strlen($this->otp) && isset($this->parameters["otp"])) { |
| 89 | $params[$this->parameters["otp"]] = $this->otp; |
| 90 | } |
| 91 | if (strlen($this->pw)) { |
| 92 | $params[$this->parameters["password"]] = $secured ? "***" : $this->pw; |
| 93 | } |
| 94 | if (strlen($this->remoteaddr) && isset($this->parameters["ipfilter"])) { |
| 95 | $params[$this->parameters["ipfilter"]] = $this->remoteaddr; |
| 96 | } |
| 97 | if (strlen($this->session)) { |
| 98 | $params[$this->parameters["session"]] = $this->session; |
| 99 | } |
| 100 | if (strlen($this->user) && isset($this->parameters["subuser"])) { |
| 101 | $params[$this->parameters["subuser"]] = $this->user; |
| 102 | } |
| 103 | if (!empty($command) && isset($this->parameters["command"])) { |
| 104 | $newcommand = ""; |
| 105 | foreach ($command as $key => $val) { |
| 106 | if (is_null($val)) { |
| 107 | continue; |
| 108 | } |
| 109 | if ($secured && preg_match("/^PASSWORD$/i", $key)) { |
| 110 | $val = "***"; |
| 111 | } |
| 112 | $newcommand .= "{$key}={$val}\n"; |
| 113 | } |
| 114 | $params[$this->parameters["command"]] = substr($newcommand, 0, -1); |
| 115 | } |
| 116 | return http_build_query($params);//RFC1738 x-www-form-urlencoded as default |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Get API Session ID in use |
| 121 | * @return string API Session ID |
| 122 | */ |
| 123 | public function getSession() |
| 124 | { |
| 125 | return $this->session; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Get API System Entity in use |
| 130 | * @return string API System Entity |
| 131 | */ |
| 132 | public function getSystemEntity() |
| 133 | { |
| 134 | return $this->entity; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Set account name to use |
| 139 | * @param string $value account name |
| 140 | * @return $this |
| 141 | */ |
| 142 | public function setLogin($value) |
| 143 | { |
| 144 | $this->session = ""; |
| 145 | $this->login = $value; |
| 146 | return $this; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Set one time password to use |
| 151 | * @param string $value one time password |
| 152 | * @return $this |
| 153 | */ |
| 154 | public function setOTP($value) |
| 155 | { |
| 156 | $this->session = ""; |
| 157 | $this->otp = $value; |
| 158 | return $this; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Set account password to use |
| 163 | * @param string $value account password |
| 164 | * @return $this |
| 165 | */ |
| 166 | public function setPassword($value) |
| 167 | { |
| 168 | $this->session = ""; |
| 169 | $this->pw = $value; |
| 170 | return $this; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Set Remote IP Address to use |
| 175 | * @param string $value remote ip address |
| 176 | * @return $this |
| 177 | */ |
| 178 | public function setRemoteAddress($value) |
| 179 | { |
| 180 | $this->remoteaddr = $value; |
| 181 | return $this; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Set API Session ID to use |
| 186 | * @param string $value API Session ID |
| 187 | * @return $this |
| 188 | */ |
| 189 | public function setSession($value) |
| 190 | { |
| 191 | $this->session = $value; |
| 192 | $this->login = ""; |
| 193 | $this->pw = ""; |
| 194 | $this->otp = ""; |
| 195 | return $this; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Set API System Entity to use |
| 200 | * This is set to 54cd / LIVE System by default |
| 201 | * @param string $value API System Entity |
| 202 | * @return $this |
| 203 | */ |
| 204 | public function setSystemEntity($value) |
| 205 | { |
| 206 | $this->entity = $value; |
| 207 | return $this; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Set subuser account name (for subuser data view) |
| 212 | * @param string $value subuser account name |
| 213 | * @return $this |
| 214 | */ |
| 215 | public function setUser($value) |
| 216 | { |
| 217 | $this->user = $value; |
| 218 | return $this; |
| 219 | } |
| 220 | } |