1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Laravel WuBook. |
5
|
|
|
* |
6
|
|
|
* (c) Filippo Galante <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace IlGala\LaravelWubook\Api; |
13
|
|
|
|
14
|
|
|
use IlGala\LaravelWubook\Api\WuBookAuth; |
15
|
|
|
use IlGala\LaravelWubook\Exceptions\WuBookException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* This is the WuBook api abstract class. |
19
|
|
|
* |
20
|
|
|
* @author ilgala |
21
|
|
|
*/ |
22
|
|
|
abstract class WuBookApi |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $config; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Illuminate\Cache\Repository |
32
|
|
|
*/ |
33
|
|
|
protected $cache; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var fXmlRpc\Client |
37
|
|
|
*/ |
38
|
|
|
protected $client; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var IlGala\LaravelWubook\Api\WuBookAuth |
42
|
|
|
*/ |
43
|
|
|
protected $auth; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Creates a new WuBookAuth instance. |
47
|
|
|
* |
48
|
|
|
* @param array $config |
49
|
|
|
* @param Illuminate\Cache\Repository $cache |
50
|
|
|
* @param fXmlRpc\Client $client |
51
|
|
|
*/ |
52
|
|
|
public function __construct($config, Repository $cache, Client $client) |
53
|
|
|
{ |
54
|
|
|
$this->config = $config; |
55
|
|
|
$this->cache = $cache; |
|
|
|
|
56
|
|
|
$this->client = $client; |
|
|
|
|
57
|
|
|
$this->auth = new WuBookAuth($config, $cache, $client); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Prepends token and lcode or custom parameters |
62
|
|
|
* |
63
|
|
|
* @param mixed $params |
64
|
|
|
*/ |
65
|
|
|
protected function setup_client($params) |
66
|
|
|
{ |
67
|
|
|
if (!is_array($params)) { |
68
|
|
|
// Setup params |
69
|
|
|
$params = [ |
|
|
|
|
70
|
|
|
$this->get_token($params), |
71
|
|
|
$this->config['lcode'] |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->client->prependParams($token); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Validate and, if necessary, retrieves a token or the cached token |
80
|
|
|
* |
81
|
|
|
* @param string $token |
82
|
|
|
* @return string |
83
|
|
|
* @throws WuBookException |
84
|
|
|
*/ |
85
|
|
|
protected function get_token($token) |
86
|
|
|
{ |
87
|
|
|
// Check token |
88
|
|
|
if (empty($token) && $this->config['cache_token']) { |
89
|
|
|
$token = $this->cache->get('wubook.token'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$response = $this->auth->is_token_valid($token, $this->config['cache_token']); |
93
|
|
|
|
94
|
|
|
if (is_int($response)) { |
95
|
|
|
// If response is integer => valid token |
96
|
|
|
return $token; |
97
|
|
|
} elseif (is_string($response)) { |
98
|
|
|
// If response is string => new token |
99
|
|
|
return $response; |
100
|
|
|
} else { |
101
|
|
|
// Error |
102
|
|
|
throw new WuBookException('Token is empty or invalid'); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Calls a wired API function. |
108
|
|
|
* |
109
|
|
|
* @param string $token |
110
|
|
|
* @param string $method |
111
|
|
|
* @param array $data |
112
|
|
|
* @return array |
113
|
|
|
* @throws WuBookException |
114
|
|
|
*/ |
115
|
|
|
protected function call_method($token, $method, $data = []) |
116
|
|
|
{ |
117
|
|
|
if ($this->config['cache_token']) { |
118
|
|
|
// Check total operations |
119
|
|
|
$operations = $this->cache->get('wubook.token.ops'); |
120
|
|
|
$operations++; |
121
|
|
|
|
122
|
|
|
if ($operations >= 60) { |
123
|
|
|
// Exceeded max ops, renew token |
124
|
|
|
$this->auth->release_token($this->get_token($token)); |
125
|
|
|
$this->auth->acquire_token(); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// Setup client |
130
|
|
|
$this->setup_client($token); |
131
|
|
|
|
132
|
|
|
try { |
133
|
|
|
// Retrieve response |
134
|
|
|
$response = $this->client->call($method, $data); |
135
|
|
|
|
136
|
|
|
return [ |
137
|
|
|
'has_error' => $response[0] != 0, |
138
|
|
|
'data' => $response[1] |
139
|
|
|
]; |
140
|
|
|
} catch (AbstractTransportException $error) { |
|
|
|
|
141
|
|
|
throw new WuBookException($error->getMessage(), $error->getCode(), $error); |
142
|
|
|
} finally { |
143
|
|
|
if ($this->config['cache_token']) { |
144
|
|
|
// Increment total operations |
145
|
|
|
$this->cache->increment('wubook.token.ops'); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..