Skip to content

Commit 573dac0

Browse files
committed
Cleaned up code around layouts in prep for new screenshare layout
1 parent af465cf commit 573dac0

File tree

3 files changed

+45
-54
lines changed

3 files changed

+45
-54
lines changed

src/OpenTok/OpenTok.php

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace OpenTok;
44

5+
use OpenTok\Layout;
56
use OpenTok\Util\Client;
67
use OpenTok\Util\Validators;
7-
use OpenTok\Exception\UnexpectedValueException;
88
use OpenTok\Exception\InvalidArgumentException;
9+
use OpenTok\Exception\UnexpectedValueException;
910

1011
/**
1112
* Contains methods for creating OpenTok sessions, generating tokens, and working with archives.
@@ -296,13 +297,17 @@ public function createSession($options = array())
296297
* @return Archive The Archive object, which includes properties defining the archive, including
297298
* the archive ID.
298299
*/
299-
public function startArchive($sessionId, $options = array())
300+
public function startArchive(string $sessionId, $options = []): Archive
300301
{
301302
// support for deprecated method signature, remove in v3.0.0 (not before)
302303
if (!is_array($options)) {
304+
trigger_error(
305+
'Archive options passed as a string is deprecated, please pass an array with a name key',
306+
E_USER_DEPRECATED
307+
);
303308
$options = array('name' => $options);
304309
}
305-
310+
306311
// unpack optional arguments (merging with default values) into named variables
307312
$defaults = array(
308313
'name' => null,
@@ -425,17 +430,11 @@ public function listArchives($offset = 0, $count = null, $sessionId = null)
425430

426431
/**
427432
* Updates the stream layout in an OpenTok Archive.
428-
*
429-
* @param string $archiveId The OpenTok archive ID.
430-
*
431-
* @param string $layout The connectionId of the connection in a session.
432433
*/
433-
434-
public function setArchiveLayout($archiveId, $layoutType)
434+
public function setArchiveLayout(string $archiveId, Layout $layoutType): void
435435
{
436436
Validators::validateArchiveId($archiveId);
437-
Validators::validateLayout($layoutType);
438-
437+
439438
$this->client->setArchiveLayout($archiveId, $layoutType);
440439
}
441440

@@ -497,7 +496,7 @@ public function forceDisconnect($sessionId, $connectionId)
497496
*
498497
* @return Broadcast An object with properties defining the broadcast.
499498
*/
500-
public function startBroadcast($sessionId, $options = array())
499+
public function startBroadcast(string $sessionId, array $options = []): Broadcast
501500
{
502501
// unpack optional arguments (merging with default values) into named variables
503502
// NOTE: although the server can be authoritative about the default value of layout, its
@@ -571,14 +570,9 @@ public function getBroadcast($broadcastId)
571570
*
572571
* @param Layout $layout An object defining the layout type for the broadcast.
573572
*/
574-
public function updateBroadcastLayout($broadcastId, $layout)
573+
public function updateBroadcastLayout(string $broadcastId, Layout $layout): void
575574
{
576575
Validators::validateBroadcastId($broadcastId);
577-
Validators::validateLayout($layout);
578-
579-
// TODO: platform implementation does not meet API Review spec
580-
// $layoutData = $this->client->updateLayout($broadcastId, $layout, 'broadcast');
581-
// return Layout::fromData($layoutData);
582576

583577
$this->client->updateLayout($broadcastId, $layout, 'broadcast');
584578
}

src/OpenTok/Util/Client.php

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,37 @@
22

33
namespace OpenTok\Util;
44

5+
use OpenTok\Layout;
6+
use Firebase\JWT\JWT;
7+
use OpenTok\MediaMode;
58
use GuzzleHttp\Middleware;
69
use GuzzleHttp\HandlerStack;
7-
use GuzzleHttp\Exception\ClientException;
8-
use GuzzleHttp\Exception\ServerException;
910
use GuzzleHttp\Psr7\Request;
10-
use Psr\Http\Message\RequestInterface;
11-
use Firebase\JWT\JWT;
12-
use GuzzleHttp\Exception\RequestException;
1311
use OpenTok\Exception\Exception;
1412
use OpenTok\Exception\DomainException;
15-
use OpenTok\Exception\UnexpectedValueException;
16-
use OpenTok\Exception\AuthenticationException;
13+
use Psr\Http\Message\RequestInterface;
1714
use OpenTok\Exception\ArchiveException;
18-
use OpenTok\Exception\ArchiveDomainException;
19-
use OpenTok\Exception\ArchiveUnexpectedValueException;
20-
use OpenTok\Exception\ArchiveAuthenticationException;
15+
use GuzzleHttp\Exception\ClientException;
16+
use GuzzleHttp\Exception\ServerException;
2117
use OpenTok\Exception\BroadcastException;
18+
use GuzzleHttp\Exception\RequestException;
19+
use function GuzzleHttp\default_user_agent;
20+
use OpenTok\Exception\ArchiveDomainException;
21+
use OpenTok\Exception\AuthenticationException;
2222
use OpenTok\Exception\BroadcastDomainException;
23-
use OpenTok\Exception\BroadcastUnexpectedValueException;
24-
use OpenTok\Exception\BroadcastAuthenticationException;
23+
use OpenTok\Exception\UnexpectedValueException;
2524
use OpenTok\Exception\SignalConnectionException;
26-
use OpenTok\Exception\SignalUnexpectedValueException;
2725
use OpenTok\Exception\SignalAuthenticationException;
28-
use OpenTok\Exception\ForceDisconnectConnectionException;
29-
use OpenTok\Exception\ForceDisconnectUnexpectedValueException;
30-
use OpenTok\Exception\ForceDisconnectAuthenticationException;
26+
use OpenTok\Exception\ArchiveAuthenticationException;
27+
use OpenTok\Exception\SignalUnexpectedValueException;
28+
use OpenTok\Exception\ArchiveUnexpectedValueException;
29+
use OpenTok\Exception\BroadcastAuthenticationException;
3130
use OpenTok\Exception\SignalNetworkConnectionException;
32-
use OpenTok\MediaMode;
31+
use OpenTok\Exception\BroadcastUnexpectedValueException;
32+
use OpenTok\Exception\ForceDisconnectConnectionException;
3333

34-
use function GuzzleHttp\default_user_agent;
34+
use OpenTok\Exception\ForceDisconnectAuthenticationException;
35+
use OpenTok\Exception\ForceDisconnectUnexpectedValueException;
3536

3637
// TODO: build this dynamically
3738
/** @internal */
@@ -154,18 +155,15 @@ private function getResponseXml($response)
154155
return $xml;
155156
}
156157

157-
// Archiving API Requests
158-
159-
public function startArchive($sessionId, $options)
158+
public function startArchive(string $sessionId, array $options = []): array
160159
{
161-
// set up the request
162160
$request = new Request('POST', '/v2/project/' . $this->apiKey . '/archive');
163161

164162
try {
165163
$response = $this->client->send($request, [
166164
'debug' => $this->isDebug(),
167165
'json' => array_merge(
168-
array( 'sessionId' => $sessionId ),
166+
['sessionId' => $sessionId],
169167
$options
170168
)
171169
]);
@@ -283,7 +281,7 @@ public function listArchives($offset, $count, $sessionId)
283281
return $archiveListJson;
284282
}
285283

286-
public function startBroadcast($sessionId, $options)
284+
public function startBroadcast(string $sessionId, array $options): array
287285
{
288286
$request = new Request(
289287
'POST',
@@ -362,40 +360,36 @@ public function getLayout($resourceId, $resourceType = 'broadcast')
362360
return $layoutJson;
363361
}
364362

365-
public function updateLayout($resourceId, $layout, $resourceType = 'broadcast')
363+
public function updateLayout(string $resourceId, Layout $layout, string $resourceType = 'broadcast'): void
366364
{
367365
$request = new Request(
368366
'PUT',
369367
'/v2/project/' . $this->apiKey . '/' . $resourceType . '/' . $resourceId . '/layout'
370368
);
371369
try {
372-
$response = $this->client->send($request, [
370+
$this->client->send($request, [
373371
'debug' => $this->isDebug(),
374-
'json' => $layout->jsonSerialize()
372+
'json' => $layout->toArray()
375373
]);
376-
$layoutJson = json_decode($response->getBody(), true);
377374
} catch (\Exception $e) {
378375
$this->handleException($e);
379376
}
380-
return $layoutJson;
381377
}
382378

383-
public function setArchiveLayout($archiveId, $layout)
379+
public function setArchiveLayout(string $archiveId, Layout $layout): void
384380
{
385381
$request = new Request(
386382
'PUT',
387383
'/v2/project/' . $this->apiKey . '/archive/' . $archiveId . '/layout'
388384
);
389385
try {
390-
$response = $this->client->send($request, [
386+
$this->client->send($request, [
391387
'debug' => $this->isDebug(),
392-
'json' => $layout->jsonSerialize()
388+
'json' => $layout->toArray()
393389
]);
394-
$layoutJson = json_decode($response->getBody(), true);
395390
} catch (\Exception $e) {
396391
$this->handleException($e);
397392
}
398-
return $layoutJson;
399393
}
400394

401395
public function updateStream($sessionId, $streamId, $properties)

tests/OpenTokTest/OpenTokTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,10 @@ public function testStartsArchiveNamed()
578578
// TODO: test the properties of the actual archive object
579579
}
580580

581-
// this is the deprecated method signature, remove in v3.0.0 (and not before)
581+
/**
582+
* this is the deprecated method signature, remove in v3.0.0 (and not before)
583+
* @todo Remove this when `startArchive` removes string support
584+
*/
582585
public function testStartsArchiveNamedDeprecated()
583586
{
584587
// Arrange
@@ -595,7 +598,7 @@ public function testStartsArchiveNamedDeprecated()
595598
$sessionId = '2_MX44NTQ1MTF-flR1ZSBOb3YgMTIgMDk6NDA6NTkgUFNUIDIwMTN-MC43NjU0Nzh-';
596599

597600
// Act
598-
$archive = $this->opentok->startArchive($sessionId, 'showtime');
601+
@$archive = $this->opentok->startArchive($sessionId, 'showtime');
599602

600603
// Assert
601604
$this->assertCount(1, $this->historyContainer);

0 commit comments

Comments
 (0)