1 | <?php |
||
14 | class Client |
||
15 | { |
||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | const DEFAULT_HOST = 'localhost'; |
||
20 | |||
21 | /** |
||
22 | * @var integer |
||
23 | */ |
||
24 | const DEFAULT_PORT = 9091; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | const DEFAULT_PATH = '/transmission/rpc'; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | const TOKEN_HEADER = 'X-Transmission-Session-Id'; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $host = self::DEFAULT_HOST; |
||
40 | |||
41 | /** |
||
42 | * @var integer |
||
43 | */ |
||
44 | protected $port = self::DEFAULT_PORT; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $path = self::DEFAULT_PATH; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $token; |
||
55 | |||
56 | /** |
||
57 | * @var ClientInterface |
||
58 | */ |
||
59 | protected $client; |
||
60 | |||
61 | /** |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $auth; |
||
65 | |||
66 | /** |
||
67 | * Constructor |
||
68 | * |
||
69 | * @param string $host The hostname of the Transmission server |
||
70 | * @param integer $port The port the Transmission server is listening on |
||
71 | * @param string $path The path to Transmission server rpc api |
||
72 | */ |
||
73 | public function __construct($host = null, $port = null, $path = null) |
||
82 | |||
83 | /** |
||
84 | * Authenticate against the Transmission server |
||
85 | * |
||
86 | * @param string $username |
||
87 | * @param string $password |
||
88 | */ |
||
89 | public function authenticate($username, $password) |
||
93 | |||
94 | /** |
||
95 | * Make an API call |
||
96 | * |
||
97 | * @param string $method |
||
98 | * @param array $arguments |
||
99 | * @return \stdClass |
||
100 | * @throws \RuntimeException |
||
101 | */ |
||
102 | public function call($method, array $arguments) |
||
103 | { |
||
104 | list($request, $response) = $this->compose($method, $arguments); |
||
105 | |||
106 | try { |
||
107 | $this->getClient()->send($request, $response); |
||
108 | } catch (\Exception $e) { |
||
109 | throw new \RuntimeException( |
||
110 | 'Could not connect to Transmission', |
||
111 | 0, |
||
112 | $e |
||
113 | ); |
||
114 | } |
||
115 | |||
116 | return $this->validateResponse($response, $method, $arguments); |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Get the URL used to connect to Transmission |
||
121 | * |
||
122 | * @return string |
||
123 | */ |
||
124 | public function getUrl() |
||
125 | { |
||
126 | return sprintf( |
||
127 | 'http://%s:%d', |
||
128 | $this->getHost(), |
||
129 | $this->getPort() |
||
130 | ); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Set the hostname of the Transmission server |
||
135 | * |
||
136 | * @param string $host |
||
137 | */ |
||
138 | public function setHost($host) |
||
142 | |||
143 | /** |
||
144 | * Get the hostname of the Transmission server |
||
145 | * |
||
146 | * @return string |
||
147 | */ |
||
148 | public function getHost() |
||
152 | |||
153 | /** |
||
154 | * Set the port the Transmission server is listening on |
||
155 | * |
||
156 | * @param integer $port |
||
157 | */ |
||
158 | public function setPort($port) |
||
162 | |||
163 | /** |
||
164 | * Get the port the Transmission server is listening on |
||
165 | * |
||
166 | * @return integer |
||
167 | */ |
||
168 | public function getPort() |
||
172 | |||
173 | /** |
||
174 | * Set the path to Transmission server rpc api |
||
175 | * |
||
176 | * @param string $path |
||
177 | */ |
||
178 | public function setPath($path) |
||
182 | |||
183 | /** |
||
184 | * Get the path to Transmission server rpc api |
||
185 | */ |
||
186 | public function getPath() |
||
190 | |||
191 | /** |
||
192 | * Set the CSRF-token of the Transmission client |
||
193 | * |
||
194 | * @param string $token |
||
195 | */ |
||
196 | public function setToken($token) |
||
200 | |||
201 | /** |
||
202 | * Get the CSRF-token for the Transmission client |
||
203 | * |
||
204 | * @return string |
||
205 | */ |
||
206 | public function getToken() |
||
210 | |||
211 | /** |
||
212 | * Set the Buzz client used to connect to Transmission |
||
213 | * |
||
214 | * @param ClientInterface $client |
||
215 | */ |
||
216 | public function setClient(ClientInterface $client) |
||
220 | |||
221 | /** |
||
222 | * Get the Buzz client used to connect to Transmission |
||
223 | * |
||
224 | * @return ClientInterface |
||
225 | */ |
||
226 | public function getClient() |
||
230 | |||
231 | /** |
||
232 | * @param string $method |
||
233 | * @param array $arguments |
||
234 | * @return array|Request[]|Response[] |
||
235 | */ |
||
236 | protected function compose($method, $arguments) |
||
251 | |||
252 | /** |
||
253 | * @param Response $response |
||
254 | * @param string $method |
||
255 | * @param array $arguments |
||
256 | * @return \stdClass |
||
257 | * @throws \RuntimeException |
||
258 | */ |
||
259 | protected function validateResponse($response, $method, $arguments) |
||
277 | } |
||
278 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: