1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* xOrder Session. |
5
|
|
|
* |
6
|
|
|
* @package craftt/xorder-sdk |
7
|
|
|
* @author Ryan Stratton <[email protected]> |
8
|
|
|
* @copyright Copyright (c) Ryan Stratton |
9
|
|
|
* @license https://github.com/craftt/xorder-php-sdk/blob/master/LICENSE.md Apache 2.0 |
10
|
|
|
* @link https://github.com/craftt/xorder-php-sdk |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace XOrder; |
14
|
|
|
|
15
|
|
|
use SimpleXMLElement; |
16
|
|
|
use XOrder\Contracts\SessionInterface; |
17
|
|
|
use XOrder\Exceptions\InvalidCredentialsException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Session |
21
|
|
|
*/ |
22
|
|
|
class Session implements SessionInterface |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var \SimpleXMLElement |
27
|
|
|
*/ |
28
|
|
|
public $data; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constructor |
32
|
|
|
* |
33
|
|
|
* @param string|null $response |
34
|
|
|
*/ |
35
|
16 |
|
public function __construct($response) |
36
|
|
|
{ |
37
|
16 |
|
$this->set($response); |
38
|
|
|
|
39
|
16 |
|
if ($this->hasError()) { |
40
|
2 |
|
throw new InvalidCredentialsException($this->data->error); |
41
|
|
|
} |
42
|
14 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Destroy the session data. |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
2 |
|
public function destroy() |
50
|
|
|
{ |
51
|
2 |
|
$this->data = null; |
52
|
2 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the BCLDB account number. |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
2 |
|
public function getAccount() |
60
|
|
|
{ |
61
|
2 |
|
return (string) $this->data->bcldbNum; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the BCLDB account number. |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
2 |
|
public function getBcldbNum() |
70
|
|
|
{ |
71
|
2 |
|
return $this->getAccount(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the session id. |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
2 |
|
public function getId() |
80
|
|
|
{ |
81
|
2 |
|
return (string) $this->data->sessionId; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the xOrder message. |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
2 |
|
public function getMessage() |
90
|
|
|
{ |
91
|
2 |
|
return (string) $this->data->xOrderMessage; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get the username. |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
2 |
|
public function getUsername() |
100
|
|
|
{ |
101
|
2 |
|
return (string) $this->data->username; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Check of the session has an error. |
106
|
|
|
* |
107
|
|
|
* @return boolean |
108
|
|
|
*/ |
109
|
16 |
|
public function hasError() |
110
|
|
|
{ |
111
|
16 |
|
return property_exists($this->data, 'error'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Check if the session is valid. |
116
|
|
|
* |
117
|
|
|
* @return boolean |
118
|
|
|
*/ |
119
|
4 |
|
public function isValid() |
120
|
|
|
{ |
121
|
4 |
|
return ($this->data instanceof SimpleXMLElement |
122
|
4 |
|
&& property_exists($this->data, 'userstatus') |
123
|
4 |
|
&& (string) $this->data->userstatus === 'valid'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Create the session from a response. |
128
|
|
|
* |
129
|
|
|
* @param string $response |
130
|
|
|
* @throws \XOrder\InvalidCredentialsException |
131
|
|
|
* @return \XOrder\Session |
132
|
|
|
*/ |
133
|
16 |
|
public function set($response) |
134
|
|
|
{ |
135
|
16 |
|
$this->data = new SimpleXMLElement($response); |
136
|
16 |
|
return $this; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|