Skip to content

Commit bb326a3

Browse files
committed
feat: Add list connections route
1 parent 5c06c8b commit bb326a3

File tree

6 files changed

+330
-0
lines changed

6 files changed

+330
-0
lines changed

src/OpenTok/Connection.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace OpenTok;
4+
5+
/**
6+
* Represents a connection in an OpenTok session.
7+
* <p>
8+
* See <a href="OpenTok.OpenTok.html#method_listConnections">OpenTok.listConnections()</a>.
9+
*
10+
* @property String $connectionId
11+
* The connection ID.
12+
*
13+
* @property String $connectionState
14+
* The state of the connection (either "Connecting" or "Connected").
15+
*
16+
* @property String $createdAt
17+
* The timestamp for when the connection was created, expressed in milliseconds since the Unix epoch.
18+
*/
19+
20+
class Connection
21+
{
22+
23+
private $data;
24+
25+
public function __construct($connectionData)
26+
{
27+
28+
$this->data = $connectionData;
29+
}
30+
31+
/** @ignore */
32+
public function __get($name)
33+
{
34+
switch ($name) {
35+
case 'connectionId':
36+
case 'connectionState':
37+
case 'createdAt':
38+
return $this->data[$name];
39+
default:
40+
return null;
41+
}
42+
}
43+
44+
public function jsonSerialize()
45+
{
46+
return $this->data;
47+
}
48+
}

src/OpenTok/ConnectionList.php

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
3+
namespace OpenTok;
4+
5+
/**
6+
* An object, returned by the <a href="OpenTok.OpenTok.html#method_listConnections">OpenTok.listConnections()</a>
7+
* method, representing a list of connections in an OpenTok session.
8+
*/
9+
class ConnectionList implements \Iterator
10+
{
11+
/** @ignore */
12+
private $data;
13+
14+
/** @ignore */
15+
private $items;
16+
17+
/** @ignore */
18+
private $position = 0;
19+
20+
/** @ignore */
21+
public function __construct($connectionListData)
22+
{
23+
$this->data = $connectionListData;
24+
$this->position = 0;
25+
}
26+
27+
/**
28+
* Returns the number of total connections for the session ID.
29+
*
30+
* @return int
31+
*/
32+
public function totalCount()
33+
{
34+
return $this->data['count'];
35+
}
36+
37+
/**
38+
* Returns the project ID (Application ID).
39+
*
40+
* @return string
41+
*/
42+
public function getProjectId()
43+
{
44+
return $this->data['projectId'];
45+
}
46+
47+
/**
48+
* Returns the session ID.
49+
*
50+
* @return string
51+
*/
52+
public function getSessionId()
53+
{
54+
return $this->data['sessionId'];
55+
}
56+
57+
/**
58+
* Returns an array of Connection objects.
59+
*
60+
* @return Connection[]
61+
*/
62+
public function getItems()
63+
{
64+
if (!is_array($this->items)) {
65+
$items = array();
66+
foreach ($this->data['items'] as $connectionData) {
67+
$items[] = new Connection($connectionData);
68+
}
69+
$this->items = $items;
70+
}
71+
return $this->items;
72+
}
73+
74+
/**
75+
* @return array
76+
*/
77+
public function jsonSerialize()
78+
{
79+
return $this->data;
80+
}
81+
82+
// Iterator interface methods
83+
84+
/**
85+
* Rewind the Iterator to the first element
86+
*/
87+
public function rewind(): void
88+
{
89+
$this->position = 0;
90+
}
91+
92+
/**
93+
* Return the current element
94+
*
95+
* @return Connection
96+
*/
97+
public function current(): Connection
98+
{
99+
$items = $this->getItems();
100+
return $items[$this->position];
101+
}
102+
103+
/**
104+
* Return the key of the current element
105+
*
106+
* @return int
107+
*/
108+
public function key(): int
109+
{
110+
return $this->position;
111+
}
112+
113+
/**
114+
* Move forward to next element
115+
*/
116+
public function next(): void
117+
{
118+
++$this->position;
119+
}
120+
121+
/**
122+
* Checks if current position is valid
123+
*
124+
* @return bool
125+
*/
126+
public function valid(): bool
127+
{
128+
$items = $this->getItems();
129+
return isset($items[$this->position]);
130+
}
131+
}

src/OpenTok/OpenTok.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,25 @@ public function listStreams($sessionId)
11401140
return new StreamList($streamListData);
11411141
}
11421142

1143+
/**
1144+
* Returns a ConnectionList object for the given session ID.
1145+
*
1146+
* Use this method to list the connections from an OpenTok session associated with an application.
1147+
*
1148+
* @param String $sessionId The session ID.
1149+
*
1150+
* @return ConnectionList A ConnectionList object. Call the getItems() method of the ConnectionList object
1151+
* to return an array of Connection objects.
1152+
*/
1153+
public function listConnections($sessionId)
1154+
{
1155+
Validators::validateSessionIdBelongsToKey($sessionId, $this->apiKey);
1156+
1157+
// make API call
1158+
$connectionListData = $this->client->listConnections($sessionId);
1159+
return new ConnectionList($connectionListData);
1160+
}
1161+
11431162
/**
11441163
* Initiates an outgoing SIP call.
11451164
* <p>

src/OpenTok/Util/Client.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,24 @@ public function listStreams($sessionId)
670670
return $streamListJson;
671671
}
672672

673+
public function listConnections($sessionId)
674+
{
675+
$request = new Request(
676+
'GET',
677+
'/v2/project/' . $this->apiKey . '/session/' . $sessionId . '/connection/'
678+
);
679+
try {
680+
$response = $this->client->send($request, [
681+
'debug' => $this->isDebug(),
682+
]);
683+
$connectionListJson = json_decode($response->getBody(), true);
684+
} catch (\Exception $e) {
685+
$this->handleException($e);
686+
return;
687+
}
688+
return $connectionListJson;
689+
}
690+
673691
public function setStreamClassLists($sessionId, $payload)
674692
{
675693
$itemsPayload = array(

tests/OpenTokTest/OpenTokTest.php

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2926,6 +2926,98 @@ public function testListStreams(): void
29262926
$this->assertInstanceOf('OpenTok\StreamList', $streamList);
29272927
}
29282928

2929+
public function testListConnections(): void
2930+
{
2931+
// Arrange
2932+
$this->setupOTWithMocks([[
2933+
'code' => 200,
2934+
'headers' => [
2935+
'Content-Type' => 'application/json'
2936+
],
2937+
'path' => '/v2/project/APIKEY/session/SESSIONID/connection/get'
2938+
]]);
2939+
2940+
$sessionId = '1_MX4xMjM0NTY3OH4-VGh1IEZlYiAyNyAwNDozODozMSBQU1QgMjAxNH4wLjI0NDgyMjI';
2941+
2942+
// Act
2943+
$connectionList = $this->opentok->listConnections($sessionId);
2944+
2945+
// Assert
2946+
$this->assertCount(1, $this->historyContainer);
2947+
2948+
$request = $this->historyContainer[0]['request'];
2949+
$this->assertEquals('GET', strtoupper($request->getMethod()));
2950+
$this->assertEquals('/v2/project/' . $this->API_KEY . '/session/' . $sessionId . '/connection/', $request->getUri()->getPath());
2951+
$this->assertEquals('api.opentok.com', $request->getUri()->getHost());
2952+
$this->assertEquals('https', $request->getUri()->getScheme());
2953+
2954+
$authString = $request->getHeaderLine('X-OPENTOK-AUTH');
2955+
$this->assertEquals(true, TestHelpers::validateOpenTokAuthHeader($this->API_KEY, $this->API_SECRET, $authString));
2956+
2957+
$this->assertInstanceOf('OpenTok\ConnectionList', $connectionList);
2958+
$this->assertEquals(3, $connectionList->totalCount());
2959+
$this->assertEquals('e9f8c166-6c67-440d-994a-04fb6dfed007', $connectionList->getProjectId());
2960+
$this->assertEquals('b40ef09b-3811-4726-b508-e41a0f96c68f', $connectionList->getSessionId());
2961+
2962+
$connections = $connectionList->getItems();
2963+
$this->assertCount(3, $connections);
2964+
$this->assertInstanceOf('OpenTok\Connection', $connections[0]);
2965+
$this->assertEquals('8b732909-0a06-46a2-8ea8-074e64d43422', $connections[0]->connectionId);
2966+
$this->assertEquals('Connected', $connections[0]->connectionState);
2967+
$this->assertEquals('1384221730000', $connections[0]->createdAt);
2968+
}
2969+
2970+
public function testListConnectionsWithInvalidSessionId(): void
2971+
{
2972+
// Arrange
2973+
$this->setupOT();
2974+
2975+
// Assert
2976+
$this->expectException('OpenTok\Exception\InvalidArgumentException');
2977+
2978+
// Act
2979+
$this->opentok->listConnections('invalid-session-id');
2980+
}
2981+
2982+
public function testListConnectionsIteration(): void
2983+
{
2984+
// Arrange
2985+
$this->setupOTWithMocks([[
2986+
'code' => 200,
2987+
'headers' => [
2988+
'Content-Type' => 'application/json'
2989+
],
2990+
'path' => '/v2/project/APIKEY/session/SESSIONID/connection/get'
2991+
]]);
2992+
2993+
$sessionId = '1_MX4xMjM0NTY3OH4-VGh1IEZlYiAyNyAwNDozODozMSBQU1QgMjAxNH4wLjI0NDgyMjI';
2994+
2995+
// Act
2996+
$connectionList = $this->opentok->listConnections($sessionId);
2997+
2998+
// Assert - Test direct iteration over ConnectionList
2999+
$iteratedConnections = [];
3000+
foreach ($connectionList as $index => $connection) {
3001+
$iteratedConnections[$index] = $connection;
3002+
$this->assertInstanceOf('OpenTok\Connection', $connection);
3003+
}
3004+
3005+
$this->assertCount(3, $iteratedConnections);
3006+
$this->assertEquals('8b732909-0a06-46a2-8ea8-074e64d43422', $iteratedConnections[0]->connectionId);
3007+
$this->assertEquals('Connected', $iteratedConnections[0]->connectionState);
3008+
$this->assertEquals('ab732909-0a06-46a2-8ea8-074e64d43412', $iteratedConnections[1]->connectionId);
3009+
$this->assertEquals('Connecting', $iteratedConnections[1]->connectionState);
3010+
$this->assertEquals('cd732909-0a06-46a2-8ea8-074e64d43433', $iteratedConnections[2]->connectionId);
3011+
$this->assertEquals('Connected', $iteratedConnections[2]->connectionState);
3012+
3013+
// Assert - Test that getItems() still works as before
3014+
$itemsConnections = $connectionList->getItems();
3015+
$this->assertCount(3, $itemsConnections);
3016+
$this->assertEquals($iteratedConnections[0]->connectionId, $itemsConnections[0]->connectionId);
3017+
$this->assertEquals($iteratedConnections[1]->connectionId, $itemsConnections[1]->connectionId);
3018+
$this->assertEquals($iteratedConnections[2]->connectionId, $itemsConnections[2]->connectionId);
3019+
}
3020+
29293021
public function testsSetArchiveLayoutWithPredefined(): void
29303022
{
29313023
// Arrange
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"count": 3,
3+
"projectId": "e9f8c166-6c67-440d-994a-04fb6dfed007",
4+
"sessionId": "b40ef09b-3811-4726-b508-e41a0f96c68f",
5+
"items": [
6+
{
7+
"connectionId": "8b732909-0a06-46a2-8ea8-074e64d43422",
8+
"connectionState": "Connected",
9+
"createdAt": "1384221730000"
10+
},
11+
{
12+
"connectionId": "ab732909-0a06-46a2-8ea8-074e64d43412",
13+
"connectionState": "Connecting",
14+
"createdAt": "1384221735000"
15+
},
16+
{
17+
"connectionId": "cd732909-0a06-46a2-8ea8-074e64d43433",
18+
"connectionState": "Connected",
19+
"createdAt": "1384221740000"
20+
}
21+
]
22+
}

0 commit comments

Comments
 (0)