コード例 #1
0
 /**
  * @param int                 $statusCode   The HTTP status code returned
  * @param string              $type         The type of request (e.g., GET, POST, etc.)
  * @param int                 $expected     The expected HTTP status code
  * @param string|array|object $responseBody The returned body
  *
  * @throws \HelpScout\ApiException If the expected $statusCode isn't returned
  */
 private function checkStatus($statusCode, $type, $expected = 200, $responseBody = array())
 {
     $expected = (array) $expected;
     if (!in_array($statusCode, $expected)) {
         $exception = new ApiException($this->getErrorMessage($statusCode, $type, $expected, $responseBody), $statusCode);
         if (array_key_exists('validationErrors', $responseBody)) {
             $exception->setErrors($responseBody['validationErrors']);
         }
         $this->debug($exception->getMessage(), 'ERROR', array('method' => $type, 'code' => $exception->getCode(), 'errors' => $exception->getErrors()));
         throw $exception;
     }
 }
 /**
  * @param $consoleCommand SyncCommandBase
  * @return \Closure
  */
 private static function generateProcessor($consoleCommand)
 {
     return function ($ticketsList) use($consoleCommand) {
         $processedTickets = array();
         $checkForDuplicates = $consoleCommand->option('checkDuplicates');
         foreach ($ticketsList as $grooveTicket) {
             $customerEmail = null;
             $grooveTicketNumber = $grooveTicket['number'];
             try {
                 if ($checkForDuplicates) {
                     /* @var $searchResults Collection */
                     $dateString = $grooveTicket['created_at'];
                     $searchResults = $consoleCommand->makeRateLimitedRequest(HELPSCOUT, function () use($consoleCommand, $dateString) {
                         return $consoleCommand->getHelpScoutClient()->conversationSearch("(modifiedAt:[{$dateString} TO {$dateString}])");
                     });
                     if ($searchResults->getCount() > 1) {
                         $helpscoutConversationNumber = null;
                         /* @var $searchConversation SearchConversation */
                         foreach ($searchResults->getItems() as $searchConversation) {
                             if (strcasecmp($searchConversation->getSubject(), $grooveTicket['title']) === 0) {
                                 $helpscoutConversationNumber = $searchConversation->getNumber();
                                 break;
                             }
                         }
                         if ($helpscoutConversationNumber) {
                             $consoleCommand->warn("Warning: Duplicate ticket #{$grooveTicketNumber} \"" . $grooveTicket['title'] . "\" on {$dateString} already uploaded to HelpScout (conversation #{$helpscoutConversationNumber}). Skipping.");
                             continue;
                         }
                     }
                 }
                 $hybridConversation = new HybridConversation();
                 $hybridConversation->setGrooveTicketNumber($grooveTicketNumber);
                 $conversation = new Conversation();
                 $conversation->setType('email');
                 $conversation->setSubject($grooveTicket['title']);
                 // mailbox
                 $mailboxName = $grooveTicket['mailbox'];
                 $assignedMailbox = APIHelper::findMatchingMailboxByName($mailboxName);
                 if (!$assignedMailbox) {
                     $mailboxRef = APIHelper::findMatchingMailboxByEmail(config('services.helpscout.default_mailbox'))->toRef();
                 } else {
                     $mailboxRef = $assignedMailbox->toRef();
                 }
                 $conversation->setMailbox($mailboxRef);
                 if (!$conversation->getMailbox()) {
                     $exception = new ApiException("Mailbox not found in HelpScout: " . $mailboxName);
                     $exception->setErrors(array(['property' => 'mailbox', 'message' => 'Mailbox not found', 'value' => $mailboxName]));
                     throw $exception;
                 }
                 $tags = $grooveTicket['tags'];
                 if ($tags && count($tags) > 0) {
                     $conversation->setTags($tags);
                 }
                 // CustomerRef
                 $matches = array();
                 if (isset($grooveTicket['links']['customer']) && preg_match('@^https://api.groovehq.com/v1/customers/(.*)@i', $grooveTicket['links']['customer']['href'], $matches) === 1) {
                     $customerEmail = $matches[1];
                     if (filter_var($customerEmail, FILTER_VALIDATE_EMAIL)) {
                         $helpscoutPersonRef = new PersonRef((object) array('email' => $customerEmail, 'type' => 'customer'));
                         $conversation->setCustomer($helpscoutPersonRef);
                         $conversation->setCreatedBy($helpscoutPersonRef);
                     } else {
                         $grooveCustomer = $consoleCommand->makeRateLimitedRequest(GROOVE, function () use($consoleCommand, $customerEmail) {
                             return $consoleCommand->getGrooveClient()->customers()->find(['customer_email' => $customerEmail])['customer'];
                         });
                         $helpscoutPersonRef = new PersonRef((object) array('email' => $grooveCustomer['email'], 'type' => 'customer'));
                         list($firstName, $lastName) = APIHelper::extractFirstAndLastNameFromFullName($grooveCustomer['name'], $consoleCommand);
                         $helpscoutPersonRef->setFirstName($firstName);
                         $helpscoutPersonRef->setLastName($lastName);
                         $conversation->setCustomer($helpscoutPersonRef);
                         $conversation->setCreatedBy($helpscoutPersonRef);
                     }
                 } else {
                     throw new ApiException("No customer defined for ticket: #{$grooveTicketNumber}");
                 }
                 // CreatedAt
                 $datetime = new DateTime($grooveTicket['created_at']);
                 $conversation->setCreatedAt($datetime->format('c'));
                 $conversation->setThreads(self::retrieveThreadsForGrooveTicket($consoleCommand, $grooveTicket));
                 $status = APIHelper::getHelpScoutStatusForGrooveState($grooveTicket['state']);
                 if ($status) {
                     $conversation->setStatus($status);
                 } else {
                     $consoleCommand->error("Unknown state provided for Groove ticket #{$grooveTicketNumber}: " . $grooveTicket['state']);
                 }
                 $hybridConversation->setConversation($conversation);
                 $processedTickets[] = $hybridConversation;
             } catch (ApiException $e) {
                 $consoleCommand->error("Failed to create HelpScout conversation for Groove ticket (#{$grooveTicketNumber} created by {$customerEmail} at " . $grooveTicket['created_at'] . "). Message was: \n" . APIHelper::formatApiExceptionArray($e));
             } catch (\CurlException $ce) {
                 $errorMessage = "CurlException encountered for ticket #{$grooveTicketNumber} \"" . $grooveTicket['summary'] . "\"";
                 $consoleCommand->error($errorMessage . ": " . $ce->getMessage());
             } catch (\ErrorException $errex) {
                 $errorMessage = "Error encountered for ticket #{$grooveTicketNumber} \"" . $grooveTicket['summary'] . "\"";
                 $consoleCommand->error($errorMessage . ": " . $errex->getMessage());
             } catch (\Exception $ex) {
                 $errorMessage = "Exception encountered for ticket #{$grooveTicketNumber} \"" . $grooveTicket['summary'] . "\"";
                 $consoleCommand->error($errorMessage . ": " . $ex->getMessage());
             }
         }
         return $processedTickets;
     };
 }