diff --git a/.fernignore b/.fernignore index 084a8ebb..82321acd 100644 --- a/.fernignore +++ b/.fernignore @@ -1 +1,4 @@ # Specify files that shouldn't be modified by Fern +src/Legacy +composer.json +phpstan.neon \ No newline at end of file diff --git a/README.md b/README.md index 1c214f4f..21e9f39f 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,27 @@ foreach ($items->getPages() as $page) { } ``` +## Legacy SDK + +While the new SDK has a lot of improvements, we at Intercom understand that it takes time to upgrade when there are breaking changes. +To make the migration easier, the new SDK also exports the legacy SDK as `Intercom\Legacy\...`. Here's an example of how you can use the +legacy SDK alongside the new SDK inside a single file: + +```php +use Intercom\IntercomClient; +use Intercom\Legacy\IntercomClient as LegacyIntercomClient; + +$intercom = new IntercomClient(); +$legacyClient = new LegacyIntercomClient(); +``` + +We recommend migrating to the new SDK using the following steps: + +1. Upgrade the package to `^5.1.0` +2. Search and replace all requires and imports from `Intercom\...` to `Intercom\Legacy\...` + +3. Gradually move over to use the new SDK by importing it from the `Intercom\...` import. + ## Advanced diff --git a/composer.json b/composer.json index e69df79e..14e29170 100644 --- a/composer.json +++ b/composer.json @@ -11,6 +11,12 @@ "require": { "php": "^8.1", "ext-json": "*", + "php-http/client-common": "^2.0", + "php-http/discovery": "^1.0", + "php-http/message": "^1.0", + "psr/http-client": "^1.0", + "psr/http-message": "^1.0 || ^2.0", + "nyholm/psr7": "^1.0", "guzzlehttp/guzzle": "^7.4" }, "require-dev": { @@ -42,5 +48,10 @@ "homepage": "https://www.intercom.com" } ], + "config": { + "allow-plugins": { + "php-http/discovery": true + } + }, "homepage": "https://developers.intercom.com/docs" -} \ No newline at end of file +} diff --git a/phpstan.neon b/phpstan.neon index 780706b8..4c0f9626 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -3,4 +3,6 @@ parameters: reportUnmatchedIgnoredErrors: false paths: - src - - tests \ No newline at end of file + - tests + excludePaths: + - src/Legacy \ No newline at end of file diff --git a/src/Legacy/IntercomAdmins.php b/src/Legacy/IntercomAdmins.php new file mode 100644 index 00000000..fc2b977e --- /dev/null +++ b/src/Legacy/IntercomAdmins.php @@ -0,0 +1,48 @@ +client->get("admins", $options); + } + + /** + * Gets a single Admin based on the Intercom ID. + * + * @see https://developers.intercom.com/v2.0/reference#view-an-admin + * @param integer $id + * @param array $options + * @return stdClass + * @throws Exception + */ + public function getAdmin($id, $options = []) + { + $path = $this->adminPath($id); + return $this->client->get($path, $options); + } + + /** + * Returns endpoint path to Admin with given ID. + * + * @param string $id + * @return string + */ + public function adminPath($id) + { + return 'admins/' . $id; + } +} diff --git a/src/Legacy/IntercomBulk.php b/src/Legacy/IntercomBulk.php new file mode 100644 index 00000000..6f97d560 --- /dev/null +++ b/src/Legacy/IntercomBulk.php @@ -0,0 +1,33 @@ +client->post("bulk/users", $options); + } + + /** + * Creates Events in bulk. + * + * @param array $options + * @return stdClass + * @throws Exception + */ + public function events($options) + { + return $this->client->post("bulk/events", $options); + } +} diff --git a/src/Legacy/IntercomClient.php b/src/Legacy/IntercomClient.php new file mode 100644 index 00000000..b303b79d --- /dev/null +++ b/src/Legacy/IntercomClient.php @@ -0,0 +1,441 @@ +users = new IntercomUsers($this); + $this->contacts = new IntercomContacts($this); + $this->events = new IntercomEvents($this); + $this->companies = new IntercomCompanies($this); + $this->messages = new IntercomMessages($this); + $this->conversations = new IntercomConversations($this); + $this->leads = new IntercomLeads($this); + $this->visitors = new IntercomVisitors($this); + $this->admins = new IntercomAdmins($this); + $this->tags = new IntercomTags($this); + $this->segments = new IntercomSegments($this); + $this->counts = new IntercomCounts($this); + $this->bulk = new IntercomBulk($this); + $this->notes = new IntercomNotes($this); + $this->teams = new IntercomTeams($this); + + $this->appIdOrToken = $appIdOrToken; + $this->passwordPart = $password; + $this->extraRequestHeaders = $extraRequestHeaders; + + $this->httpClient = $this->getDefaultHttpClient(); + $this->requestFactory = Psr17FactoryDiscovery::findRequestFactory(); + $this->uriFactory = Psr17FactoryDiscovery::findUriFactory(); + $this->streamFactory = Psr17FactoryDiscovery::findStreamFactory(); + } + + /** + * Sets the HTTP client. + * + * @param ClientInterface $httpClient + */ + public function setHttpClient(ClientInterface $httpClient) + { + $this->httpClient = $httpClient; + } + + /** + * Sets the request factory. + * + * @param RequestFactoryInterface $requestFactory + */ + public function setRequestFactory(RequestFactoryInterface $requestFactory) + { + $this->requestFactory = $requestFactory; + } + + /** + * Sets the URI factory. + * + * @param UriFactoryInterface $uriFactory + */ + public function setUriFactory(UriFactoryInterface $uriFactory) + { + $this->uriFactory = $uriFactory; + } + + /** + * Sets the stream factory. + * + * @param StreamFactoryInterface $streamFactory + */ + public function setStreamFactory(StreamFactoryInterface $streamFactory) + { + $this->streamFactory = $streamFactory; + } + + /** + * Sends POST request to Intercom API. + * + * @param string $endpoint + * @param array $json + * @return stdClass + */ + public function post($endpoint, $json) + { + $response = $this->sendRequest('POST', "https://api.intercom.io/$endpoint", $json); + return $this->handleResponse($response); + } + + /** + * Sends PUT request to Intercom API. + * + * @param string $endpoint + * @param array $json + * @return stdClass + */ + public function put($endpoint, $json) + { + $response = $this->sendRequest('PUT', "https://api.intercom.io/$endpoint", $json); + return $this->handleResponse($response); + } + + /** + * Sends DELETE request to Intercom API. + * + * @param string $endpoint + * @param array $json + * @return stdClass + */ + public function delete($endpoint, $json) + { + $response = $this->sendRequest('DELETE', "https://api.intercom.io/$endpoint", $json); + return $this->handleResponse($response); + } + + /** + * Sends GET request to Intercom API. + * + * @param string $endpoint + * @param array $queryParams + * @return stdClass + */ + public function get($endpoint, $queryParams = []) + { + $uri = $this->uriFactory->createUri("https://api.intercom.io/$endpoint"); + if (!empty($queryParams)) { + $uri = $uri->withQuery(http_build_query($queryParams)); + } + + $response = $this->sendRequest('GET', $uri); + + return $this->handleResponse($response); + } + + /** + * Returns the next page of the result. + * + * @param stdClass $pages + * @return stdClass + */ + public function nextPage($pages) + { + $response = $this->sendRequest('GET', $pages->next); + return $this->handleResponse($response); + } + + /** + * Returns the next page of the result for a search query. + * + * @param string $path + * @param array $query + * @param stdClass $pages + * @return stdClass + */ + public function nextSearchPage(string $path, array $query, $pages) + { + $options = [ + "query" => $query, + "pagination" => [ + "per_page" => $pages->per_page, + "starting_after" => $pages->next->starting_after, + ] + ]; + return $this->post($path, $options); + } + + /** + * Returns the next page of the result for a cursor based search. + * + * @param string $path + * @param string $startingAfter + * @return stdClass + */ + public function nextCursorPage(string $path, string $startingAfter) + { + return $this->get($path . "?starting_after=" . $startingAfter); + } + + /** + * Gets the rate limit details. + * + * @return array + */ + public function getRateLimitDetails() + { + return $this->rateLimitDetails; + } + + /** + * @return ClientInterface + */ + private function getDefaultHttpClient() + { + return new PluginClient( + Psr18ClientDiscovery::find(), + [new ErrorPlugin()] + ); + } + + /** + * @return array + */ + private function getRequestHeaders() + { + return array_merge( + [ + 'Accept' => 'application/json', + 'Content-Type' => 'application/json', + 'User-Agent' => 'Intercom-PHP/' . self::SDK_VERSION, + ], + $this->extraRequestHeaders + ); + } + + /** + * Returns authentication parameters + * + * @return Authentication + */ + private function getAuth() + { + if (!empty($this->appIdOrToken) && !empty($this->passwordPart)) { + return new BasicAuth($this->appIdOrToken, $this->passwordPart); + } elseif (!empty($this->appIdOrToken)) { + return new Bearer($this->appIdOrToken); + } + return null; + } + + /** + * Authenticates a request object + * @param RequestInterface $request + * + * @return RequestInterface + */ + private function authenticateRequest(RequestInterface $request) + { + $auth = $this->getAuth(); + return $auth ? $auth->authenticate($request) : $request; + } + + /** + * @param string $method + * @param string|UriInterface $uri + * @param array|string|null $body + * + * @return ResponseInterface + * @throws ClientExceptionInterface + */ + private function sendRequest($method, $uri, $body = null) + { + $body = is_array($body) ? json_encode($body) : $body; + $request = $this->requestFactory + ->createRequest($method, $uri); + + if ($body !== null) { + $request = $request + ->withBody($this->streamFactory->createStream($body)); + } + + foreach ($this->getRequestHeaders() as $name => $value) { + $request = $request + ->withHeader($name, $value); + } + + $request = $this->authenticateRequest($request); + + return $this->httpClient->sendRequest($request); + } + + /** + * @param ResponseInterface $response + * + * @return stdClass + */ + private function handleResponse(ResponseInterface $response) + { + $this->setRateLimitDetails($response); + + $stream = $response->getBody()->getContents(); + + return json_decode($stream); + } + + /** + * @param ResponseInterface $response + */ + private function setRateLimitDetails(ResponseInterface $response) + { + $this->rateLimitDetails = [ + 'limit' => $response->hasHeader('X-RateLimit-Limit') + ? (int)$response->getHeader('X-RateLimit-Limit')[0] + : null, + 'remaining' => $response->hasHeader('X-RateLimit-Remaining') + ? (int)$response->getHeader('X-RateLimit-Remaining')[0] + : null, + 'reset_at' => $response->hasHeader('X-RateLimit-Reset') + ? (new \DateTimeImmutable())->setTimestamp((int)$response->getHeader('X-RateLimit-Reset')[0]) + : null, + ]; + } +} diff --git a/src/Legacy/IntercomCompanies.php b/src/Legacy/IntercomCompanies.php new file mode 100644 index 00000000..97290285 --- /dev/null +++ b/src/Legacy/IntercomCompanies.php @@ -0,0 +1,149 @@ +client->post("companies", $options); + } + + /** + * Updates a Company. + * + * @see https://developers.intercom.io/reference#create-or-update-company + * @param array $options + * @return stdClass + * @throws Exception + */ + public function update($options) + { + return $this->create($options); + } + + /** + * Attaches a Contact to a Company. + * + * @see https://developers.intercom.io/reference#attach-contact-to-company + * @param string $contactId + * @param string $companyId + * @param array $options + * @return stdClass + * @throws Exception + */ + public function attachContact(string $contactId, string $companyId, array $options = []) + { + $path = $this->companyAttachPath($contactId); + $options = array_merge($options, ["id" => $companyId]); + return $this->client->post($path, $options); + } + + /** + * Detaches a Contact from a Company. + * + * @see https://developers.intercom.io/reference#detach-contact-from-company + * @param string $contactId + * @param string $companyId + * @param array $options + * @return stdClass + * @throws Exception + */ + public function detachContact(string $contactId, string $companyId, array $options = []) + { + $path = $this->companyDetachPath($contactId, $companyId); + return $this->client->delete($path, $options); + } + + /** + * Returns list of Companies. + * + * @see https://developers.intercom.io/reference#list-companies + * @param array $options + * @return stdClass + * @throws Exception + */ + public function getCompanies($options) + { + return $this->client->get("companies", $options); + } + + /** + * Gets a single Company based on the Intercom ID. + * + * @see https://developers.intercom.com/reference#view-a-company + * @param string $id + * @param array $options + * @return stdClass + * @throws Exception + */ + public function getCompany($id, $options = []) + { + $path = $this->companyPath($id); + return $this->client->get($path, $options); + } + + + /** + * Returns a list of Users belonging to a single Company based on the Intercom ID. + * + * @see https://developers.intercom.com/reference#list-company-users + * @param string $id + * @param array $options + * @return stdClass + * @throws Exception + */ + public function getCompanyUsers($id, $options = []) + { + $path = $this->companyUsersPath($id); + return $this->client->get($path, $options); + } + + /** + * @param string $id + * @return string + */ + public function companyPath($id) + { + return 'companies/' . $id; + } + + /** + * @param string $id + * @return string + */ + public function companyUsersPath($id) + { + return 'companies/' . $id . '/users'; + } + + /** + * @param string $contactId + * @return string + */ + public function companyAttachPath(string $contactId) + { + return 'contacts/' . $contactId . '/companies'; + } + + /** + * @param string $contactId + * @param string $companyId + * @return string + */ + public function companyDetachPath(string $contactId, string $companyId) + { + return 'contacts/' . $contactId . '/companies/' . $companyId; + } +} diff --git a/src/Legacy/IntercomContacts.php b/src/Legacy/IntercomContacts.php new file mode 100644 index 00000000..1f0de82e --- /dev/null +++ b/src/Legacy/IntercomContacts.php @@ -0,0 +1,133 @@ +client->post("contacts", $options); + } + + /** + * Updates a Contact. + * + * @see https://developers.intercom.com/intercom-api-reference/reference#update-contact + * @param string $id + * @param array $options + * @return stdClass + * @throws Exception + */ + public function update(string $id, array $options) + { + $path = $this->contactPath($id); + return $this->client->put($path, $options); + } + + /** + * Lists Contacts. + * + * @see https://developers.intercom.com/intercom-api-reference/reference#list-contacts + * @param array $options + * @return stdClass + * @throws Exception + */ + public function getContacts(array $options = []) + { + return $this->client->get('contacts', $options); + } + + /** + * Gets a single Contact based on the Intercom ID. + * + * @see https://developers.intercom.com/intercom-api-reference/reference#get-contact + * @param string $id + * @param array $options + * @return stdClass + * @throws Exception + */ + public function getContact(string $id, array $options = []) + { + $path = $this->contactPath($id); + return $this->client->get($path, $options); + } + + /** + * Permenently Deletes a single Contact based on the Intercom ID. + * + * @see https://developers.intercom.com/intercom-api-reference/reference#delete-contact + * @param string $id + * @param array $options + * @return stdClass + * @throws Exception + */ + public function deleteContact(string $id, array $options = []) + { + $path = $this->contactPath($id); + return $this->client->delete($path, $options); + } + + /** + * Returns list of Contacts that match search query. + * + * @see https://developers.intercom.com/reference#search-for-contacts + * @param array $options + * @return stdClass + * @throws Exception + */ + public function search(array $options) + { + $path = 'contacts/search'; + return $this->client->post($path, $options); + } + + /** + * Returns next page of Contacts that match search query. + * + * @see https://developers.intercom.com/intercom-api-reference/reference#pagination-search + * @param array $query + * @param stdClass $pages + * @return stdClass + * @throws Exception + */ + public function nextSearch(array $query, $pages) + { + $path = 'contacts/search'; + return $this->client->nextSearchPage($path, $query, $pages); + } + + /** + * Returns next page of a Contacts list. + * + * @see https://developers.intercom.com/intercom-api-reference/reference#pagination + * @param stdClass $pages + * @return stdClass + * @throws Exception + */ + public function nextCursor($pages) + { + $path = 'contacts'; + $starting_after = $pages->next->starting_after; + return $this->client->nextCursorPage($path, $starting_after); + } + + /** + * @param string $id + * @return string + */ + public function contactPath(string $id) + { + return 'contacts/' . $id; + } +} diff --git a/src/Legacy/IntercomConversations.php b/src/Legacy/IntercomConversations.php new file mode 100644 index 00000000..e5f6de4a --- /dev/null +++ b/src/Legacy/IntercomConversations.php @@ -0,0 +1,145 @@ +client->post('conversations', $options); + } + + /** + * Returns list of Conversations. + * + * @see https://developers.intercom.io/reference#list-conversations + * @param array $options + * @return stdClass + * @throws Exception + */ + public function getConversations($options) + { + return $this->client->get('conversations', $options); + } + + /** + * Returns single Conversation. + * + * @see https://developers.intercom.io/reference#get-a-single-conversation + * @param string $id + * @param array $options + * @return stdClass + * @throws Exception + */ + public function getConversation($id, $options = []) + { + $path = $this->conversationPath($id); + return $this->client->get($path, $options); + } + + /** + * Returns list of Conversations that match search query. + * + * @see https://developers.intercom.com/intercom-api-reference/reference#search-for-conversations + * @param array $options + * @return stdClass + * @throws Exception + */ + public function search(array $options) + { + $path = 'conversations/search'; + return $this->client->post($path, $options); + } + + /** + * Returns next page of Conversations that match search query. + * + * @see https://developers.intercom.com/intercom-api-reference/reference#pagination-search + * @param array $query + * @param stdClass $pages + * @return stdClass + * @throws Exception + */ + public function nextSearch(array $query, $pages) + { + $path = 'conversations/search'; + return $this->client->nextSearchPage($path, $query, $pages); + } + + /** + * Creates Conversation Reply to Conversation with given ID. + * + * @see https://developers.intercom.io/reference#replying-to-a-conversation + * @param string $id + * @param array $options + * @return stdClass + * @throws Exception + */ + public function replyToConversation($id, $options) + { + $path = $this->conversationReplyPath($id); + return $this->client->post($path, $options); + } + + /** + * Creates Conversation Reply to last conversation. (no need to specify Conversation ID.) + * + * @see https://developers.intercom.io/reference#replying-to-users-last-conversation + * @param array $options + * @return stdClass + * @throws Exception + */ + public function replyToLastConversation($options) + { + $path = 'conversations/last/reply'; + return $this->client->post($path, $options); + } + + /** + * Marks a Conversation as read based on the given Conversation ID. + * + * @see https://developers.intercom.io/reference#marking-a-conversation-as-read + * @param string $id + * @return stdClass + * @throws Exception + */ + public function markConversationAsRead($id) + { + $path = $this->conversationPath($id); + $data = ['read' => true]; + return $this->client->put($path, $data); + } + + /** + * Returns endpoint path to Conversation with given ID. + * + * @param string $id + * @return string + */ + public function conversationPath($id) + { + return 'conversations/' . $id; + } + + /** + * Returns endpoint path to Conversation Reply for Conversation with given ID. + * + * @param string $id + * @return string + */ + public function conversationReplyPath($id) + { + return 'conversations/' . $id . '/reply'; + } +} diff --git a/src/Legacy/IntercomCounts.php b/src/Legacy/IntercomCounts.php new file mode 100644 index 00000000..440df671 --- /dev/null +++ b/src/Legacy/IntercomCounts.php @@ -0,0 +1,22 @@ +client->get("counts", $options); + } +} diff --git a/src/Legacy/IntercomEvents.php b/src/Legacy/IntercomEvents.php new file mode 100644 index 00000000..0edf024a --- /dev/null +++ b/src/Legacy/IntercomEvents.php @@ -0,0 +1,35 @@ +client->post("events", $options); + } + + /** + * Lists User Events. + * + * @see https://developers.intercom.io/reference#list-user-events + * @param array $options + * @return stdClass + * @throws Exception + */ + public function getEvents($options) + { + return $this->client->get("events", array_merge(["type" => "user"], $options)); + } +} diff --git a/src/Legacy/IntercomLeads.php b/src/Legacy/IntercomLeads.php new file mode 100644 index 00000000..330ccd08 --- /dev/null +++ b/src/Legacy/IntercomLeads.php @@ -0,0 +1,115 @@ +client->post("contacts", $options); + } + + /** + * Creates Lead. + * + * @see https://developers.intercom.io/reference#create-lead + * @param array $options + * @return stdClass + * @throws Exception + */ + public function update($options) + { + return $this->create($options); + } + + /** + * Lists Leads. + * + * @see https://developers.intercom.io/reference#list-leads + * @param array $options + * @return stdClass + * @throws Exception + */ + public function getLeads($options) + { + return $this->client->get("contacts", $options); + } + + /** + * Returns single Lead. + * + * @see https://developers.intercom.io/reference#view-a-lead + * @param string $id + * @param array $options + * @return stdClass + * @throws Exception + */ + public function getLead($id, $options = []) + { + $path = $this->leadPath($id); + return $this->client->get($path, $options); + } + + /** + * Deletes Lead. + * + * @see https://developers.intercom.io/reference#delete-a-lead + * @param string $id + * @param array $options + * @return stdClass + * @throws Exception + */ + public function deleteLead($id, $options = []) + { + $path = $this->leadPath($id); + return $this->client->delete($path, $options); + } + + /** + * Converts Lead. + * + * @see https://developers.intercom.io/reference#convert-a-lead + * @param $options + * @return stdClass + * @throws Exception + */ + public function convertLead($options) + { + return $this->client->post("contacts/convert", $options); + } + + /** + * Returns endpoint path to Lead with given ID. + * + * @param string $id + * @return string + */ + public function leadPath($id) + { + return "contacts/" . $id; + } + + /** + * Gets a list of Leads through the contacts scroll API. + * + * @see https://developers.intercom.com/v2.0/reference#iterating-over-all-leads + * @param array $options + * @return stdClass + * @throws Exception + */ + public function scrollLeads($options = []) + { + return $this->client->get('contacts/scroll', $options); + } +} diff --git a/src/Legacy/IntercomMessages.php b/src/Legacy/IntercomMessages.php new file mode 100644 index 00000000..834d18e9 --- /dev/null +++ b/src/Legacy/IntercomMessages.php @@ -0,0 +1,22 @@ +client->post("messages", $options); + } +} diff --git a/src/Legacy/IntercomNotes.php b/src/Legacy/IntercomNotes.php new file mode 100644 index 00000000..f3f8dd42 --- /dev/null +++ b/src/Legacy/IntercomNotes.php @@ -0,0 +1,48 @@ +client->post("notes", $options); + } + + /** + * Lists Notes. + * + * @see https://developers.intercom.io/reference#list-notes-for-a-user + * @param array $options + * @return stdClass + * @throws Exception + */ + public function getNotes($options) + { + return $this->client->get("notes", $options); + } + + /** + * Returns single Note. + * + * @see https://developers.intercom.io/reference#view-a-note + * @param string $id + * @return stdClass + * @throws Exception + */ + public function getNote($id) + { + return $this->client->get("notes/" . $id, []); + } +} diff --git a/src/Legacy/IntercomResource.php b/src/Legacy/IntercomResource.php new file mode 100644 index 00000000..5895ebfd --- /dev/null +++ b/src/Legacy/IntercomResource.php @@ -0,0 +1,21 @@ +client = $client; + } +} diff --git a/src/Legacy/IntercomSegments.php b/src/Legacy/IntercomSegments.php new file mode 100644 index 00000000..458fa160 --- /dev/null +++ b/src/Legacy/IntercomSegments.php @@ -0,0 +1,36 @@ +client->get('segments/' . $id, $options); + } + + /** + * Lists Segments. + * + * @see https://developers.intercom.com/reference#list-segments + * @param array $options + * @return stdClass + * @throws Exception + */ + public function getSegments($options = []) + { + return $this->client->get("segments", $options); + } +} diff --git a/src/Legacy/IntercomTags.php b/src/Legacy/IntercomTags.php new file mode 100644 index 00000000..a9c30ad9 --- /dev/null +++ b/src/Legacy/IntercomTags.php @@ -0,0 +1,35 @@ +client->post("tags", $options); + } + + /** + * Lists Tags. + * + * @see https://developers.intercom.io/reference#list-tags-for-an-app + * @param array $options + * @return stdClass + * @throws Exception + */ + public function getTags($options = []) + { + return $this->client->get("tags", $options); + } +} diff --git a/src/Legacy/IntercomTeams.php b/src/Legacy/IntercomTeams.php new file mode 100644 index 00000000..ed35060f --- /dev/null +++ b/src/Legacy/IntercomTeams.php @@ -0,0 +1,48 @@ +client->get("teams", $options); + } + + /** + * Gets a single Team based on the Intercom ID. + * + * @see https://developers.intercom.com/reference#view-a-team + * @param integer $id + * @param array $options + * @return stdClass + * @throws Exception + */ + public function getTeam($id, $options = []) + { + $path = $this->teamPath($id); + return $this->client->get($path, $options); + } + + /** + * Returns endpoint path to Team with given ID. + * + * @param string $id + * @return string + */ + public function teamPath($id) + { + return 'teams/' . $id; + } +} diff --git a/src/Legacy/IntercomUsers.php b/src/Legacy/IntercomUsers.php new file mode 100644 index 00000000..27659673 --- /dev/null +++ b/src/Legacy/IntercomUsers.php @@ -0,0 +1,129 @@ +client->post("users", $options); + } + + /** + * Creates a User. + * + * @see https://developers.intercom.io/reference#create-or-update-user + * @param array $options + * @return stdClass + * @throws Exception + */ + public function update(array $options) + { + return $this->create($options); + } + + /** + * Lists Users. + * + * @see https://developers.intercom.io/reference#list-users + * @param array $options + * @return stdClass + * @throws Exception + */ + public function getUsers(array $options) + { + return $this->client->get('users', $options); + } + + /** + * Gets a single User based on the Intercom ID. + * + * @see https://developers.intercom.com/reference#view-a-user + * @param string $id + * @param array $options + * @return stdClass + * @throws Exception + */ + public function getUser($id, $options = []) + { + $path = $this->userPath($id); + return $this->client->get($path, $options); + } + + /** + * Gets a list of Users through the user scroll API. + * + * @see https://developers.intercom.com/reference#iterating-over-all-users + * @param array $options + * @return stdClass + * @throws Exception + */ + public function scrollUsers(array $options = []) + { + return $this->client->get('users/scroll', $options); + } + + /** + * Deletes a single User based on the Intercom ID. + * + * @see https://developers.intercom.com/reference#archive-a-user + * @param string $id + * @param array $options + * @return stdClass + * @throws Exception + */ + public function archiveUser(string $id, array $options = []) + { + $path = $this->userPath($id); + return $this->client->delete($path, $options); + } + + /** + * Deletes a single User based on the Intercom ID. + * + * @see https://developers.intercom.com/reference#archive-a-user + * @param string $id + * @param array $options + * @return stdClass + * @throws Exception + */ + public function deleteUser(string $id, array $options = []) + { + return $this->archiveUser($id, $options); + } + + /** + * Permanently deletes a single User based on the Intercom ID. + * + * @see https://developers.intercom.com/reference#delete-users + * @param string $id + * @return stdClass + * @throws Exception + */ + public function permanentlyDeleteUser(string $id) + { + return $this->client->post('user_delete_requests', [ + 'intercom_user_id' => $id + ]); + } + + /** + * @param string $id + * @return string + */ + public function userPath(string $id) + { + return 'users/' . $id; + } +} diff --git a/src/Legacy/IntercomVisitors.php b/src/Legacy/IntercomVisitors.php new file mode 100644 index 00000000..d44f493f --- /dev/null +++ b/src/Legacy/IntercomVisitors.php @@ -0,0 +1,77 @@ +client->put("visitors", $options); + } + + + /** + * Returns single Visitor. + * + * @see https://developers.intercom.com/reference#view-a-visitor + * @param string $id + * @param array $options + * @return stdClass + * @throws Exception + */ + public function getVisitor($id, $options = []) + { + $path = $this->visitorPath($id); + return $this->client->get($path, $options); + } + + /** + * Deletes Visitor. + * + * @see https://developers.intercom.com/reference#delete-a-visitor + * @param string $id + * @param array $options + * @return stdClass + * @throws Exception + */ + public function deleteVisitor($id, $options = []) + { + $path = $this->visitorPath($id); + return $this->client->delete($path, $options); + } + + /** + * Converts Visitor. + * + * @see https://developers.intercom.io/reference#convert-a-lead + * @param $options + * @return stdClass + * @throws Exception + */ + public function convertVisitor($options) + { + return $this->client->post("visitors/convert", $options); + } + + /** + * Returns endpoint path to Visitor with given ID. + * + * @param string $id + * @return string + */ + public function visitorPath($id) + { + return "visitors/" . $id; + } +}