コード例 #1
0
 /**
  * @param Message $message
  * @return SendMessageResult
  *
  * @throws ApiRuntimeException
  * @throws TransferException Thrown by Guzzle during communication failure or unexpected server behaviour.
  */
 public function send(Message $message)
 {
     $response = $this->guzzleClient->post('/messages', ['json' => ['originator' => $message->getOriginator(), 'recipients' => $message->getRecipient(), 'body' => $message->getBody()], 'exceptions' => false]);
     try {
         $document = $response->json();
     } catch (\RuntimeException $e) {
         throw new ApiRuntimeException('The MessageBird server did not return valid JSON.', [], $e);
     }
     if (isset($document['errors'])) {
         $errors = $document['errors'];
     } else {
         $errors = [];
     }
     $statusCode = (int) $response->getStatusCode();
     if (!in_array($statusCode, [200, 201, 204, 401, 404, 405, 422]) && !($statusCode >= 500 && $statusCode < 600)) {
         throw new ApiRuntimeException(sprintf('Unexpected MessageBird server behaviour (HTTP %d)', $statusCode), $errors);
     }
     if (!isset($document['recipients']['items'][0]['status'])) {
         $deliveryStatus = SendMessageResult::STATUS_NOT_SENT;
     } else {
         $deliveryStatus = $document['recipients']['items'][0]['status'];
     }
     return new SendMessageResult($deliveryStatus, $errors);
 }
コード例 #2
0
 /**
  * @param Message $message
  * @return array
  */
 private function createMessageLogContext(Message $message)
 {
     return ['message' => ['recipient' => $message->getRecipient(), 'body' => $message->getBody()]];
 }
コード例 #3
0
 public function testItHasARecipient()
 {
     $message = new Message('SURFnet', '31612345678', 'body');
     $this->assertEquals('31612345678', $message->getRecipient());
 }