1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Radowoj\Yaah; |
4
|
|
|
|
5
|
|
|
use stdClass; |
6
|
|
|
use SoapClient; |
7
|
|
|
use SoapFault; |
8
|
|
|
use Radowoj\Yaah\Constants\Sysvars; |
9
|
|
|
|
10
|
|
|
class Client |
11
|
|
|
{ |
12
|
|
|
protected $config = null; |
13
|
|
|
|
14
|
|
|
protected $soapClient = null; |
15
|
|
|
|
16
|
|
|
protected $allegroSessionHandle = null; |
17
|
|
|
|
18
|
|
|
protected $allegroUserId = null; |
19
|
|
|
|
20
|
|
|
protected $localVersionKey = null; |
21
|
|
|
|
22
|
22 |
|
public function __construct(Config $config, SoapClient $soapClient) |
23
|
|
|
{ |
24
|
22 |
|
$this->config = $config; |
25
|
22 |
|
$this->soapClient = $soapClient; |
26
|
22 |
|
} |
27
|
|
|
|
28
|
|
|
|
29
|
7 |
|
protected function getLocalVersionKey() |
30
|
|
|
{ |
31
|
7 |
|
if (is_null($this->localVersionKey)) { |
32
|
7 |
|
$response = $this->soapClient->doQuerySysStatus([ |
33
|
7 |
|
'sysvar' => Sysvars::SYSVAR_CATEGORY_TREE, |
34
|
7 |
|
'countryId' => $this->config->getCountryCode(), |
35
|
7 |
|
'webapiKey' => $this->config->getApiKey(), |
36
|
7 |
|
]); |
37
|
|
|
|
38
|
7 |
|
if (!isset($response->verKey)) { |
39
|
1 |
|
throw new Exception("Invalid WebAPI doQuerySysStatus() response: " . print_r($response, 1)); |
40
|
|
|
} |
41
|
|
|
|
42
|
6 |
|
$this->localVersionKey = $response->verKey; |
43
|
6 |
|
} |
44
|
|
|
|
45
|
6 |
|
return $this->localVersionKey; |
46
|
|
|
} |
47
|
|
|
|
48
|
7 |
|
protected function getWebApiRequest($data) |
49
|
|
|
{ |
50
|
7 |
|
return array_merge($data, [ |
51
|
7 |
|
'webapiKey' => $this->config->getApiKey(), |
52
|
7 |
|
'localVersion' => $this->getLocalVersionKey(), |
53
|
|
|
|
54
|
|
|
//for some methods... |
55
|
6 |
|
'countryId' => $this->config->getCountryCode(), |
56
|
6 |
|
'sessionId' => $this->allegroSessionHandle, |
57
|
|
|
|
58
|
|
|
//...for some other methods |
59
|
6 |
|
'countryCode' => $this->config->getCountryCode(), |
60
|
6 |
|
'sessionHandle' => $this->allegroSessionHandle, |
61
|
6 |
|
]); |
62
|
|
|
} |
63
|
|
|
|
64
|
7 |
|
public function login() |
65
|
|
|
{ |
66
|
|
|
$data = [ |
67
|
7 |
|
'userLogin' => $this->config->getLogin() |
68
|
7 |
|
]; |
69
|
|
|
|
70
|
7 |
|
if ($this->config->getPasswordHash()) { |
71
|
5 |
|
$data['userHashPassword'] = $this->config->getPasswordHash(); |
72
|
5 |
|
$response = $this->soapClient->doLoginEnc($this->getWebApiRequest($data)); |
73
|
4 |
|
} else { |
74
|
|
|
//ugly non-encrypted way, but sometimes sandbox fails to accept correct hashed login data |
75
|
2 |
|
$data['userPassword'] = $this->config->getPasswordPlain(); |
76
|
2 |
|
$response = $this->soapClient->doLogin($this->getWebApiRequest($data)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
//validate basic response properties |
80
|
6 |
|
if (!is_object($response) || !isset($response->sessionHandlePart) || !isset($response->userId)) { |
81
|
2 |
|
throw new Exception("Invalid WebAPI doLogin[Enc]() response: " . print_r($response, 1)); |
82
|
|
|
} |
83
|
|
|
|
84
|
4 |
|
$this->allegroSessionHandle = $response->sessionHandlePart; |
85
|
4 |
|
$this->allegroUserId = $response->userId; |
86
|
4 |
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
4 |
|
public function __call($name, $args) |
90
|
|
|
{ |
91
|
4 |
|
if (!$this->allegroUserId || !$this->allegroSessionHandle) { |
92
|
4 |
|
$this->login(); |
93
|
4 |
|
} |
94
|
|
|
|
95
|
4 |
|
$request = $this->getWebApiRequest( |
96
|
4 |
|
array_key_exists(0, $args) ? $args[0] : [] |
97
|
4 |
|
); |
98
|
|
|
|
99
|
|
|
try { |
100
|
4 |
|
return $this->soapClient->{$name}($request); |
101
|
1 |
|
} catch (SoapFault $e) { |
102
|
1 |
|
throw new Exception("Trying to call: {$name}(); WebAPI exception: {$e->getMessage()}"); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|