Add NexmoClient generic exception class

This commit is contained in:
Javier Hernández Gil 2014-04-09 00:09:57 +02:00
parent 50616db723
commit 058ac3e0b7
4 changed files with 29 additions and 8 deletions

View File

@ -0,0 +1,11 @@
<?php
namespace Jhg\NexmoBundle\NexmoClient\Exceptions;
/**
* Class NexmoClientException
* @package Jhg\NexmoBundle\NexmoClient\Exceptions
* @author Javi Hernández <javibilboweb@gmail.com>
*/
class NexmoClientException extends \Exception {
}

View File

@ -6,6 +6,6 @@ namespace Jhg\NexmoBundle\NexmoClient\Exceptions;
* @package Jhg\NexmoBundle\NexmoClient\Exceptions * @package Jhg\NexmoBundle\NexmoClient\Exceptions
* @author Javi Hernández <javibilboweb@gmail.com> * @author Javi Hernández <javibilboweb@gmail.com>
*/ */
class QuotaExcededException extends \Exception { class QuotaExcededException extends NexmoClientException {
} }

View File

@ -6,6 +6,6 @@ namespace Jhg\NexmoBundle\NexmoClient\Exceptions;
* @package Jhg\NexmoBundle\NexmoClient\Exceptions * @package Jhg\NexmoBundle\NexmoClient\Exceptions
* @author Javi Hernández * @author Javi Hernández
*/ */
class UnroutableSmsMessageException extends \Exception { class UnroutableSmsMessageException extends NexmoClientException {
} }

View File

@ -1,6 +1,7 @@
<?php <?php
namespace Jhg\NexmoBundle\NexmoClient; namespace Jhg\NexmoBundle\NexmoClient;
use Jhg\NexmoBundle\NexmoClient\Exceptions\NexmoClientException;
use Jhg\NexmoBundle\NexmoClient\Exceptions\QuotaExcededException; use Jhg\NexmoBundle\NexmoClient\Exceptions\QuotaExcededException;
use Jhg\NexmoBundle\NexmoClient\Exceptions\UnroutableSmsMessageException; use Jhg\NexmoBundle\NexmoClient\Exceptions\UnroutableSmsMessageException;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -133,21 +134,30 @@ class NexmoClient {
if($this->disable_delivery) { if($this->disable_delivery) {
$this->logger->debug("Nexmo sendTextMessage delivery disabled by config"); $this->logger->debug("Nexmo sendTextMessage delivery disabled by config");
return null; return array(
"status" => "0",
"message-id" => "delivery-disabled",
"to" => $toNumber,
"client-ref" => 0,
"remaining-balance" => 0,
"message-price" => 0,
"network" => 0,
);
} }
$response = $this->jsonRequest('/sms/json',$params); $response = $this->jsonRequest('/sms/json',$params);
if((int)$response['messages'][0]['status']!=0) { if(0 !== $code = (int)$response['messages'][0]['status']) {
switch((int)$response['messages'][0]['status']) { $error = $response['messages'][0]['error-text'];
switch( $code) {
case 6: case 6:
throw new UnroutableSmsMessageException(); throw new UnroutableSmsMessageException($error, $code);
case 9: case 9:
throw new QuotaExcededException(); throw new QuotaExcededException($error, $code);
default: default:
throw new \Exception($response['messages'][0]['error-text'],(int)$response['messages'][0]['status']); throw new NexmoClientException($error, $code);
} }
} }