Skip to content

Commit

Permalink
Merge pull request #206 from helpscout/list-users-by-mailbox
Browse files Browse the repository at this point in the history
Can now list users in a given mailbox
  • Loading branch information
bkuhl authored Nov 22, 2019
2 parents c26c598 + e74f003 commit 3a94a57
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 3 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,18 @@ Get users.
$users = $client->users()->list();
```

Narrow down the list of Users based on a set of filters.

```php
use HelpScout\Api\Users\UserFilters;

$filters = (new UserFilters())
->withMailbox(1)
->withEmail('[email protected]');

$users = $client->users()->list($filters);
```

## Reports

When running reports using the SDK, refer to the [developer docs](https://developer.helpscout.com/mailbox-api/) for the exact endpoint, parameters, and response formats. While most of the endpoints in this SDK are little more than pass-through methods to call the API, there are a few conveniences.
Expand Down
7 changes: 7 additions & 0 deletions examples/users.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
require '_credentials.php';

use HelpScout\Api\ApiClientFactory;
use HelpScout\Api\Users\UserFilters;

$client = ApiClientFactory::createClient();
$client->useClientCredentials($appId, $appSecret);

// List users
$users = $client->users()->list();

$filters = (new UserFilters())
->withMailbox(197271);

$users = $client->users()->list($filters);

print_r($users->getFirstPage()->toArray());
55 changes: 55 additions & 0 deletions src/Users/UserFilters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace HelpScout\Api\Users;

use HelpScout\Api\Assert\Assert;

class UserFilters
{
/**
* @var int
*/
private $mailbox;

/**
* @var string
*/
private $email;

public function getParams(): array
{
$params = [
'mailbox' => $this->mailbox,
'email' => $this->email,
];

// Filter out null values & empty strings
return array_filter($params);
}

/**
* @return self
*/
public function withMailbox(int $mailbox)
{
Assert::greaterThan($mailbox, 0);

$filters = clone $this;
$filters->mailbox = $mailbox;

return $filters;
}

/**
* @return self
*/
public function withEmail(string $email)
{
$filters = clone $this;
$filters->email = $email;

return $filters;
}
}
15 changes: 12 additions & 3 deletions src/Users/UsersEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,21 @@ public function getAuthenticatedUser(): User
/**
* @return User[]|PagedCollection
*/
public function list(): PagedCollection
{
public function list(
UserFilters $userFilters = null
): PagedCollection {
$uri = self::LIST_USERS_URI;
if ($userFilters) {
$params = $userFilters->getParams();
if (!empty($params)) {
$uri .= '?'.http_build_query($params);
}
}

return $this->loadPage(
User::class,
self::RESOURCE_KEY,
self::LIST_USERS_URI
$uri
);
}
}
20 changes: 20 additions & 0 deletions tests/Users/UserClientIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use HelpScout\Api\Tests\ApiClientIntegrationTestCase;
use HelpScout\Api\Tests\Payloads\UserPayloads;
use HelpScout\Api\Users\User;
use HelpScout\Api\Users\UserFilters;

/**
* @group integration
Expand Down Expand Up @@ -108,4 +109,23 @@ public function testGetUsersLazyLoadsPages()
['GET', 'https://api.helpscout.net/v2/users?page=2'],
]);
}

public function testListUsersWithFilters()
{
$this->stubResponse(
$this->getResponse(200, UserPayloads::getUsers(1, 10))
);

$filters = (new UserFilters())
->withMailbox(256);

$users = $this->client->users()->list($filters);

$this->assertCount(10, $users);
$this->assertInstanceOf(User::class, $users[0]);

$this->verifySingleRequest(
'https://api.helpscout.net/v2/users?mailbox=256'
);
}
}
30 changes: 30 additions & 0 deletions tests/Users/UserFiltersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace HelpScout\Api\Tests\Users;

use HelpScout\Api\Users\UserFilters;
use PHPUnit\Framework\TestCase;

class UserFiltersTest extends TestCase
{
public function testGetParamsDoesNotReturnNullValues()
{
$filters = new UserFilters();

$this->assertSame([], $filters->getParams());
}

public function testGetParams()
{
$filters = (new UserFilters())
->withMailbox(1)
->withEmail('[email protected]');

$this->assertSame([
'mailbox' => 1,
'email' => '[email protected]',
], $filters->getParams());
}
}

0 comments on commit 3a94a57

Please sign in to comment.