Skip to content

Commit 18e6284

Browse files
committed
Move user agent to connector
1 parent 1a28a56 commit 18e6284

6 files changed

+52
-32
lines changed

Diff for: src/Client.php

-8
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,6 @@ public function __construct(TokenInterface $token, $api_version = null)
4848
}
4949
}
5050

51-
/**
52-
* {@inheritdoc}
53-
*/
54-
public function getUserAgent()
55-
{
56-
return 'Active Collab API Wrapper; v' . self::VERSION;
57-
}
58-
5951
// ---------------------------------------------------
6052
// Info
6153
// ---------------------------------------------------

Diff for: src/ClientInterface.php

-7
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@ interface ClientInterface
1515
{
1616
const VERSION = '3.0.0';
1717

18-
/**
19-
* Return user agent string.
20-
*
21-
* @return string
22-
*/
23-
public function getUserAgent();
24-
2518
/**
2619
* Return info.
2720
*

Diff for: src/Connector.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
*/
1717
class Connector implements ConnectorInterface
1818
{
19+
/**
20+
* {@inheritdoc}
21+
*/
22+
public function getUserAgent()
23+
{
24+
return 'Active Collab API Wrapper; v' . Client::VERSION;
25+
}
26+
1927
/**
2028
* {@inheritdoc}
2129
*/
@@ -137,7 +145,7 @@ private function &getHandle($url, $headers)
137145
{
138146
$http = curl_init();
139147

140-
curl_setopt($http, CURLOPT_USERAGENT, Client::getUserAgent());
148+
curl_setopt($http, CURLOPT_USERAGENT, $this->getUserAgent());
141149
curl_setopt($http, CURLOPT_RETURNTRANSFER, true);
142150
curl_setopt($http, CURLINFO_HEADER_OUT, true);
143151
curl_setopt($http, CURLOPT_URL, $url);

Diff for: src/ConnectorInterface.php

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
*/
1414
interface ConnectorInterface
1515
{
16+
/**
17+
* Return user agent string.
18+
*
19+
* @return string
20+
*/
21+
public function getUserAgent();
22+
1623
/**
1724
* GET data.
1825
*

Diff for: src/Response.php

+22-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace ActiveCollab\SDK;
1010

11+
use RuntimeException;
12+
1113
/**
1214
* Abstract result.
1315
*/
@@ -75,11 +77,25 @@ public function getBody()
7577
}
7678

7779
/**
78-
* Cached JSON data.
80+
* Flag whether we have a JSON response or not.
7981
*
8082
* @var mixed
8183
*/
82-
private $is_json = null, $json_loaded = false, $json = null;
84+
private $is_json = null;
85+
86+
/**
87+
* Flag whether JSON was parsed or not.
88+
*
89+
* @var bool
90+
*/
91+
private $json_loaded = false;
92+
93+
/**
94+
* Parsed JSON data.
95+
*
96+
* @var array|null
97+
*/
98+
private $json = null;
8399

84100
/**
85101
* Return true if response is JSON.
@@ -105,6 +121,10 @@ public function getJson()
105121
if (empty($this->json_loaded)) {
106122
if ($this->getBody() && $this->isJson()) {
107123
$this->json = json_decode($this->getBody(), true);
124+
125+
if (json_last_error()) {
126+
throw new RuntimeException('Failed to parse JSON. Reason: ' . json_last_error_msg());
127+
}
108128
}
109129

110130
$this->json_loaded = true;

Diff for: src/ResponseInterface.php

+14-14
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@
1313
*/
1414
interface ResponseInterface
1515
{
16+
/**
17+
* Return HTTP code.
18+
*
19+
* @return int
20+
*/
21+
public function getHttpCode();
22+
23+
/**
24+
* Return content type.
25+
*
26+
* @return string
27+
*/
28+
public function getContentType();
29+
1630
/**
1731
* Return raw response body.
1832
*
@@ -33,18 +47,4 @@ public function isJson();
3347
* @return mixed
3448
*/
3549
public function getJson();
36-
37-
/**
38-
* Return content type.
39-
*
40-
* @return string
41-
*/
42-
public function getContentType();
43-
44-
/**
45-
* Return HTTP code.
46-
*
47-
* @return int
48-
*/
49-
public function getHttpCode();
5050
}

0 commit comments

Comments
 (0)