Skip to content

Commit

Permalink
Autoload video module if it is installed (#458)
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonmantank authored Dec 12, 2023
1 parent 5021494 commit 2c8d6b4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 21 deletions.
53 changes: 32 additions & 21 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
* @method Verify\Client verify()
* @method Verify2\Client verify2()
* @method Voice\Client voice()
* @method Vonage\Video\Client video()
*
* @property string restUrl
* @property string apiUrl
Expand Down Expand Up @@ -211,29 +212,39 @@ public function __construct(
$this->debug = $options['debug'];
}

$services = [
// Registered Services by name
'account' => ClientFactory::class,
'applications' => ApplicationClientFactory::class,
'conversion' => ConversionClientFactory::class,
'insights' => InsightsClientFactory::class,
'numbers' => NumbersClientFactory::class,
'meetings' => MeetingsClientFactory::class,
'messages' => MessagesClientFactory::class,
'redact' => RedactClientFactory::class,
'secrets' => SecretsClientFactory::class,
'sms' => SMSClientFactory::class,
'subaccount' => SubaccountClientFactory::class,
'users' => UsersClientFactory::class,
'verify' => VerifyClientFactory::class,
'verify2' => Verify2ClientFactory::class,
'voice' => VoiceClientFactory::class,

// Additional utility classes
APIResource::class => APIResource::class,
];

if (class_exists('Vonage\Video\ClientFactory')) {
$services['video'] = 'Vonage\Video\ClientFactory';
} else {
$services['video'] = function() {
throw new \RuntimeException('Please install @vonage/video to use the Video API');
};
}

$this->setFactory(
new MapFactory(
[
// Registered Services by name
'account' => ClientFactory::class,
'applications' => ApplicationClientFactory::class,
'conversion' => ConversionClientFactory::class,
'insights' => InsightsClientFactory::class,
'numbers' => NumbersClientFactory::class,
'meetings' => MeetingsClientFactory::class,
'messages' => MessagesClientFactory::class,
'redact' => RedactClientFactory::class,
'secrets' => SecretsClientFactory::class,
'sms' => SMSClientFactory::class,
'subaccount' => SubaccountClientFactory::class,
'users' => UsersClientFactory::class,
'verify' => VerifyClientFactory::class,
'verify2' => Verify2ClientFactory::class,
'voice' => VoiceClientFactory::class,

// Additional utility classes
APIResource::class => APIResource::class,
],
$services,
$this
)
);
Expand Down
32 changes: 32 additions & 0 deletions test/ClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* Vonage Client Library for PHP
*
* @copyright Copyright (c) 2016-2023 Vonage, Inc. (http://vonage.com)
* @license https://github.com/Vonage/vonage-php-sdk-core/blob/master/LICENSE.txt Apache License 2.0
*/

declare(strict_types=1);

namespace VonageTest;

use RuntimeException;
use VonageTest\VonageTestCase;
use Vonage\Client;
use Vonage\Client\Credentials\Basic;

class ClientTest extends VonageTestCase
{
/**
* Make sure that when calling the video module it errors if the class isn't found
*/
public function testCallingVideoWithoutPackageGeneratesRuntimeError(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Please install @vonage/video to use the Video API');

$client = new Client(new Basic('abcd', '1234'));
$video = $client->video();
}
}

0 comments on commit 2c8d6b4

Please sign in to comment.