Skip to content

Commit 6d85bd2

Browse files
Wraps User and Conversation clients in a factory (#278)
1 parent 7cfa394 commit 6d85bd2

File tree

4 files changed

+113
-5
lines changed

4 files changed

+113
-5
lines changed

src/Client.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
use Vonage\Client\Factory\FactoryInterface;
4040
use Vonage\Client\Factory\MapFactory;
4141
use Vonage\Client\Signature;
42-
use Vonage\Conversations\Collection as ConversationsCollection;
42+
use Vonage\Conversations\ClientFactory as ConversationsClientFactory;
4343
use Vonage\Conversion\ClientFactory as ConversionClientFactory;
4444
use Vonage\Entity\EntityInterface;
4545
use Vonage\Insights\ClientFactory as InsightsClientFactory;
@@ -49,7 +49,6 @@
4949
use Vonage\Redact\ClientFactory as RedactClientFactory;
5050
use Vonage\Secrets\ClientFactory as SecretsClientFactory;
5151
use Vonage\SMS\ClientFactory as SMSClientFactory;
52-
use Vonage\User\Collection as UserCollection;
5352
use Vonage\Verify\ClientFactory as VerifyClientFactory;
5453
use Vonage\Verify\Verification;
5554
use Vonage\Voice\ClientFactory as VoiceClientFactory;
@@ -58,7 +57,6 @@
5857
use function array_merge;
5958
use function base64_encode;
6059
use function call_user_func_array;
61-
use function class_exists;
6260
use function get_class;
6361
use function http_build_query;
6462
use function implode;
@@ -73,6 +71,7 @@
7371
use function strpos;
7472
use function unserialize;
7573
use Vonage\Logger\LoggerTrait;
74+
use Vonage\User\ClientFactory as UserClientFactory;
7675

7776
/**
7877
* Vonage API Client, allows access to the API from PHP.
@@ -88,6 +87,8 @@
8887
* @method SMS\Client sms()
8988
* @method Verify\Client verify()
9089
* @method Voice\Client voice()
90+
* @method User\Collection user()
91+
* @method Conversation\Collection conversation()
9192
*
9293
* @property string restUrl
9394
* @property string apiUrl
@@ -204,8 +205,8 @@ public function __construct(CredentialsInterface $credentials, $options = [], ?C
204205
// Legacy Namespaces
205206
'message' => MessageClient::class,
206207
'calls' => Collection::class,
207-
'conversation' => ConversationsCollection::class,
208-
'user' => UserCollection::class,
208+
'conversation' => ConversationsClientFactory::class,
209+
'user' => UserClientFactory::class,
209210

210211
// Registered Services by name
211212
'account' => ClientFactory::class,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Vonage\Conversations;
4+
5+
use Vonage\Client\Factory\MapFactory;
6+
7+
class ClientFactory
8+
{
9+
public function __invoke(MapFactory $factory)
10+
{
11+
$collection = new Collection();
12+
$collection->setClient($factory->getClient());
13+
14+
return $collection;
15+
}
16+
}

src/User/ClientFactory.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Vonage\User;
4+
5+
use Vonage\Client\Factory\MapFactory;
6+
7+
class ClientFactory
8+
{
9+
public function __invoke(MapFactory $factory)
10+
{
11+
$collection = new Collection();
12+
$collection->setClient($factory->getClient());
13+
14+
return $collection;
15+
}
16+
}

src/User/Collection.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,79 @@ public function offsetUnset($offset): void
236236
{
237237
throw new RuntimeException('can not unset collection properties');
238238
}
239+
240+
/**
241+
* Handle pagination automatically (unless configured not to).
242+
*/
243+
public function valid(): bool
244+
{
245+
//can't be valid if there's not a page (rewind sets this)
246+
if (!isset($this->page)) {
247+
return false;
248+
}
249+
250+
if (isset($this->page['_embedded'])) {
251+
//all hal collections have an `_embedded` object, we expect there to be a property matching the collection name
252+
if (!isset($this->page['_embedded'][static::getCollectionName()])) {
253+
return false;
254+
}
255+
256+
//if we have a page with no items, we've gone beyond the end of the collection
257+
if (!count($this->page['_embedded'][static::getCollectionName()])) {
258+
return false;
259+
}
260+
261+
//index the start of a page at 0
262+
if (is_null($this->current)) {
263+
$this->current = 0;
264+
}
265+
266+
//if our current index is past the current page, fetch the next page if possible and reset the index
267+
if (!isset($this->page['_embedded'][static::getCollectionName()][$this->current])) {
268+
if (isset($this->page['_links']['next'])) {
269+
$this->fetchPage($this->page['_links']['next']['href']);
270+
$this->current = 0;
271+
272+
return true;
273+
}
274+
275+
return false;
276+
}
277+
} else {
278+
if (!isset($this->page)) {
279+
return false;
280+
}
281+
282+
//index the start of a page at 0
283+
if (is_null($this->current)) {
284+
$this->current = 0;
285+
}
286+
287+
//if our current index is past the current page, fetch the next page if possible and reset the index
288+
if (!isset($this->page[$this->current])) {
289+
if (isset($this->page['_links']['next'])) {
290+
$this->fetchPage($this->page['_links']['next']['href']);
291+
$this->current = 0;
292+
293+
return true;
294+
}
295+
296+
return false;
297+
}
298+
}
299+
300+
return true;
301+
}
302+
303+
/**
304+
* Return the current item, expects concrete collection to handle creating the object.
305+
*/
306+
public function current(): User
307+
{
308+
if (isset($this->page['_embedded'])) {
309+
return $this->hydrateEntity($this->page['_embedded'][static::getCollectionName()][$this->current], $this->key());
310+
} else {
311+
return $this->hydrateEntity($this->page[$this->current], $this->key());
312+
}
313+
}
239314
}

0 commit comments

Comments
 (0)