Skip to content

Commit 17b535e

Browse files
authored
Merge pull request #331 from opentok/dev
Dev release merge
2 parents db655f8 + 66425a8 commit 17b535e

File tree

4 files changed

+153
-264
lines changed

4 files changed

+153
-264
lines changed

src/OpenTok/OpenTok.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class OpenTok
2626
private $apiSecret;
2727
/** @internal */
2828
private $client;
29+
/** @internal */
30+
public $options;
2931

3032
/** @internal */
3133
public function __construct($apiKey, $apiSecret, $options = array())
@@ -36,8 +38,10 @@ public function __construct($apiKey, $apiSecret, $options = array())
3638
'client' => null,
3739
'timeout' => null // In the future we should set this to 2
3840
);
39-
$options = array_merge($defaults, array_intersect_key($options, $defaults));
40-
list($apiUrl, $client, $timeout) = array_values($options);
41+
42+
$this->options = array_merge($defaults, array_intersect_key($options, $defaults));
43+
44+
list($apiUrl, $client, $timeout) = array_values($this->options);
4145

4246
// validate arguments
4347
Validators::validateApiKey($apiKey);
@@ -52,7 +56,7 @@ public function __construct($apiKey, $apiSecret, $options = array())
5256
$apiKey,
5357
$apiSecret,
5458
$apiUrl,
55-
['timeout' => $timeout]
59+
array_merge(['timeout' => $timeout], $this->options)
5660
);
5761
}
5862
$this->apiKey = $apiKey;

src/OpenTok/Util/Client.php

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace OpenTok\Util;
44

5+
use Composer\InstalledVersions;
56
use Exception as GlobalException;
7+
use GuzzleHttp\Utils;
68
use OpenTok\Layout;
79
use Firebase\JWT\JWT;
810
use OpenTok\MediaMode;
@@ -18,7 +20,6 @@
1820
use GuzzleHttp\Exception\ServerException;
1921
use OpenTok\Exception\BroadcastException;
2022
use GuzzleHttp\Exception\RequestException;
21-
use function GuzzleHttp\default_user_agent;
2223
use OpenTok\Exception\ArchiveDomainException;
2324
use OpenTok\Exception\AuthenticationException;
2425
use OpenTok\Exception\BroadcastDomainException;
@@ -36,17 +37,13 @@
3637
use OpenTok\Exception\ForceDisconnectAuthenticationException;
3738
use OpenTok\Exception\ForceDisconnectUnexpectedValueException;
3839

39-
// TODO: build this dynamically
40-
/** @internal */
41-
define('OPENTOK_SDK_VERSION', '4.12.0');
42-
/** @internal */
43-
define('OPENTOK_SDK_USER_AGENT', 'OpenTok-PHP-SDK/' . OPENTOK_SDK_VERSION);
44-
4540
/**
46-
* @internal
47-
*/
41+
* @internal
42+
*/
4843
class Client
4944
{
45+
public const OPENTOK_SDK_USER_AGENT_IDENTIFIER = 'OpenTok-PHP-SDK/';
46+
5047
protected $apiKey;
5148
protected $apiSecret;
5249
protected $configured = false;
@@ -56,18 +53,24 @@ class Client
5653
*/
5754
protected $client;
5855

56+
/**
57+
* @var array|mixed
58+
*/
59+
public $options;
60+
5961
public function configure($apiKey, $apiSecret, $apiUrl, $options = array())
6062
{
63+
$this->options = $options;
6164
$this->apiKey = $apiKey;
6265
$this->apiSecret = $apiSecret;
6366

64-
if (isset($options['client'])) {
67+
if (isset($this->options['client'])) {
6568
$this->client = $options['client'];
6669
} else {
6770
$clientOptions = [
6871
'base_uri' => $apiUrl,
6972
'headers' => [
70-
'User-Agent' => OPENTOK_SDK_USER_AGENT . ' ' . default_user_agent(),
73+
'User-Agent' => $this->buildUserAgentString()
7174
],
7275
];
7376

@@ -94,6 +97,26 @@ public function configure($apiKey, $apiSecret, $apiUrl, $options = array())
9497
$this->configured = true;
9598
}
9699

100+
private function buildUserAgentString(): string
101+
{
102+
$userAgent = [];
103+
104+
$userAgent[] = self::OPENTOK_SDK_USER_AGENT_IDENTIFIER
105+
. InstalledVersions::getVersion('opentok/opentok');
106+
107+
$userAgent[] = 'php/' . PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION;
108+
109+
if (isset($this->options['app'])) {
110+
$app = $this->options['app'];
111+
if (isset($app['name'], $app['version'])) {
112+
// You must use both of these for custom agent strings
113+
$userAgent[] = $app['name'] . '/' . $app['version'];
114+
}
115+
}
116+
117+
return implode(' ', $userAgent);
118+
}
119+
97120
public function isConfigured()
98121
{
99122
return $this->configured;
@@ -939,7 +962,7 @@ private function handleSignalingException(ClientException $e)
939962
throw new SignalConnectionException($message, $responseCode);
940963
case 413:
941964
$message = 'The type string exceeds the maximum length (128 bytes),'
942-
. ' or the data string exceeds the maximum size (8 kB).';
965+
. ' or the data string exceeds the maximum size (8 kB).';
943966
throw new SignalUnexpectedValueException($message, $responseCode);
944967
default:
945968
break;

tests/OpenTokTest/ArchiveTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,6 @@ public function testStopsArchive()
160160
$authString = $request->getHeaderLine('X-OPENTOK-AUTH');
161161
$this->assertEquals(true, TestHelpers::validateOpenTokAuthHeader($this->API_KEY, $this->API_SECRET, $authString));
162162

163-
// TODO: test the dynamically built User Agent string
164-
$userAgent = $request->getHeaderLine('User-Agent');
165-
$this->assertNotEmpty($userAgent);
166-
$this->assertStringStartsWith('OpenTok-PHP-SDK/4.12.0', $userAgent);
167-
168163
// TODO: test the properties of the actual archive object
169164
$this->assertEquals('stopped', $this->archive->status);
170165

@@ -277,11 +272,6 @@ public function testDeletesArchive()
277272
$authString = $request->getHeaderLine('X-OPENTOK-AUTH');
278273
$this->assertEquals(true, TestHelpers::validateOpenTokAuthHeader($this->API_KEY, $this->API_SECRET, $authString));
279274

280-
// TODO: test the dynamically built User Agent string
281-
$userAgent = $request->getHeaderLine('User-Agent');
282-
$this->assertNotEmpty($userAgent);
283-
$this->assertStringStartsWith('OpenTok-PHP-SDK/4.12.0', $userAgent);
284-
285275
$this->assertTrue($success);
286276
// TODO: assert that all properties of the archive object were cleared
287277
}

0 commit comments

Comments
 (0)