22
33namespace OpenTok \Util ;
44
5+ use Composer \InstalledVersions ;
56use Exception as GlobalException ;
7+ use GuzzleHttp \Utils ;
68use OpenTok \Layout ;
79use Firebase \JWT \JWT ;
810use OpenTok \MediaMode ;
1820use GuzzleHttp \Exception \ServerException ;
1921use OpenTok \Exception \BroadcastException ;
2022use GuzzleHttp \Exception \RequestException ;
21- use function GuzzleHttp \default_user_agent ;
2223use OpenTok \Exception \ArchiveDomainException ;
2324use OpenTok \Exception \AuthenticationException ;
2425use OpenTok \Exception \BroadcastDomainException ;
3637use OpenTok \Exception \ForceDisconnectAuthenticationException ;
3738use 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+ */
4843class 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 ;
0 commit comments