Skip to content

Commit

Permalink
Merge pull request #276 from Tremmors/master
Browse files Browse the repository at this point in the history
Add in user specific ticket endpoints.
  • Loading branch information
miogalang committed Oct 21, 2016
2 parents 02c91d0 + e0216ea commit 9409d32
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 1 deletion.
127 changes: 127 additions & 0 deletions src/Zendesk/API/Resources/Core/UserTickets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

namespace Zendesk\API\Resources\Core;

use Zendesk\API\Exceptions\MissingParametersException;
use Zendesk\API\Exceptions\ResponseException;
use Zendesk\API\Http;
use Zendesk\API\Resources\ResourceAbstract;
use Zendesk\API\Traits\Utility\InstantiatorTrait;

/**
* The UserTickets class exposes methods to retrieve tickets created, cc'ed to, or
* assigned to a user
*/
class UserTickets extends ResourceAbstract
{
use InstantiatorTrait;

/**
* Wrapper for common GET requests
*
* @param $route
* @param array $params
*
* @return \stdClass | null
* @throws ResponseException
* @throws \Exception
*/
private function sendGetRequest($route, array $params = [])
{
$response = Http::send(
$this->client,
$this->getRoute($route, $params),
['queryParams' => $params]
);

return $response;
}

/**
* Declares routes to be used by this resource.
*/
protected function setUpRoutes()
{
parent::setUpRoutes();

$this->setRoutes(
[
'requested' => 'users/{id}/tickets/requested.json',
'ccd' => 'users/{id}/tickets/ccd.json',
'assigned' => 'users/{id}/tickets/assigned.json',
]
);
}

/**
* List tickets that a user requested
*
* @param array $params
*
* @throws MissingParametersException
* @throws ResponseException
* @throws \Exception
* @return \stdClass | null
*/
public function requested(array $params = [])
{
$params = $this->addChainedParametersToParams(
$params,
['id' => Users::class]
);

if (! $this->hasKeys($params, ['id'])) {
throw new MissingParametersException(__METHOD__, ['id']);
}

return $this->sendGetRequest(__FUNCTION__, $params);
}

/**
* List tickets a user was CC'ed on
*
* @param array $params
*
* @throws MissingParametersException
* @throws ResponseException
* @throws \Exception
* @return \stdClass | null
*/
public function ccd(array $params = [])
{
$params = $this->addChainedParametersToParams(
$params,
['id' => Users::class]
);

if (! $this->hasKeys($params, ['id'])) {
throw new MissingParametersException(__METHOD__, ['id']);
}

return $this->sendGetRequest(__FUNCTION__, $params);
}

/**
* List tickets a user was assigned
*
* @param array $params
*
* @throws MissingParametersException
* @throws ResponseException
* @throws \Exception
* @return \stdClass | null
*/
public function assigned(array $params = [])
{
$params = $this->addChainedParametersToParams(
$params,
['id' => Users::class]
);

if (! $this->hasKeys($params, ['id'])) {
throw new MissingParametersException(__METHOD__, ['id']);
}

return $this->sendGetRequest(__FUNCTION__, $params);
}
}
3 changes: 2 additions & 1 deletion src/Zendesk/API/Resources/Core/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ public static function getValidSubResources()
'organizationMemberships' => OrganizationMemberships::class,
'organizationSubscriptions' => OrganizationSubscriptions::class,
'requests' => Requests::class,
'sessions' => Sessions::class
'sessions' => Sessions::class,
'tickets' => UserTickets::class,
];
}

Expand Down
43 changes: 43 additions & 0 deletions tests/Zendesk/API/UnitTests/Core/UserTicketsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Zendesk\API\UnitTests\Core;

use Zendesk\API\UnitTests\BasicTest;

/**
* UserTicket test class
*/
class UserTicketsTest extends BasicTest
{
protected $number;

/**
* Tests if the requested endpoint can be called by the client and is passed the correct ID
*/
public function testRelated()
{
$this->assertEndpointCalled(function () {
$this->client->users(12345)->tickets()->requested();
}, 'users/12345/tickets/requested.json');
}

/**
* Tests if the requested endpoint can be called by the client and is passed the correct ID
*/
public function testCCD()
{
$this->assertEndpointCalled(function () {
$this->client->users(12345)->tickets()->ccd();
}, 'users/12345/tickets/ccd.json');
}

/**
* Tests if the requested endpoint can be called by the client and is passed the correct ID
*/
public function testAssigned()
{
$this->assertEndpointCalled(function () {
$this->client->users(12345)->tickets()->assigned();
}, 'users/12345/tickets/assigned.json');
}
}

0 comments on commit 9409d32

Please sign in to comment.