Skip to content

Commit

Permalink
Now casting eagerly loaded Threads to their appropriate types
Browse files Browse the repository at this point in the history
  • Loading branch information
bkuhl committed Jun 5, 2019
1 parent 9b2f56b commit a497a51
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 13 deletions.
2 changes: 1 addition & 1 deletion examples/conversations.php
Expand Up @@ -20,7 +20,7 @@

// GET conversation with the threads
$conversationRequest = new ConversationRequest();
$conversationRequest->withThreads();
$conversationRequest = $conversationRequest->withThreads();
$conversation = $client->conversations()->get(12, $conversationRequest);

// List conversations
Expand Down
5 changes: 5 additions & 0 deletions src/Conversations/Conversation.php
Expand Up @@ -771,6 +771,11 @@ public function addCustomField(CustomField $customField): Conversation
/**
* Obtain the threads that were eagerly loaded when this conversation was obtained.
*
* We will attempt to map the incoming Thread to a typed class. The only threads
* we type are threads that can be created through the API (e.g. CustomerThread,
* NoteThread, etc.). We do not type any kind of system threads such as a notice
* that a Workflow has run on a Conversation.
*
* @see ConversationRequest
*
* @return Thread[]|Collection
Expand Down
8 changes: 6 additions & 2 deletions src/Conversations/ConversationLoader.php
Expand Up @@ -4,7 +4,7 @@

namespace HelpScout\Api\Conversations;

use HelpScout\Api\Conversations\Threads\Thread;
use HelpScout\Api\Conversations\Threads\ThreadFactory;
use HelpScout\Api\Customers\Customer;
use HelpScout\Api\Entity\LinkedEntityLoader;
use HelpScout\Api\Mailboxes\Mailbox;
Expand Down Expand Up @@ -43,7 +43,11 @@ public function load()
}

if ($this->shouldLoadResource(ConversationLinks::THREADS)) {
$threads = $this->loadResources(Thread::class, ConversationLinks::THREADS);
$threadFactory = new ThreadFactory();
$threads = $this->loadResources(function (array $data) use ($threadFactory) {
return $threadFactory->make($data['type'], $data);
}, ConversationLinks::THREADS);

$conversation->setThreads($threads);
}

Expand Down
5 changes: 5 additions & 0 deletions src/Conversations/Threads/ThreadFactory.php
Expand Up @@ -15,6 +15,11 @@ class ThreadFactory
'_default' => Thread::class,
];

/**
* Attempt to map the incoming Thread to a typed class. The only threads we type
* are threads that can be created through the API. We do not type any kind of
* system threads such as a notice that a Workflow has run on a Conversation.
*/
public function make(string $type, array $data): Thread
{
/** @var Thread $thread */
Expand Down
8 changes: 6 additions & 2 deletions src/Entity/LinkedEntityLoader.php
Expand Up @@ -48,6 +48,8 @@ protected function shouldLoadResource(string $rel): bool
* @param string $rel
*
* @return mixed
* @throws \HelpScout\Api\Exception\JsonException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
protected function loadResource(string $entityClass, string $rel)
{
Expand All @@ -57,12 +59,14 @@ protected function loadResource(string $entityClass, string $rel)
}

/**
* @param string $entityClass
* @param \Closure|string $entityClass
* @param string $rel
*
* @return Collection
* @throws \HelpScout\Api\Exception\JsonException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
protected function loadResources(string $entityClass, string $rel): Collection
protected function loadResources($entityClass, string $rel): Collection
{
$uri = $this->resource->getLinks()->getHref($rel);
$resources = $this->restClient->getResources($entityClass, $rel, $uri);
Expand Down
23 changes: 17 additions & 6 deletions src/Http/RestClient.php
Expand Up @@ -72,9 +72,10 @@ public function getDefaultHeaders(): array

/**
* @param Extractable $entity
* @param string $uri
* @param string $uri
*
* @return int|null
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function createResource(Extractable $entity, string $uri): ?int
{
Expand All @@ -94,7 +95,8 @@ public function createResource(Extractable $entity, string $uri): ?int

/**
* @param Extractable $entity
* @param string $uri
* @param string $uri
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function updateResource(Extractable $entity, string $uri): void
{
Expand All @@ -109,7 +111,8 @@ public function updateResource(Extractable $entity, string $uri): void

/**
* @param Extractable $entity
* @param string $uri
* @param string $uri
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function patchResource(Extractable $entity, string $uri): void
{
Expand All @@ -124,6 +127,7 @@ public function patchResource(Extractable $entity, string $uri): void

/**
* @param string $uri
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function deleteResource(string $uri): void
{
Expand All @@ -137,9 +141,11 @@ public function deleteResource(string $uri): void

/**
* @param \Closure|string $entityClass
* @param string $uri
* @param string $uri
*
* @return HalResource
* @throws \HelpScout\Api\Exception\JsonException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getResource($entityClass, string $uri): HalResource
{
Expand All @@ -158,6 +164,8 @@ public function getResource($entityClass, string $uri): HalResource
* @param Report $report
*
* @return array
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \HelpScout\Api\Exception\JsonException
*/
public function getReport(Report $report): array
{
Expand All @@ -175,10 +183,12 @@ public function getReport(Report $report): array

/**
* @param \Closure|string $entityClass
* @param string $rel
* @param string $uri
* @param string $rel
* @param string $uri
*
* @return HalResources
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \HelpScout\Api\Exception\JsonException
*/
public function getResources($entityClass, string $rel, string $uri): HalResources
{
Expand Down Expand Up @@ -207,6 +217,7 @@ private function encodeEntity(Extractable $entity): string
* @param Request $request
*
* @return mixed|ResponseInterface
* @throws \GuzzleHttp\Exception\GuzzleException
*/
private function send(Request $request)
{
Expand Down
5 changes: 3 additions & 2 deletions tests/Conversations/ConversationIntegrationTest.php
Expand Up @@ -9,7 +9,7 @@
use HelpScout\Api\Conversations\ConversationRequest;
use HelpScout\Api\Conversations\CustomField;
use HelpScout\Api\Conversations\CustomFieldsCollection;
use HelpScout\Api\Conversations\Threads\Thread;
use HelpScout\Api\Conversations\Threads\CustomerThread;
use HelpScout\Api\Customers\Customer;
use HelpScout\Api\Entity\Collection;
use HelpScout\Api\Mailboxes\Mailbox;
Expand Down Expand Up @@ -221,7 +221,8 @@ public function testGetConversationPreloadsThreads()
$threads = $conversation->getThreads();

$this->assertInstanceOf(Collection::class, $threads);
$this->assertInstanceOf(Thread::class, $threads[0]);

$this->assertInstanceOf(CustomerThread::class, $threads[0]);

$this->verifyMultipleRequests([
['GET', 'https://api.helpscout.net/v2/conversations/1'],
Expand Down

0 comments on commit a497a51

Please sign in to comment.