Skip to content

Commit

Permalink
Zendesk Sell API and Contacts resource support
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-Sh committed Jan 12, 2021
1 parent 13f0c1f commit f2dfd37
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Zendesk/API/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
use Zendesk\API\Resources\HelpCenter;
use Zendesk\API\Resources\Talk;
use Zendesk\API\Resources\Voice;
use Zendesk\API\Resources\Sell;
use Zendesk\API\Traits\Utility\InstantiatorTrait;
use Zendesk\API\Utilities\Auth;

Expand Down Expand Up @@ -185,6 +186,11 @@ class HttpClient
*/
public $talk;

/**
* @var Sell
*/
public $sell;

/**
* @param string $subdomain
* @param string $username
Expand Down Expand Up @@ -229,6 +235,7 @@ public function __construct(
$this->embeddable = new Embeddable($this);
$this->chat = new Chat($this);
$this->talk = new Talk($this);
$this->sell = new Sell($this);
}

/**
Expand Down
42 changes: 42 additions & 0 deletions src/Zendesk/API/Resources/Sell.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Zendesk\API\Resources;

use Zendesk\API\HttpClient;
use Zendesk\API\Resources\Sell\Contacts;
use Zendesk\API\Traits\Utility\ChainedParametersTrait;
use Zendesk\API\Traits\Utility\InstantiatorTrait;

/**
* This class serves as a container to allow $this->client->sell
*
* @method Contacts contacts()
*/
class Sell
{
use ChainedParametersTrait;
use InstantiatorTrait;

public $client;

/**
* Sets the client to be used
*
* @param HttpClient $client
*/
public function __construct(HttpClient $client)
{
$this->client = $client;
}

/**
* @inheritdoc
* @return array
*/
public static function getValidSubResources()
{
return [
'contacts' => Contacts::class,
];
}
}
62 changes: 62 additions & 0 deletions src/Zendesk/API/Resources/Sell/Contacts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Zendesk\API\Resources\Sell;

use Zendesk\API\Traits\Resource\Defaults;
use Zendesk\API\Traits\Utility\InstantiatorTrait;

/**
* Contacts class provides a simple interface to manage your contacts in Sell
* https://developers.getbase.com/docs/rest/reference/contacts
*/
class Contacts extends ResourceAbstract
{
use InstantiatorTrait;
use Defaults;

/**
* {@inheritdoc}
*/
protected $objectName = 'data';

/**
* {@inheritdoc}
*/
protected $resourceName = 'contacts';

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

$this->setRoutes([
"find" => "{$this->resourceName}/{id}",
"findAll" => $this->resourceName,
"create" => $this->resourceName,
"update" => "{$this->resourceName}/{id}",
"delete" => "{$this->resourceName}/{id}",
"upsert" => "{$this->resourceName}/upsert",
]);
}

/**
* Create a new contact or update an existing, based on a value of a filter or a set of filters
* @param array $params
* @param array $updateResourceFields
* @return \stdClass|null
* @throws \Zendesk\API\Exceptions\ApiResponseException
* @throws \Zendesk\API\Exceptions\AuthException
*/
public function upsert(array $params, array $updateResourceFields)
{
$route = $this->getRoute(__FUNCTION__);

return $this->client->post(
$route,
[$this->objectName => $updateResourceFields],
['queryParams' => $params]
);
}
}
11 changes: 11 additions & 0 deletions src/Zendesk/API/Resources/Sell/ResourceAbstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Zendesk\API\Resources\Sell;

/**
* Abstract class for Sell resources
*/
abstract class ResourceAbstract extends \Zendesk\API\Resources\ResourceAbstract
{
protected $apiBasePath = 'v2/';
}
51 changes: 51 additions & 0 deletions tests/Zendesk/API/UnitTests/Sell/ContactsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Zendesk\API\UnitTests\Sell;

use Faker\Factory;
use Zendesk\API\UnitTests\BasicTest;

class ContactsTest extends BasicTest
{

/**
* Test that the correct traits were added by checking the available methods
*/
public function testMethods()
{
$this->assertTrue(method_exists($this->client->sell->contacts(), 'create'));
$this->assertTrue(method_exists($this->client->sell->contacts(), 'delete'));
$this->assertTrue(method_exists($this->client->sell->contacts(), 'find'));
$this->assertTrue(method_exists($this->client->sell->contacts(), 'findAll'));
$this->assertTrue(method_exists($this->client->sell->contacts(), 'update'));
$this->assertTrue(method_exists($this->client->sell->contacts(), 'upsert'));
}

/**
* Tests if the upsert endpoint can be called and passed the correct params
*/
public function testUpsert()
{
$faker = Factory::create();

$queryParams = [
'email' => $faker->email,
'phone' => $faker->phoneNumber,
];

$postFields = [
'email' => $faker->email,
'custom_fields' => [
'Some Field' => $faker->text
]
];

$this->assertEndpointCalled(function () use ($queryParams, $postFields) {
$this->client->sell->contacts()->upsert($queryParams, $postFields);
}, '/contacts/upsert', 'POST', [
'queryParams' => $queryParams,
'postFields' => ['data' => $postFields],
'apiBasePath' => '/v2'
]);
}
}

0 comments on commit f2dfd37

Please sign in to comment.